Mysql use basics

1. Connect to MYSQL.
Format: mysql -h host address -u user name -p user password
1. Example 1: Connect to MYSQL on this machine.
First open the DOS window, then enter the directory mysql\bin, then type the command mysql -uroot -p, and you will be prompted to enter the password after pressing Enter. If MYSQL has just been installed, the super user root has no password, so just press Enter. Enter into MYSQL, the prompt of MYSQL is: mysql>
2. Example 2: Connect to MYSQL on the remote host. Suppose the IP of the remote host is: 110.110.110.110, the user name is root, and the password is abcd123. Then type the following command:

mysql -h110.110.110.110 -uroot -pabcd123

(Note: u and root do not need to add spaces, the others are the same)
3. Exit the MYSQL command: exit (Enter)


Second, modify the password.
Format: mysqladmin -u username -p old password password new password
1. Example 1: Add a password ab12 to root. First enter the directory mysqlbin under DOS, and then type the following command
mysqladmin -uroot -password ab12
Note: Because root has no password at the beginning, the -p old password can be omitted.
2. Example 2: Change the root password to djg345.
mysqladmin -uroot -pab12 password djg345


Third, add new users.

(Note: Unlike the above, the following is a command in the MYSQL environment, so it is followed by a semicolon as the command terminator)
Format: grant select on database.* to username@login host identified by "password"
1. Example 1. Add a user test1 with a password of abc, so that he can log in on any host and have the permissions to query, insert, modify, and delete all databases. First connect to MYSQL as root user, and then type the following commands:
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
but the user added in example 1 is very dangerous, you If someone knows the password of test1, then he can log in to your mysql database on any computer on the internet and do whatever he wants with your data. See example 2 for the solution.
2. Example 2. Add a user test2 with a password of abc, so that he can only log in on localhost, and can query, insert, modify, and delete the database mydb (localhost refers to the local host, which is the one where the MYSQL database is located. host), so that even if the user knows the password of test2, he cannot directly access the database from the internet, but can only access it through the web page 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 type another command to remove the password.
grant select, insert, update, delete on mydb.


Simple operation of MYSQL database

Let 's take a look at the operation of the database in MYSQL. Note: You must first log in to MYSQL, the following operations are performed at the MYSQL prompt, and each command ends with a "semicolon".

1. Operation skills
1. If you forgot to add a semicolon when you press Enter, you don't need to type the command again, just type a "semicolon" and press Enter. That is to say, you can divide a complete command into several lines to type, and then use a semicolon as the end sign and it will be OK.
2. You can use the cursor up and down keys to recall previous commands. But an old version of MYSQL that I used before doesn't support it. I am using mysql-3.23.27-beta-win now.

Second, the display command
1, display the database list.

show databases;

there are only two databases at the beginning: mysql and test. The mysql library is very important. It contains MYSQL system information. When we change passwords and add new users, we actually use this library to operate.
2. Display the data tables in the library:

use mysql; //Open the library, those who have studied FOXBASE must not be unfamiliar
show tables;
3. Display the structure of the data table:

describe table name;
4. Build the database:

create database database name ;
5. Create table:

use database name;

create table table name (field setting list);
6. Delete database and delete table:

drop database database name;

drop table table name;
7. Empty the records in the table:

delete from table name;
8. Display the records in the table:

select * from table name;

3. An instance of building a database, building a table and inserting data
drop database if exists school; //If it exists SCHOOL deletes
create database school; //Create library SCHOOL
use school; //Open library SCHOOL
create table teacher //Create 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 building

//The following is the

insert into teacher values('','glchengang','Shenzhen No.1 Middle School','1976-10-10');

insert into teacher values('','jack','Shenzhen No.1 Middle School','1975-12-23');

Note, in the construction table:

(1) Set the ID to a numeric field with a length of 3: int(3) and let it automatically add one to each record: auto_increment cannot be empty: not null and make it the primary key of the primary field

(2) Set the NAME For a character field of length 10

(3), set ADDRESS to a character field of length 50, and the default value is Shenzhen. What is the difference between varchar and char, we can only talk about it in later articles.

(4) Set YEAR as the date field.

If you type the above command at the mysql prompt it's fine, but it's not easy to debug. You can write the above command as it is in a text file, assuming school.sql, then copy it to c:\, and enter the directory \mysql\bin in DOS state, then type the following command:

mysql -uroot -p password < c :\school.sql

If successful, leave a line without any display; if there is an error, there will be a prompt. (The above command has been debugged, you just need to remove the comment of // to use it).

4. Transfer the text data to the database
1. The format that the text data should conform to: separate the field data with the tab key, and replace the null value with \n.
Example:

3 rose Shenzhen No. 2 Middle School 1976-10-10

4 mike Shenzhen No.1 Middle School 1975-12-23
2. The data input command load data local infile "file name" into table table name;

Note: You'd better copy the file to the \mysql\bin directory, and use the use command to type The library where the table is located.

5. Backup the database:

(The command is executed in the \mysql\bin directory of DOS)

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 something new.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326321518&siteId=291194637