MYSQL8用户权限配置详解

        单位的系统性能问题需要把Mysql5升级到Mysql8,需要用到Mysql8的一些特性来提升系统的性能。

        配置用户权限过程中发现一些问题,学习并记录一下。

目录

一、环境

二、MySQL8 用户权限

2.1 账号管理权限

2.1.1 连接数据库

2.1.2 账号权限配置

2.2 密码管理

2.3 锁账号配置(含示例)

三、MySQL8 用户资源限制


一、环境

# 下载镜像
$ docker pull mysql:8.0.28

# 创建容器并运行
$ docker run -itd --name mysql8 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST='%' mysql

二、MySQL8 用户权限

        在 MySQL8 之前,授权表使用 MyISAM 并且是非事务性的,而在 MySQL8 中,授权表使用 InnoDB 存储引擎并且是事务性的。通过查看创建表的语句(命令:show create table user;)来查看表的存储引擎,如下图:

        服务器在启动时将授权表的内容读入内存,当修改权限时需要通过命令 FLUSH PRIVILEGES 重新加载使权限生效。

        在mysql8中有如下权限相关表:

user:用户帐户、静态全局权限表;
global_grants:动态全局权限表;
db:数据库级的权限表;
tables_priv:存储表级权限;
columns_priv: 存储列级权限;
procs_priv: 存储过程和函数权限表;
proxies_priv: 代理用户权限表;
default_roles:默认用户角色表;
role_edges:记录角色与用户的授权关系表;
password_history: 密码更改历史表。

        本文只是针对一般的开发者为满足开发和部署一般系统需要配置用户权限信息,基本上只涉及到user表。        

2.1 账号管理权限

2.1.1 连接数据库

  • 短命令
docker exec -it mysql3307 bash    // 回车
mysql -uroot -p //回车再输入密码

    如下图所示:

  • 长命令
docker exec -it mysql3307 bash    // 回车
mysql --user=root --password    // 回车,再输入密码

    如下图所示: 

2.1.2 账号权限配置

  • 创建和删除帐户
CREATE USER 和 DROP USER 创建和删除帐户;
  • 分配权限和撤销权限
GRANT 和 REVOKE 分配权限和撤销权限;

示例:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';  // 创建局域网络账号
GRANT ALL ON *.* TO 'username'@'localhost' WITH GRANT OPTION; // 分配权限
REVOKE ALL ON *.* FROM 'username'@'localhost';                // 撤销权限
SHOW GRANTS FOR 'username'@'localhost';                       // 查看权限
DROP USER 'username'@'localhost';                             // 删除账号

    注:其它机器用户访问的用户权限配置只需要修改localhost,如 localhost修改为% 表示任意机器网络,localhost修改为192.168.2.% 表示给192.168.2这个ip连接数据库配的用户权限。

2.2 密码管理

  • 修改密码
ALTER USER 'username'@'%' IDENTIFIED BY 'password';//修改密码
  • 设置密码过期时间
ALTER USER 'username'@'%' PASSWORD EXPIRE;                 //设置立即过期
ALTER USER 'username'@'%' PASSWORD EXPIRE INTERVAL 30 DAY; //设置30天过期
ALTER USER 'username'@'%' PASSWORD EXPIRE NEVER;           //禁用密码过期
  •  禁止重复使用最近3个或超过30天的密码

        修改文件 my.cnf,mysql8.0.28是/etc/my.cnf:

[mysqld]
password_history=3
password_reuse_interval=30

2.3 锁账号配置(含示例)

# 连续登录失败3次则锁定1天,天数可取值:0-32767,设置 0 则代表解锁
CREATE USER 'test'@'localhost' IDENTIFIED BY 'test123' FAILED_LOGIN_ATTEMPTS 7 PASSWORD_LOCK_TIME 1;

# 连续登录失败3次则永久锁定
ALTER USER 'try8'@'localhost' FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LOCK_TIME UNBOUNDED;

示例:

# 登陆MYSQL
PS C:\Users\Administrator> docker exec -it mysql8 bash
bash-4.4# mysql -uroot -p
Enter password:                                    # 此外输入密码完成登陆
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.2.0 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
# 登陆连续登陆失败3次锁一天的用户test
mysql> CREATE USER 'test'@'localhost' IDENTIFIED BY 'test123' FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LOCK_TIME 1;
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
# 打开另一终端,用正确密码测试是否登陆成功
PS C:\Users\Administrator> docker exec -it mysql8 bash
bash-4.4# mysql -utest -ptest123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.2.0 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>                                # 进到这里说明登陆成功
mysql> exit
Bye
# 测试登陆3次失败
bash-4.4# mysql -utest -ptest12345
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'test'@'localhost' (using password: YES)
bash-4.4# mysql -utest -ptest12345
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'test'@'localhost' (using password: YES)
bash-4.4# mysql -utest -ptest12345
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 3955 (HY000): Access denied for user 'test'@'localhost'. Account is blocked for 1 day(s) (1 day(s) remaining) due to 3 consecutive failed logins.
bash-4.4#

        第三次输入失败后有提示  Account is blocked for 1 day(s) (1 day(s) remaining) 账号已被锁1天。

# 输入正确的用户名密码登陆看是否成功
bash-4.4# mysql -utest -ptest123
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 3955 (HY000): Access denied for user 'test'@'localhost'. Account is blocked for 1 day(s) (1 day(s) remaining) due to 3 consecutive failed logins.

        因输入错误3次被锁定,即使再输入正确的用户名和密码也无法登陆。

  • 解锁
ALTER USER 'test'@'localhost' IDENTIFIED BY 'test123' FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LOCK_TIME 0;

        设置0:代表解锁。

三、MySQL8 用户资源限制

        将全局系统变量 max_user_connections 设置为非零值,可以限制同时建立的连接数,但是对客户端连接后可以执行的查询更新操作没有限制。客户端可以发出的任何语句都计入查询限制,只有修改库表才计入更新限制。

  • MAX_QUERIES_PER_HOUR:客户端每小时可以发出的查询数;
  • MAX_UPDATES_PER_HOUR:客户端每小时可以发出的更新数;
  • MAX_CONNECTIONS_PER_HOUR:客户端每小时可以连接到服务器的次数;
  • MAX_USER_CONNECTIONS:客户端同时连接的服务器数量等。

通过创建用户账号限制示例:

CREATE USER 'test'@'localhost' IDENTIFIED BY 'test123'
  WITH MAX_QUERIES_PER_HOUR 10000 
  MAX_UPDATES_PER_HOUR 10000 
  MAX_CONNECTIONS_PER_HOUR 10000 
  MAX_USER_CONNECTIONS 10000 ; # 客户端每小时的查询数、更新数、连接服务器的次数和数量为10000 。

通过修改和删除账号限制示例:

ALTER USER 'test'@'localhost' WITH MAX_QUERIES_PER_HOUR 10000; //修改限制
ALTER USER 'test'@'localhost' WITH MAX_CONNECTIONS_PER_HOUR 0; //设置0,为删除限制

猜你喜欢

转载自blog.csdn.net/imwucx/article/details/134804312