姚强 2020-06-02
? +-----------------------------------------------------------------------------------------------------------------------------------------------
----------------------------+
? | CREATE USER ‘root‘@‘localhost‘ IDENTIFIED WITH ‘mysql_native_password‘ AS
‘E74858DB86EBA20BC33D0AECAE8A8108C56B17FA‘ REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT
UNLOCK |
? +-----------------------------------------------------------------------------------------------------------------------------------------------
----------------------------+
MySQL授权用户
MySQL的授权用户由两部分组成:用户名和登录主机名
? 表达用户的语法为‘user_name’@‘host_name’
? 单引号不是必须,但如果其中包含特殊字符则是必须的
? ‘’@‘localhost’代表匿名登录的用户
? Host_name可以使主机名或者ipv4/ipv6的地址。Localhost代表本机,127.0.0.1代表ipv4的
本机地址,::1代表ipv6的本机地址
? Host_name字段允许使用%和_两个匹配字符,比如’%’代表所有主机,’%.mysql.com’代表
来自mysql.com这个域名下的所有主机,‘192.168.1.%’代表所有来自192.168.1网段的主机
MySQL修改权限的生效
? 执行Grant,revoke,set password,rename user命令修改权限之后,MySQL会自动
将修改后的权限信息同步加载到系统内存中
? 如果执行insert/update/delete操作上述的系统权限表之后,则必须再执行刷
新权限命令才能同步到系统内存中,刷新权限命令包括:flush
privileges/mysqladmin flush-privileges/mysqladmin reload
? 如果是修改tables和columns级别的权限,则客户端的下次操作新权限就会生
效
? 如果是修改database级别的权限,则新权限在客户端执行use database命令后
生效
? 如果是修改global级别的权限,则需要重新创建连接新权限才能生效
? --skip-grant-tables可以跳过所有系统权限表而允许所有用户登录,只在特殊
情况下暂时使用
MySQL用户连接
? shell> mysql -u db用户名 -p db密码
创建MySQL用户
? 有两种方式创建MySQL授权用户
? 执行create user/grant命令(推荐方式)
? 通过insert语句直接操作MySQL系统权限表
? mysql> CREATE USER ‘finley‘@‘localhost‘ IDENTIFIED BY ‘some_pass‘;
? mysql> GRANT ALL PRIVILEGES ON . TO ‘finley‘@‘localhost‘ WITH
GRANT OPTION;
? mysql> CREATE USER ‘finley‘@‘%‘ IDENTIFIED BY ‘some_pass‘;
? mysql> GRANT ALL PRIVILEGES ON . TO ‘finley‘@‘%‘ WITH GRANT
OPTION;
? mysql> CREATE USER ‘admin‘@‘localhost‘ IDENTIFIED BY ‘admin_pass‘;
? mysql> GRANT RELOAD,PROCESS ON . TO ‘admin‘@‘localhost‘;
? mysql> grant select(id) on test.temp to ;
创建MySQL用户
? mysql> SHOW GRANTS FOR ‘admin‘@‘localhost‘;
? +-----------------------------------------------------+
? | Grants for |
? +-----------------------------------------------------+
? | GRANT RELOAD, PROCESS ON .* TO ‘admin‘@‘localhost‘ |
? +-----------------------------------------------------+
? mysql> SHOW CREATE USER ‘admin‘@‘localhost‘\G
? 1. row
? CREATE USER for : CREATE USER ‘admin‘@‘localhost‘
? IDENTIFIED WITH ‘mysql_native_password‘
? AS ‘67ACDEBDAB923990001F0FFB017EB8ED41861105‘
? REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK
创建MySQL用户
? mysql> CREATE USER ‘custom‘@‘localhost‘ IDENTIFIED BY ‘obscure‘;
? mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
? -> ON bankaccount.
? -> TO ‘custom‘@‘localhost‘;
? mysql> CREATE USER ‘custom‘@‘host47.example.com‘ IDENTIFIED BY ‘obscure‘;
? mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
? -> ON expenses.
? -> TO ‘custom‘@‘host47.example.com‘;
? mysql> CREATE USER ‘custom‘@‘%.example.com‘ IDENTIFIED BY ‘obscure‘;
? mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
? -> ON customer.
? -> TO ‘custom‘@‘%.example.com‘;
回收MySQL用户权限
? 通过revoke命令收回用户权限
? mysql> show grants for ‘mysql.sys‘@localhost;
? +---------------------------------------------------------------+
? | Grants for |
? +---------------------------------------------------------------+
? | GRANT USAGE ON . TO ‘mysql.sys‘@‘localhost‘ |
? | GRANT TRIGGER ON sys
. TO ‘mysql.sys‘@‘localhost‘ |
? | GRANT SELECT ON sys
.sys_config
TO ‘mysql.sys‘@‘localhost‘ |
? mysql> revoke select on sys
.sys_config
from ‘mysql.sys‘@localhost;
? mysql> show grants for ‘mysql.sys‘@localhost;
? +-----------------------------------------------------+
? | Grants for |
? +-----------------------------------------------------+
? | GRANT USAGE ON . TO ‘mysql.sys‘@‘localhost‘ |
? | GRANT TRIGGER ON sys
. TO ‘mysql.sys‘@‘localhost‘ |
删除MySQL用户
? 通过执行drop user命令删除MySQL用户
? mysql> DROP USER ‘jeffrey‘@‘localhost‘;
? mysql> DROP USER ‘jeffrey‘@‘localhost‘;
设置MySQL用户资源限制
? 通过设置全局变量max_user_connections可以限制所有用户在同一时
间连接MySQL实例的数量,但此参数无法对每个用户区别对待,所以
MySQL提供了对每个用户的资源限制管理
? MAX_QUERIES_PER_HOUR:一个用户在一个小时内可以执行查询的次
数(基本包含所有语句)
? MAX_UPDATES_PER_HOUR:一个用户在一个小时内可以执行修改的次
数(仅包含修改数据库或表的语句)
? MAX_CONNECTIONS_PER_HOUR:一个用户在一个小时内可以连接
MySQL的时间
? MAX_USER_CONNECTIONS:一个用户可以在同一时间连接MySQL实例
的数量
? 从5.0.3版本开始,对用户‘user’@‘%.example.com’的资源限制是指所有
通过example.com域名主机连接user用户的连接,而不是分别指从
host1.example.com和host2.example.com主机过来的连接
设置MySQL用户资源限制
? 通过执行create user/alter user设置/修改用户的资源限制
? mysql> CREATE USER ‘francis‘@‘localhost‘ IDENTIFIED BY ‘frank‘
-> WITH MAX_QUERIES_PER_HOUR 20
-> MAX_UPDATES_PER_HOUR 10
-> MAX_CONNECTIONS_PER_HOUR 5
-> MAX_USER_CONNECTIONS 2;
? mysql> ALTER USER ‘francis‘@‘localhost‘ WITH MAX_QUERIES_PER_HOUR
100;
? 取消某项资源限制既是把原先的值修改成0
? mysql> ALTER USER ‘francis‘@‘localhost‘ WITH MAX_CONNECTIONS_PER_HOUR 0;
? 当针对某个用户的max_user_connections非0时,则忽略全局系统参数
max_user_connections,反之则全局系统参数生效
设置MySQL用户资源限制
? 通过执行create user/alter user设置/修改用户的资源限制
? mysql> CREATE USER ‘francis‘@‘localhost‘ IDENTIFIED BY ‘frank‘
-> WITH MAX_QUERIES_PER_HOUR 20
-> MAX_UPDATES_PER_HOUR 10
-> MAX_CONNECTIONS_PER_HOUR 5
-> MAX_USER_CONNECTIONS 2;
? mysql> ALTER USER ‘francis‘@‘localhost‘ WITH MAX_QUERIES_PER_HOUR
100;
? 取消某项资源限制既是把原先的值修改成0
? mysql> ALTER USER ‘francis‘@‘localhost‘ WITH MAX_CONNECTIONS_PER_HOUR 0;
? 当针对某个用户的max_user_connections非0时,则忽略全局系统参数
max_user_connections,反之则全局系统参数生效
设置MySQL用户的密码
? 执行create user创建用户和密码
? mysql> CREATE USER ‘jeffrey‘@‘localhost‘ IDENTIFIED BY ‘mypass‘;
? 修改用户密码的方式包括:
? mysql> ALTER USER ‘jeffrey‘@‘localhost‘ IDENTIFIED BY ‘mypass‘;
? mysql> SET PASSWORD FOR ‘jeffrey‘@‘localhost‘ = PASSWORD(‘mypass‘);
? mysql> GRANT USAGE ON . TO ‘jeffrey‘@‘localhost‘ IDENTIFIED BY ‘mypass‘;
? shell> mysqladmin -u user_name -h host_name password "new_password"
? 修改本身用户密码的方式包括:
? mysql> ALTER USER USER() IDENTIFIED BY ‘mypass‘;
? mysql> SET PASSWORD = PASSWORD(‘mypass‘);
设置MySQL用户密码过期策略
? 设置系统参数default_password_lifetime作用于所有的用户账户
? default_password_lifetime=180 设置180天过期
? default_password_lifetime=0 设置密码不过期
? 如果为每个用户设置了密码过期策略,则会覆盖上述系统参数
? ALTER USER ‘jeffrey‘@‘localhost‘ PASSWORD EXPIRE INTERVAL 90 DAY;
? ALTER USER ‘jeffrey’@‘localhost’ PASSWORD EXPIRE NEVER; 密码不过期
? ALTER USER ‘jeffrey’@‘localhost’ PASSWORD EXPIRE DEFAULT; 默认过期策略
? 手动强制某个用户密码过期
? ALTER USER ‘jeffrey‘@‘localhost‘ PASSWORD EXPIRE;
mysql> SELECT 1;
ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
mysql> ALTER USER USER() IDENTIFIED BY ‘new_password‘;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT 1;
| 1 |
MySQL用户lock
? 通过执行create user/alter user命令中带account lock/unlock子句设
置用户的lock状态
? Create user语句默认的用户是unlock状态
? mysql> create user identified by ‘mysql‘ account lock;
? Query OK, 0 rows affected (0.01 sec)
? Alter user语句默认不会修改用户的lock/unlock状态
? mysql> alter user ‘mysql.sys‘@localhost account lock;
? Query OK, 0 rows affected (0.00 sec)
? mysql> alter user ‘mysql.sys‘@localhost account unlock;
? Query OK, 0 rows affected (0.00 sec)
? 当客户端使用lock状态的用户登录MySQL时,会收到如此报错
Access denied for user ‘user_name‘@‘host_name‘.
Account is locked.