如何创建 MySQL 数据库用户并赋予权限

创建数据库用户和系统用户是两件不同的事情。
在这篇文章中,将介绍如何创建 MySQL 数据库用户和分配权限。

登录到MySQL数据库

# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. 
Commands end with ; or g. 
Your MySQL connection id is 1112 Server version: 5.0.77 Source distribution 
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

mysql> create user jack@'localhost' identified by 'my_password'; 
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES on test.* to jack@'%'; 
Query OK, 0 rows affected (0.00 sec)
NOTE: The host definition of '%' or '' (null) refers to any host.

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

现在我们将验证用户 jack 是否已获得分配给他的所有权限。

让我们尝试在他获得权限的数据库的表中插入一些值。

# mysql -u jack -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1111
Server version: 5.0.77 Source distribution
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

mysql> show databases;
+--------------------+
| Database           | 
+--------------------+
| information_schema |
| mycomp             |
+--------------------+
2 rows in set (0.00 sec)

mysql> use mycomp;
Database changed

mysql> INSERT INTO employee VALUES ('3','Anita Chaudhary','22');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * from employee;
+----------+-----------------+------+
| SerialNo |          Name   |  Age |
+----------+-----------------+------+
| 1        | jack Prasad   |   23 |
| 2        |  cherry Dubey     |   24 |
| 3        | Anita Chaudhary |   22 |
+----------+-----------------+------+
3 rows in set (0.00 sec)
日期:2020-06-02 22:17:38 来源:oir作者:oir