mysql 用户权限管理 之 增删改查

Posted 闭关苦炼内功

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 用户权限管理 之 增删改查相关的知识,希望对你有一定的参考价值。

mysql 用户权限管理经常会使用到,之前也分享过,但是绝对没有这篇详细
这篇文章会对如何 增删改查 mysql 用户做详细具体展开


文章目录


1. 查询 mysql 数据库 现有用户及权限

select `user`,`host` from mysql.user;

mysql> select `user`,`host` from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

mysql>

2. 添加 mysql 数据库 新用户及权限

GRANT ALL PRIVILEGES ON *.* TO 'demo'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;

FLUSH PRIVILEGES;

mysql> GRANT ALL PRIVILEGES ON *.* TO 'demo'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.01 sec)

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

mysql> select `user`,`host` from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| demo          | localhost |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
5 rows in set (0.00 sec)

mysql>

3. 修改 mysql 数据库 某用户及权限

update mysql.user set `host`="%" where `user`="demo";

FLUSH PRIVILEGES;

mysql> update mysql.user set `host`="%" where `user`="demo";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

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

mysql> select `user`,`host` from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| demo          | %         |
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
5 rows in set (0.00 sec)

mysql>

4. 删除 mysql 数据库 某用户及权限

drop user 'demo'@'%';

FLUSH PRIVILEGES;

mysql> drop user 'demo'@'%';
Query OK, 0 rows affected (0.01 sec)

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

mysql> select `user`,`host` from mysql.user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

mysql>

之前写过的相关文章


小伙伴们,你学废了没?
我们下期见!拜拜!

以上是关于mysql 用户权限管理 之 增删改查的主要内容,如果未能解决你的问题,请参考以下文章

mysql 的数据的操作增删改查和用户管理

数据库之增删改查

django——个人博客之分页/筛选功能

用户和组 之 增删改

ORACLE 中如何把A用户的增删改查权限赋给B用户

使用自定义注解和AOP管理shiro权限