Experiment + theory: MySQL database addition, deletion, modification, query and database user authorization-super detailed, suitable for novices! ! !

One, the basic operation of the database

1.1, access to MySQL database

1.1.1, log in to the MySQL server

We first log in to the MySQL server, after the initialization process after installation, the default administrator root of the MySQL database. Log in to the local MySQL database as the root user and perform the following operations.

[root@localhost ~]# mysql -u root -p   ##用root用户登录
Enter password:   ##输入密码
mysql>      ## 进来了

1.1.2, access MySQL operation statement

mysql> show databases;   ## 查看数据库信息
+--------------------+
| Database           |
+--------------------+
| information_schema |              |
| mysql              |
| performance_schema |
| root               |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

1.1.3, Exit "mysql>" operating environment

In the "mysql>" operating environment, execute exit, quit or \t to exit the mysql> command tool and return to the original shell environment

mysql> exit
Bye
[root@localhost ~]# 

1.2, use MySQL database

1.2.1, view the database structure

1.2.1.1, view the libraries contained in the current server

SHOW DATABASE statement: used to view the libraries contained in the current MySQL server. After initializing the MySQL server, four libraries are established by default: sys, mysql, information_schema, and performance_schema (the mysql library contains tables related to user authentication). Perform the following operations to view.

mysql> show databases;   ## 查看数据库信息
+--------------------+
| Database           |
+--------------------+
| information_schema |              |
| mysql              |
| performance_schema |
| root               |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

1.2.1.2, view the tables contained in the currently used library

SHOW TABLES statement: used to view the tables contained in the current library. Before this operation, you need to use the USE statement to switch to the library used. For example: Perform the following operations to display all tables contained in the mysql library.

mysql> use mysql   ##切换到这个库
Database changed
mysql> show tables;  ##查看当前库中包含的表
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
........  ##以下省略 ..........
| users                     |
+---------------------------+
32 rows in set (0.00 sec)

MySQL database files are stored in the /usr/local/mysql/data directory. Each database corresponds to a subdirectory for storing data table files. Each piece of data corresponds to three files, and the suffixes are ".frm", "myd", and ".myi". Of course, there are a few that end with opt, csm, csv, and ibd.

1.2.1.3, view the structure of the table

DESCRIBE statement: used to display the structure of the table, that is, the information of the fields (columns) that make up the table. Need to formulate "library name. table name" as a parameter; if you only specify the indicated parameter, you need to switch to the specified target library through a USE statement.
Perform the following operations to view the structure of the user table in the mysql library, which has the same effect as directly executing the "DESCRIBE mysql.user;" statement

mysql> use mysql
Database changed
mysql> describe user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key | Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                       |       |
| User                   | char(32)                          | NO   | PRI |                       |       |
| account_locked         | enum('N','Y')                     | NO   |     | N                     |       |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.00 sec)   ## 中间省略

1.2.2, DDL-Data Definition Language

12.2.1, create a new library

CREATE DAREBASE statement: used to create a new library, you need to specify the database name as a parameter.
For example: perform the following operations to create a library named abc

mysql> create database abc;  ## 创建新的库
Query OK, 1 row affected (0.00 sec)

The newly created database is spatiotemporal and does not contain any tables. A folder with the same name as the newly created library will be automatically generated in the /usr/local/mysql/data directory

12.2.2, create a new table

CREATE TABLE statement: used to create a new table in the current library, you need to specify the data table name as a parameter, and define the fields used by the table.

mysql> use abc;  ## 跳转到abc库
mysql> create table abc (user_name CHAR(16) NOT NULL,user_passwd CHAR(48) DEFAULT '',PRIMARY KEY(user_name)); 
Query OK, 0 rows affected (0.00 sec)
mysql> describe abc;  ## 查看刚才创建的表
+-------------+----------+------+-----+---------+-------+
| Field       | Type     | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| user_name   | char(16) | NO   | PRI | NULL    |       |
| user_passwd | char(48) | YES  |     |         |       |
+-------------+----------+------+-----+---------+-------+
2 rows in set (0.02 sec)

In the above table, "Username" is a string of no more than 16 characters and cannot be empty;
"Password" is a string of no more than 48 characters (use MySQL function encryption when inserting records), the default value is blank String.
The PRIMARY statement sets the name of the primary key field.

12.2.3, delete a database

DROP DATABASE statement: used to delete the specified library, you need to specify the library name as a parameter. Such as: perform the following operations to delete the library named abc

mysql> drop database abc; ##删除 abc 数据库
Query OK, 1 row affected (0.00 sec)
mysql> show database;  ## 查看库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| TX                 |
| mysql              |
| performance_schema |
| root               |
| zbc                |
+--------------------+
6 rows in set (0.00 sec)

12.2.4, delete a data table

DROP TABLE statement: Used to delete tables in the library, you need to specify "Library name. Table name" as a parameter; if you only specify the table name parameter, you need to switch to the target database by executing a USE statement. Such as: perform the following operations to delete the data table named zbc

mysql> drop table mysql.zbc;
Query OK, 0 rows affected (0.00 sec)

1.2.3, DML-data manipulation language, DQL-data query statement

1.2.3.1, insert data record

INSERT INTO statement: used to insert new data records into the table.

mysql> create database kk;
Query OK, 1 row affected (0.00 sec)  ## 创建库 kk
mysql> create table KK (user_name CHAR(16) NOT NULL,user_passwd CHAR(48) DEFAULT '',PRIMARY KEY(user_name)); 
Query OK, 0 rows affected (0.00 sec)   ## 创建 表 KK
mysql> insert into  KK(user_name,user_passwd)values('lisi',PASSWORD('123456'));
Query OK, 1 row affected, 1 warning (0.00 sec)  ## 在表中插入记录,按指定插入
mysql> insert into KK values('wangwu',PASSWORD('456789'));
Query OK, 1 row affected, 1 warning (0.00 sec)  ## 在表中插入记录,按顺序插入
mysql> insert into KK(user_name,user_passwd) values('zhao','123456');  ## 不加 PASSWORD(),就是明文
Query OK, 1 row affected (0.00 sec)

1.2.3.2, query data records

When representing all fields, you can use the wildcard character *, if you want to display all data records, you can omit the where clause. For example: Performing the following operations will display all records in the table, but because of the addition of PASSWORD(), the password string is encrypted, so the actual password content will not be displayed directly

mysql> select * from kk.KK;  ## 查询 库kk中的表KK,所有内容
+-----------+-------------------------------------------+
| user_name | user_passwd                               |
+-----------+-------------------------------------------+
| lisi      | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| wangwu    | *E710DC2512FCF6F18FE0D652B53290DCB11F3334 |
| zhao      | 123456                                    |
+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

Insert picture description here
When you need a specific condition to find records, the where clause is essential. For example: Find the record of lisi, display the information of the user name and password field, you can do the following operations

mysql> select * from kk.KK where user_name='lisi';
+-----------+-------------------------------------------+
| user_name | user_passwd                               |
+-----------+-------------------------------------------+
| lisi      | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)

1.2.3.3, modify data records

UPDATE statement: used to modify and update data records in the table.
The sentence format is as follows:
UPDATE indicates that SET field name 1=field value 1[,field name 2=field value 2] WHERE conditional expression
executes the following operations to modify the record of the user name "lisi" in the KK table, and change the password string Set to a null value. Verify the content of the record to find the password string value of the lisi user so as to be blank

mysql> update kk.KK set user_passwd='' where user_name='lisi';
mysql> select * from kk.KK;  ## 修改完后进行验证
+-----------+-------------------------------------------+
| user_name | user_passwd                               |
+-----------+-------------------------------------------+
| lisi      |                                           |
| wangwu    | *E710DC2512FCF6F18FE0D652B53290DCB11F3334 |
| zhao      | 123456                                    |
+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

1.2.3.4, delete data records

DELETE statement: used to delete the specified data record in the table.

The statement format is as follows: DELETE FROM indicates the WHERE conditional expression

Perform the following operations to delete the data record of the user named "lisi" in the KK table. Verify the content of the record and find that the data record of the user "lisi" has disappeared

mysql> delete from kk.KK where user_name='lisi';
Query OK, 1 row affected (0.00 sec)  ## 删除用户名为lisi的记录
mysql> select * from kk.KK;  ## 查询一下KK表中lisi记录是否删除
+-----------+-------------------------------------------+
| user_name | user_passwd                               |
+-----------+-------------------------------------------+
| wangwu    | *E710DC2512FCF6F18FE0D652B53290DCB11F3334 |
| zhao      | 123456                                    |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)

Two, DCL-data control statement

2.1, set user permissions

  • DCL statement sets user permissions (when the user does not exist, create a new user)

GRANT permission list ON database name. Table name TO user name@source address [IDENTIFIEDBY'password']

mysql> grant select on kk.* to 'xiaoqi'@'localhost' identified by '123456'; 
 ## 给xiaoqi用户授权可以查看kk库里面表,用户不存在时,则新建用户,并写入密码123456
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;  ## 刷新
Query OK, 0 rows affected (0.00 sec)
mysql> exit  ## 退出当前用户
Bye
[root@localhost ~]# mysql -u xiaoqi -p  ##用xiaoqi用户登录数据库
Enter password: 
mysql> select * from kk.KK;   
## 验证授权访问操作,查看kk库里面的KK表,验证成功!
+-----------+-------------------------------------------+
| user_name | user_passwd                               |
+-----------+-------------------------------------------+
| wangwu    | *E710DC2512FCF6F18FE0D652B53290DCB11F3334 |
| zhao      | 123456                                    |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
mysql> drop database kk;  
## 验证非授权访问操作,没有给xiaoqi用户授权删除库的权力,所以无法删除!
ERROR 1044 (42000): Access denied for user 'xiaoqi'@'localhost' to database 'kk'

If you want a user (or a new user) to obtain all permissions, you can do the following

## 这边我们把之前的 select 改成 all,这样此用户就可获得对kk库的所有权限!
mysql> grant all on kk.* to 'xiaoqi'@'localhost' identified by '123456'; 

2.2. View user permissions

SHOW GRANTS for username@source address;

mysql>  show grants for xiaoqi@localhost;
+------------------------------------------------+
| Grants for xiaoqi@localhost                    |
+------------------------------------------------+
| GRANT USAGE ON *.* TO 'xiaoqi'@'localhost'     |
| GRANT SELECT ON "kk".* TO 'xiaoqi'@'localhost' |
+------------------------------------------------+
2 rows in set (0.00 sec)

2.3, revoke user permissions

REMOVE permission list ON database name. table name FROM user name@source address. To
revoke user permissions, you must log in with the highest authority of root to be revoked!

mysql> revoke select on kk.* from xiaoqi@localhost;  ## 撤销授权用户的权限
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for xiaoqi@localhost;  ## 查看被撤销授权用户的权限
+--------------------------------------------+
| Grants for xiaoqi@localhost                |
+--------------------------------------------+
| GRANT USAGE ON *.* TO 'xiaoqi'@'localhost' |
+--------------------------------------------+
1 row in set (0.01 sec)

Guess you like

Origin blog.csdn.net/m0_46563938/article/details/108488022