MySQL 用户权限管理主要有以下作用:
1. 可以限制用户访问哪些库、哪些表
2. 可以限制用户对哪些表执行 SELECT、CREATE、DELETE、DELETE、ALTER 等操作
3. 可以限制用户登录的 IP 或域名
4. 可以限制用户自己的权限是否可以授权给别的用户
- mysql> grant all privileges on *.* to 'yangxin'@'%' identified by 'yangxin123456' with grant option;
用户详情的权限列表请参考 MySQL 官网说明:http://dev.mysql.com/doc/refman/5.7/en/privileges-provided.html
对用户做了权限变更之后,一定记得重新加载一下权限,将权限信息从内存中写入数据库。
- mysql> flush privileges;
- mysql > grant select,
- create,
- drop,
- update,
- alter on * . * to 'yangxin'@'localhost'identified by 'yangxin0917'with grant option;
- mysql > show grants
- for 'yangxin'@'localhost';
删除 yangxin 这个用户的 create 权限,该用户将不能创建数据库和表。
- mysql> revoke create on *.* from 'yangxin@localhost';
- mysql> flush privileges;
- mysql> select host,user from user;
- +---------------+---------+
- | host | user |
- +---------------+---------+
- | % | root |
- | % | test3 |
- | % | yx |
- | 192.168.0.% | root |
- | 192.168.0.% | test2 |
- | 192.168.0.109 | test |
- | ::1 | yangxin |
- | localhost | yangxin |
- +---------------+---------+
- 8 rows in set (0.00 sec)
- mysql> drop user 'yangxin'@'localhost';
- shell> rename user 'test3'@'%' to 'test1'@'%';
- mysql> use mysql;
- # mysql5.7之前
- mysql> update user set password=password('123456') where user='root';
- # mysql5.7之后
- mysql> update user set authentication_string=password('123456') where user='root';
- mysql> flush privileges;
语法:set password for '用户名'@'登录地址'=password('密码')
- mysql > set password
- for 'root'@'localhost' = password('123456');
语法:mysqladmin -u 用户名 -p 旧的密码 password 新密码
- mysql> mysqladmin -uroot -p123456 password 1234abcd
注意:mysqladmin 位于 mysql 安装目录的 bin 目录下
修改 my.cnf,在 mysqld 配置节点添加 skip-grant-tables 配置
- [mysqld]
- skip-grant-tables
- shell> service mysqld restart
此时在终端用 mysql 命令登录时不需要用户密码,然后按照修改密码的第一种方式将密码修改即可。
注意:mysql 库的 user 表,5.7 以下版本密码字段为 password,5.7 以上版本密码字段为 authentication_string
将 my.cnf 中 mysqld 节点的 skip-grant-tables 配置删除,然后重新启动服务即可。
来源: