MySQL增加/删除用户、授权、修改密码
// 创建用户
mysql> use mysql;
mysql> insert into user(Host,User,Password) values('localhost','yongfu_a',password('my_password'));
// 刷新系统权限表
mysql> flush privileges;
//退出后登录一下
mysql>exit;
说明:
至此创建mysql的第二个用户:yongfu_a(第一个用户是root),他的密码是my_password。需要刷新系统权限表flush
privilege,该用户才能生效登录。此时该用户(yongfu_a)仅有在本机(localhost)使用密码(my_password)登录的权
限,还没有其他权限,需要使用GRANT命令对该用户进行相应授权。
通过GRANT授权的方式新增用户
// 以root用户登录mysql
[yongfu]$ mysql -uroot -p
// 新增一个新用户yongfu_b
mysql> grant all privileges on *.* to yongfu_b@'192.168.1.%' identified by 'my_password_2';
说明:
至此创建了mysql的第三个用户:yongfu_b,他的密码是my_password_2。不需要使用flush
privilege刷新系统权限表,改用户立即生效。此时该用户(yongfu_b)可以在192.168.1.0/24的网段上的任意机器使用密码
(my_password_2)登录。同时已经对所有库的所有表赋予了全部权限
ps:使用grant方法新增用户更方便也更省事。只需要一步就可以完成新增用户和用户授权的全部操作
2. MySQL修改用户密码
2.1 直接修改数据库修改密码
// 在数据库中修改密码字段
mysql> use mysql
mysql> update user set Password = password("new_password") where User = "yongfu_a" and Host="localhost";
mysql> flush privileges;
2.2 通过GRANT方法修改密码
// 其实就是新增用户那个方法,grant本身是授权、授予的意思。
// 修改用户yongfu_b的密码为 my_password_new
mysql> grant all privileges on *.* to yongfu_b@'192.168.1.%' identified by 'my_password_new';
*************************** 1. row ***************************
Host: localhost
User: yongfu_a
Password: *1603BD6A73604A872220561294D3C5916A7B1E60
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
省略后面的
Host: localhost
User: yongfu_a
Password: *1603BD6A73604A872220561294D3C5916A7B1E60
Select_priv: Y
Insert_priv: Y
Update_priv: Y
Delete_priv: Y
mysql> REVOKE SELECT,UPDATE,INSERT,DELETE on *.* from yongfu_a@'localhost';
mysql> select * from user where User='yongfu_a' \G;
就可以看到,被取消了增删改查的权限了
REVOKE语句用于取消用户的权限,而不可以删除用户。
浙公网安备 33010602011771号