mysql -u root -p #查看帮助 help account management; topics: ALTER USER CREATE USER DROP USER GRANT RENAME USER REVOKE SET PASSWORD
#创建用户 CREATE USER 'username'@'host' IDENTIFIED BY '密码'; 可选项: 加密套件 CREATE USER 'username'@'host' IDENTIFIED WITH '加密套件' BY '密码'; 加密套件有mysql_native_password、caching_sha2_password
设置密码过期时间 WITH PASSWORD EXPIRE INTERVAL 90 DAY;
设置账户锁定 WITH ACCOUNT LOCK;
例: CREATE USER 'xhy'@'%' IDENTIFIED WITH 'mysql_native_password' BY 'Admin@123!' WITH PASSWORD EXPIRE INTERVAL 90 DAY,WITH ACCOUNT LOCK;
#删除用户 DROP USER 'user'@'localhost' 可选项: 删除多个 DROP USER 'user1'@'localhost', 'user2'@'localhost';
避免错误,使用IF EXISTS DROP USER IF EXISTS 'username'@'localhost';
#重命名用户 rename user 'newwangsheng'@'%' to 'wangsheng'@'%';
#用户授权 格式: GRANT 权限类型 ON 数据库名.表名 TO '用户名'@'登录主机' [IDENTIFIED BY '密码' [WITH GRANT OPTION]]; - 权限类型:可以是单个权限(如SELECT、INSERT、UPDATE、DELETE等)或多个权限的组合,甚至可以是ALL PRIVILEGES表示所有权限
例: 授予用户所有权限 GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%';
授予用户对特定数据库中的特定表的权限 GRANT SELECT, INSERT, UPDATE ON 数据库名.表名.列名 TO '用户名'@'%';
授予用户可以进行授权操作的权限 WITH GRANT OPTION
#权限刷新 flush privileges;
#授权总结案例 host可选:%,localhost,127.0.0.1,::1
1.授权普通用户具有查询插入更新删除wsdb的所有数据表的权限 grant select,insert,update,delete on wsdb.* to wangsheng@'%'; flush privileges; show grants for wangshen@'%';
2.开发人员授权具有创建索引、视图、表、存储过程等的权限 create user dev@'192.168.10.%' identified by 'Admin@123!'; grant create,alter,drop on wsdb1.* to dev@'%'; flush privileges; show grants for dev@'%';
3.操作mysql外键的权限 grant references on wsdb.* to dev@'%'; 4.创建mysql临时表的权限 grant create temportary tables on wsdb.* to dev@'%'; 5.操作mysql索引的权限 grant index on wsdb.* to dev@'%'; 6.操作视图的权限 grant create view,show view on wsdb.* to dev@'%'; 7.操作存储过程和函数的权限 grant create routine,alter routine,execute on wsdb.* to dev@'%'; 8.授权普通用户操作某个数据库 grant all privileges on wsdb.* to dev@'%'; 9.授权高级用户管理所有数据库 grant all privileges *.* to dev@'%'; 10.授权针对单个表 grant select on wsdb.table01 to dev@'%'; 11.授权针对单个列 grant select(列名1,列名2) on wsdb.table01 to dev@'%'; 12.授权针对函数存储过程和函数 grant execute on procedure 库名.过程名 to dev@'%'; grant execute on function 库名.函数名 to dev@'%';
权限回收
1 2 3 4 5
语法 revoke 权限列表 on 库.对象名 from '用户名'@'登陆位置';
和grant类似,只需要将to改为from就行 revoke
用户密码管理
修改用户密码 1.修改root密码 (1)mysqladmin 修改密码 mysqladmin -u -root -h localhost -p password "newpasswd"; (2)修改mysql.user表 user mysql; update mysql.user set authentication_string=PASSWORD('newpasswd') where user='root'; flush privileges; (3)使用set语句 set password=PASSWORD('newpasswd');
2.修改普通用户密码 (1)修改mysql.user表 user mysql; update mysql.user set authentication_string=PASSWORD('newpasswd') where user='wangsheng'; (2)使用grant语句 grant usage on *.* to 'wsdb'@'%' identified by 'newpasswd'; (3)当前用户修改自己的密码方法1 set password=PASSWORD('newpasswd'); (4)当前用户修改自己的密码方法2 alter user 'wsdb'@'%' identified by 'newpasswd'
用户过期问题 相关参数: show variables like 'default_password_lifetime'; 修改默认策略: (1)my.cnf参数 [mysqld] default_passwod_lifetime=0
(2)使用alter 默认90天 alter user 'wangsheng'@'localhost' password expire interval 90 day; 默认没有 alter user 'wangsheng'@'localhost' password expire interval never; 默认使用my.cnf的默认设置 alter user 'wangsheng'@'localhost' password expire interval default;
root用户密码丢失问题 1.windows系统 加入参数到my.ini --skip-grant-tables选项启动数据库 update mysql.user set authentication_string=PASSWORD('newpasswd') where user='root'; flush privileges; 修改完成后移除参数