mysql 创建用户 并 授权
一,添加用户
1,create user
mysql> create user '44'@'127.0.0.1'; //创建一个44用户
Query OK, 0 rows affected (0.00 sec)
mysql> create user '33'@'localhost' identified by 'aaaa'; //创建一个33用户,密码为aaaa
Query OK, 0 rows affected (0.00 sec)
mysql> select * from mysql.user where user='33' or user='44'/G; //查看一下mysql下的user
create user 虽然可以创建用户,但是它只是创建用户,并没有给用户分配置权限,所以一般被 grant命令所取代。
2,grant
grant all ON test.* TO 'test'@'localhost'; //test用户拥有test数据库下的所有操作
grant select,update on test.user to 'test'@'localhost'; //test用户可以对test数据库下user表,进行查找和更新操作
//test用户的的密码是111111,对user表中的name字段有读取权限,对id,name有更新权限
grant select(name),update(id,name) on test.user to 'test'@'localhost' identified by '111111';
//test用户对所有数据库拥有所有权力,并且要求ssl加密
grant all privileges on *.* to 'test'@'%' identified by '123456' require ssl
当添加完用户后,别忘 了 flush privileges;
二,删除用户
drop user 'test2'@'localhost';
//当用drop删除用户进,tables_priv,procs_priv等表中的数据也会被删除
在这里为什么要用'test2'@'localhost'当用户名,而不是直接test2呢,因为mysql.user这张表,是根用户名和host名决定一个用户,你可查看一下表结构就知道了。
show create table mysql.user/G;你会发现有这个东西PRIMARY KEY (Host,User),表示联合主键
三,修改用户
rename user 'test2'@'localhost' to 'test'@'%';
四,修改权限
grant就给用户分配权限,revoke是把权限从用户的身上拿走。
mysql> revoke update on *.* from 'tank'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> select mysql.user.update_priv from mysql.user where user='tank' and host='localhost';
+-------------+
| update_priv |
+-------------+
| N |
+-------------+
1 row in set (0.00 sec)
去掉tank@localhost这个用户的更新功能,这个是去掉一个权限,如果我要全部去掉怎么办呢,一个一个写太麻烦了,看下面的一个例子
mysql> revoke all privileges ,grant option from '33'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
用掉33@localhost这个用户的所有权限
欢迎大家阅读《mysql 创建用户 并 受权_mysql》,跪求各位点评,by 搞代码
微信 赏一包辣条吧~
支付宝 赏一听可乐吧~