MySQL operation in Linux

1. Log in to MySQL

1. Start:

The MYSQL startup file is in the /etc/init.d directory, start and run the following command

/etc/init.d/mysql start

2. Stop:

/usr/bin/mysqladmin -u root -p shutdown

3. Login:

mysql [-u username] [-h host] [-p password] [dbname]

Here is the structure of MySQL: database -> table. The last dbname refers to the table name, you can specify which table in the database you want to use, otherwise you still need to specify the table after logging in to the database. Below I specify the scraping database

insert image description here

You can also log in without specifying the database. After logging in, you can specify the database name: USE + database name

insert image description here

2. Several important directories of MySQL

Database files, configuration files and command files are in separate directories

  1. Database directory: /var/lib/mysql/

  2. Configuration file: /usr/share/mysql (mysql.server command and its configuration file)

  3. Related commands: /usr/bin/ (mysql admin mysql dump and other commands)

  4. Startup script: /etc/rc.d/init.d (directory of startup script file mysql)

3. Common commands

1. Display the database

show databases; [must be followed by ' ; ' every command in MYSQL ends with ; ]

Among them, the myql library is very important. It contains the system information of MYSQL, and the modification of passwords and the addition of new users are all performed in the relevant tables.

2. Display the tables in the database

First use the library: use mysql

Show the tables in the database: show tables

3. Display the structure of the table

describe tablename;

4. Display record

select * from 表名

5. Create a new database

create database + database name;

6. Create table

USE library name;

create table table name (field setting list);

7. Add record:

insert into

8. Amendment records

update

9. Delete records

delete

10. Drop table/database

drop database + 库名;

drop table + table name;

4. User Settings

grant select on database.* to username@loginhostidentified by "password"

mysql> grant select,insert,update,delete on . to user_1@"%" Identified by “123”;

The user added in Example 1 is very dangerous. If he knows the password of user_1, he can log in to your MySQL database on any computer on the Internet and do whatever he wants with your data. The solution is shown in Example 2.

Example 2. Add a user user_2 with a password of 123, so that this user can only log in on localhost, and can query, insert, modify, and delete the database aaa (localhost refers to the local host, that is, the host where the MySQL database is located. ), so that even if the user knows the password of user_2, he cannot directly access the database from the Internet, and can only operate the aaa library through the MYSQL host.

mysql>grant select,insert,update,delete on aaa.* to user_2@localhost identified by “123”;

If you cannot log in to MySQL with the newly added user, use the following command when logging in:

mysql -u user_1 -p -h 192.168.113.50 (-h is followed by the ip address of the host to be logged in)

Guess you like

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