51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

mysql创建用户

# mysql创建用户 {#mysql创建用户}

本文介绍mysql如何创建用户,mysql低版本和8.0以后版本的操作方法略有不同。低版本的grant命令支持自动创建用户,而8.0以后版本不支持。

# 1. 创建用户 {#_1-创建用户}

若mysql版本小于8.0,该步骤可以省略。

ubuntu@VM-0-6-ubuntu:~$ mysql -uroot -pbuzhidao -h 127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 221
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

mysql> create user gitea@'%' identified by 'gitea';
Query OK, 0 rows affected (0.01 sec)

# 2. 授权 {#_2-授权}

授权的维度如下:

  • 哪个用户
  • 哪个数据库
  • 哪些表
  • 增加、删除、修改或查询等

示例如下:

mysql> grant all privileges on gitea.* to gitea@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)

mysql> 

# 3. 验证 {#_3-验证}

ubuntu@VM-0-6-ubuntu:~$ mysql -ugitea -pgitea -h 127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 224
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

mysql> 

可以看出该用户只能访问到数据库gitea,说明权限配置已生效。

赞(5)
未经允许不得转载:工具盒子 » mysql创建用户