摄影网站的制作,做导航网站怎么赚钱,自己做网站 空间怎么买,记事本代码做网站单位的系统性能问题需要把Mysql5升级到Mysql8#xff0c;需要用到Mysql8的一些特性来提升系统的性能。 配置用户权限过程中发现一些问题#xff0c;学习并记录一下。 目录
一、环境
二、MySQL8 用户权限
2.1 账号管理权限
2.1.1 连接数据库
2.1.2 账号权限配置
2.2 密码… 单位的系统性能问题需要把Mysql5升级到Mysql8需要用到Mysql8的一些特性来提升系统的性能。 配置用户权限过程中发现一些问题学习并记录一下。 目录
一、环境
二、MySQL8 用户权限
2.1 账号管理权限
2.1.1 连接数据库
2.1.2 账号权限配置
2.2 密码管理
2.3 锁账号配置含示例
三、MySQL8 用户资源限制 一、环境
Windows11Docker 中文下载Docker中文网 官网英文下载:Get Started | Docker安装一直下一步即可Mysql5、Mysql8.0.28。
# 下载镜像
$ docker pull mysql:8.0.28# 创建容器并运行
$ docker run -itd --name mysql8 -p 3307:3306 -e MYSQL_ROOT_PASSWORDroot -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 --userroot --password // 回车再输入密码 如下图所示 2.1.2 账号权限配置
创建和删除帐户
CREATE USER 和 DROP USER 创建和删除帐户
分配权限和撤销权限
GRANT 和 REVOKE 分配权限和撤销权限
示例
CREATE USER usernamelocalhost IDENTIFIED BY password; // 创建局域网络账号
GRANT ALL ON *.* TO usernamelocalhost WITH GRANT OPTION; // 分配权限
REVOKE ALL ON *.* FROM usernamelocalhost; // 撤销权限
SHOW GRANTS FOR usernamelocalhost; // 查看权限
DROP USER usernamelocalhost; // 删除账号 注其它机器用户访问的用户权限配置只需要修改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.cnfmysql8.0.28是/etc/my.cnf
[mysqld]
password_history3
password_reuse_interval30
2.3 锁账号配置含示例
# 连续登录失败3次则锁定1天天数可取值0-32767设置 0 则代表解锁
CREATE USER testlocalhost IDENTIFIED BY test123 FAILED_LOGIN_ATTEMPTS 7 PASSWORD_LOCK_TIME 1;# 连续登录失败3次则永久锁定
ALTER USER try8localhost 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 testlocalhost 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 testlocalhost (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 testlocalhost (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 testlocalhost. 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 testlocalhost. Account is blocked for 1 day(s) (1 day(s) remaining) due to 3 consecutive failed logins. 因输入错误3次被锁定即使再输入正确的用户名和密码也无法登陆。
解锁
ALTER USER testlocalhost 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 testlocalhost IDENTIFIED BY test123WITH MAX_QUERIES_PER_HOUR 10000 MAX_UPDATES_PER_HOUR 10000 MAX_CONNECTIONS_PER_HOUR 10000 MAX_USER_CONNECTIONS 10000 ; # 客户端每小时的查询数、更新数、连接服务器的次数和数量为10000 。
通过修改和删除账号限制示例
ALTER USER testlocalhost WITH MAX_QUERIES_PER_HOUR 10000; //修改限制
ALTER USER testlocalhost WITH MAX_CONNECTIONS_PER_HOUR 0; //设置0为删除限制