1、作用
2、示例
#格式'用户'@'白名单'#本地用户'user'@'localhost'#单一IP'user'@'192.168.10.240'#范围IP'user'@'192.168.10.2%''user'@'192.168.10.%''user'@'192.168.10.240/255.255.255.0''user'@'%'#%等同于于*,表示所有
用户安全规范
1. 白名单尽量小,最好细化到单一IP,尽量不要使用%2. 用户名要有特点3. 无用的用户删除或者锁定4. 密码复杂度#不常用'user'@'%''user'@'db02''user'@'mingdikeji.com'
3、用户管理
3.1、查询用户
#查询用户,白名单,密码,插件select User,Host,authentication_string,plugin from mysql.user;#查询user表定义desc mysql.user;
3.2、创建用户
create user 'laidenglin'@'192.168.10.240' identified by '123456';flush privileges;#指定密码插件为:mysql_native_passwordcreate user 'laidenglin'@'192.168.10.240' identified with mysql_native_password by '123456';flush privileges;
MySQL 8.0 版本用户彩蛋
1. 必须先创建用户,再授权,不再支持grant创建用户和修改密码功能不再支持: grant all on *.* to 'zabbix'@'%' identified by '123456';2. 密码插件8.0之前: mysql_native_password8.0开始: caching_sha2_password#caching_sha2_password 导致的问题老版本的客户端程序,连接不了8.0版本例如: 客户端软件、主从、MHA、#解决方法(1). 建用户时,指定mysql_native_password插件进行密码加密create user 'laidenglin'@'192.168.10.240' identified with mysql_native_password by '123456';(2). 修改用户密码插件alter user 'laidenglin'@'192.168.10.240' identified with mysql_native_password;(3). 配置文件中指定默认加密插件为mysql_native_password[mysqld]default_authentication_plugin=mysql_native_password
3.3、修改用户
#修改密码alter user 'laidenglin'@'192.168.10.240' identified by '456789';flush privileges;#5.6版本修改密码update mysql.user set password=password("123") where user="root";flush privileges;#修改插件alter user 'laidenglin'@'192.168.10.240' identified with mysql_native_password;flush privileges;#修改密码和插件alter user 'laidenglin'@'192.168.10.240' identified with mysql_native_password by '654321';flush privileges;#修改白名单update mysql.user set host='127.0.0.1' where user='laidenglin' and host='192.168.10.240';flush privileges;
3.4、锁定用户
#锁定用户alter user 'laidenglin'@'192.168.10.240' account lock;flush privileges;#锁定用户登录提示 (错误码: 3118)ERROR 3118 (HY000): Access denied for user 'laidenglin'@'localhost'. Account is locked.#解锁锁定用户alter user 'laidenglin'@'192.168.10.240' account unlock;flush privileges;#查看锁定用户select user,host,account_locked from mysql.user where account_locked = 'Y';
3.5、密码过期
不常用
alter user 'laidenglin'@'192.168.10.240' password expire;flush privileges;#可以使用旧密码连接,不能运行命令,提示修改密码 (错误码: 1082)ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
3.6、删除用户
生产环境中,先备份mysql库,或备份mysql.user表,再执行删除用户
drop user 'laidenglin'@'192.168.10.240';flush privileges;
