In eight simple words, I will teach you how to deal with MySQL database _ MySQL

Source: Internet
Author: User
Tags mysql host
This article allows you to use eight simple sentences to teach you how to deal with the MySQL database. I. MYSQL connection format: mysql-h host address-u user name-p User Password 1. Example 1: Connect to MySQL on the local machine: first open the DOS window and then enter the directory mysqlbin, enter the mysql-uroot-p command and press enter to prompt you to enter the password.

This article allows you to use eight simple sentences to teach you how to deal with the MySQL database.

1. connect to MYSQL

Format:

     Mysql-h host address-u user name-p user password

1. Example 1: Connect to MySQL on the local machine:

First, open the DOS window, enter the directory mysqlbin, then type the command mysql-uroot-p, and press enter to prompt you to enter the password. if you have just installed MYSQL, super User root has no password, so press enter to enter MYSQL. the MYSQL prompt is: mysql>.

2. Example 2: Connect to MYSQL on the remote host. Assume that the IP address of the remote host is 110.110.110.110, the user name is root, and the password is abcd123. Run the following command: mysql-h110.110.110.110-uroot-pabcd123.

3. exit MYSQL command: exit (press enter ).

II. change the password

Format: mysqladmin-u username-p old password new password

1. Example 1: add a password ab12 to the root user. First, enter the directory mysqlbin in DOS, and then type the following command:

Mysqladmin-uroot-password ab12

Note: Because the root account does not have a password at the beginning, the old-p password can be omitted.

2. Example 2: change the root password to djg345:

Mysqladmin-uroot-pab12 password djg345

3. add new users

(Note: Unlike the above, the following commands in the MYSQL environment are followed by a semicolon as the command Terminator)

Format:

     Grant select on database. * to username @ login host identified by "password"

Example 1: add a user named "test1" with the password "abc" so that the user can log on to any host and have the permission to query, insert, modify, and delete all databases. First, use the root user to connect to MYSQL, and then type the following command:

     grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";

However, the user added in Example 1 is very dangerous. if someone knows the password of test1, then he can log on to your mysql database on any computer on the internet and do whatever he wants. for the solution, see example 2.

Example 2: Add a user named "test2" with the password "abc" so that the user can only log on to localhost, you can also query, insert, modify, and delete the database mydb (localhost refers to the local host, that is, the host where the MYSQL database is located), so that the user knows the password of test2, he cannot access the database directly from the internet, but can only access the database through the web pages on the MYSQL host.

Grant select, insert, update, delete on mydb. * to test2 @ localhost identified by "abc ";

If you do not want test2 to have a password, you can run another command to remove the password.

Grant select, insert, update, delete on mydb. * to test2 @ localhost identified "";

Note: you must first log on to MYSQL. the following operations are performed at the MYSQL prompt and each command ends with a semicolon.

IV. operation skills

1. if you forget the extra points after you press enter when making the command, you don't have to repeat the command. you just need to press a semicolon to press Enter. That is to say, you can divide a complete command into several lines, and then use a semicolon as the end sign to complete the operation. 2. you can use the cursor to bring up or down the previous commands. However, an old MYSQL version I used earlier does not support this feature. I am using: mysql-3.23.27-beta-win.

5. display commands

1. display the database list: show databases;

At the beginning, there were only two databases: mysql and test. The mysql database contains the MYSQL system information. we change the password and add new users to use this database for operations.

2. display the data table in the database:

     Use mysql; // open the database. if you have learned FOXBASE, you will not be unfamiliar with show tables;

3. display the structure of the data table: describe table name;

4. database creation: create database name;

5. create a table:

     Use database name; create table name (field setting list );

6. delete databases and tables:

     Drop database name; drop table name;

7. clear the records in the table: delete from table name;

8. display records in the table: select * from table name;

6. an instance for creating a database, creating tables, and inserting data

     Drop database if exists school; // if SCHOOL exists, delete create database school; // create a database SCHOOLuse school; // open the database SCHOOLcreate table teacher // create the table TEACHER (id int (3) auto_increment not null primary key, name char (10) not null, address varchar (50) default shenzhen, year date); // end of table creation // insert into teacher valuess (, glchengang, Shenzhen No. 1 Middle School, 1976-10-10); insert into teacher valuess (, jack, Shenzhen No. 1 Middle School, 1975-12-23 );

Note: in the table in progress (1), set the ID to a numeric field of 3: int (3) and make it automatically add one: auto_increment for each record. it cannot be blank: not null and set it as the main field primary key (2) set NAME to the character field with a length of 10 (3) set ADDRESS to the character field with a length of 50, the default value is Shenzhen. What is the difference between varchar and char? it will only be discussed later. (4) set YEAR as the date field.

If you type the preceding command at the mysql prompt, debugging is not convenient. You can write the preceding command as is to a text file, assuming it is school. SQL, then copy it to c:, and enter the directory mysqlbin in DOS state, and then type the following command:

Mysql-uroot-p password <c: school. SQL

If it succeeds, no display is displayed for a blank row. if there is an error, a prompt is displayed. (The preceding command has been debugged. you only need to remove the // annotation to use it ).

7. transfer text materials to the database

1. text data should conform to the format: field data should be separated by the tab key, and the null value should be replaced by n.

Example:

     3 rose Shenzhen No. 2 Middle School 1976-10-104 mike Shenzhen No. 1 1975-12-23

2. data input command load data local infile "file name" into table name;

Note: You 'd better copy the file to the mysqlbin directory and use the use command to create the database where the table is located.

8. back up the database:

(The command is executed in the DOS mysqlbin directory)

Mysqldump -- opt school> school. bbb

Note: back up the database school to the school. bbb file. school. bbb is a text file with any file name. open it and you will find new discoveries.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.