mysql5.7系列教程(四)安全

密码安全

  1. mysql -u root -p db_name
    Enter password: ********

2.配置文件中保存密码
you can list your password in the [client] section of the .my.cnf file in your home directory:
[client]
password=your_pass
为了保证密码的安全,除了你以外,任何人都不能访问这个文件。要确保这一点,请将文件访问模式设置为400或600。例如:
chmod 600 .my.cnf
登录方式:
mysql --defaults-file=/etc/mysqlps.cnf
在这里插入图片描述
3. mysql_config_editor会保存认证信息到一个加密的登陆文件(.mylogin.cnf)
【推荐只用此方法】

shell> mysql_config_editor [program_options] command [command_options]
例:
shell> mysql_config_editor set --host=localhost --user=root --password
Enter password: enter password "localpass" here
[不能粘贴]
【--login-path=client (默认)】
查看帮助
shell> mysql_config_editor --help set
shell> mysql_config_editor set --help
显示内容:
shell> mysql_config_editor print --all
删除:
shell> mysql_config_editor remove --login-path=client --user

之后就可以直接登录了
在这里插入图片描述
查看密码安全策略:

SHOW VARIABLES LIKE 'validate_password%';

在这里插入图片描述

1).validate_password_dictionary_file 指定密码验证的文件路径;
2).validate_password_length  密码最小长度
3).validate_password_mixed_case_count  密码至少要包含的小写字母个数和大写字母个数;
4).validate_password_number_count  密码至少要包含的数字个数
5).validate_password_policy 密码强度检查等级,对应等级为:0/LOW、1/MEDIUM、2/STRONG,默认为1
0/LOW:只检查长度;
1/MEDIUM:检查长度、数字、大小写、特殊字符;
2/STRONG:检查长度、数字、大小写、特殊字符字典文件。
6).validate_password_special_char_count密码至少要包含的特殊字符数
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
报错原因:密码强度不够。
解决方法:(该账号为测试账号,所以采用降低密码策略强度)
set global validate_password_policy=0;
set global validate_password_length=6;
去除IP访问限制
grant all privileges on *.* to root@'%' identified by 'MyNewPass4!';//让所有IP都可以访问
flush privileges;

linux6防火墙设置
#vi /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
service  iptables  restart
linux7防火墙设置
firewall-cmd --zone=public --add-port=3306/tcp6 --permanent
firewall-cmd –reload
firewall-cmd --zone=public --list-ports

密码过期
在这里插入图片描述

ALTER USER 'script'@'localhost' PASSWORD EXPIRE NEVER;
•	要建立密码长达六个月的全局策略,请在服务器my.cnf文件中使用这些行启动服务器:
•	的[mysqld]
•	default_password_lifetime = 180
•	要建立密码永不过期的全局策略,设置default_password_lifetime 为0:
•	的[mysqld]
•	default_password_lifetime = 0
•	default_password_lifetime 也可以在运行时更改:
SET GLOBAL default_password_lifetime = 180;
SET GLOBAL default_password_lifetime = 0;
Example account-specific expiration statements:
•	Require the password to be changed every 90 days:
•	CREATE USER 'jeffrey'@'localhost' PASSWORD EXPIRE INTERVAL 90 DAY;
•	ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE INTERVAL 90 DAY;
•	Disable password expiration:
•	CREATE USER 'jeffrey'@'localhost' PASSWORD EXPIRE NEVER;
•	ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE NEVER;
•	Defer to the global expiration policy:
•	CREATE USER 'jeffrey'@'localhost' PASSWORD EXPIRE DEFAULT;
ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE DEFAULT;

加密连接
加密连接的服务器端配置
要启用服务器进行加密连接,请使用my.cnf 文件中的这些行来启动它,必要时更改文件名【加密文件存放在数据库目录 默认: /var/lib/mysql】:

[mysqld]
ssl-ca=ca.pem
ssl-cert=server-cert.pem
ssl-key=server-key.pem
•	--ssl-ca 标识证书颁发机构(CA)证书。
•	--ssl-cert标识服务器公钥证书。这可以发送到客户端,并根据其拥有的CA证书进行身份验证。
•	--ssl-key 标识服务器私钥。
加密连接的客户端配置
默认情况下,如果服务器支持加密连接,MySQL客户端程序将尝试建立加密连接,并通过以下--ssl-mode选项进一步控制 :
•	在没有--ssl-mode选项的情况下 ,客户端尝试使用加密进行连接,如果无法建立加密连接,则返回到未加密的连接。这也是一个显式--ssl-mode=PREFFERED选项的行为 。
•	随着--ssl-mode=REQUIRED客户端需要加密连接,如果无法建立,则会失败。
•	有了--ssl-mode=DISABLED,客户端使用未加密的连接。
为了额外的安全性,客户端上的以下选项标识客户端在建立到服务器的加密连接时使用的证书和密钥文件。它们类似于在服务器端使用的选项,但 --ssl-cert并 --ssl-key识别客户端公钥和私钥:
•	--ssl-ca标识证书颁发机构(CA)证书。此选项(如果使用)必须指定服务器使用的相同证书。
•	--ssl-cert 标识客户端公钥证书。
•	--ssl-key 标识客户端私钥。
如果服务器支持加密连接,建立加密连接,如果无法建立加密连接,则返回到未加密的连接
mysql --ssl-mode=PREFERRED
如果无法建立加密连接,则连接尝试失败
mysql --ssl-mode=REQUIRED
建立未加密的连接。这就像旧 --ssl=0选项或其同义词(--skip-ssl, --disable-ssl)。
mysql --ssl-mode=DISABLED
加密连接例子:
mysql mysql --ssl-mode=REQUIRED -uroot -p'MyNewPass4!'

查看是否支持加密连接:
SHOW VARIABLES LIKE 'have_ssl';
If the value is YES, the server supports encrypted connections. If the value is DISABLED, the server is capable of supporting encrypted connections but was not started with the appropriate --ssl-xxx options to enable encrypted connections to be used;

要确定当前与服务器的连接是否使用加密,请检查Ssl_cipher状态变量的值 。如果该值为空,则该连接未加密。否则,连接被加密,该值表示加密密码。例如:
SHOW SESSION STATUS LIKE 'Ssl_version';
SHOW SESSION STATUS LIKE 'Ssl_cipher';
For the mysql client, an alternative is to use the STATUS or \s command and check the SSL line:
mysql> \s
...
SSL: Not in use
...
Or:
mysql> \s
...
SSL: Cipher in use is DHE-RSA-AES128-GCM-SHA256

在这里插入图片描述
其他参数
在这里插入图片描述
创建加密连接用户
To require use of encrypted connections by a MySQL account, use CREATE USER to create the account with a REQUIRE SSL clause, or use ALTER USER for an existing account to add a REQUIRE SSL clause. Connection attempts by clients that use the account will be rejected unless MySQL supports encrypted connections and an encrypted connection can be established.
创建SSL和RSA证书和密钥
1.使用系统自带 【mysql_ssl_rsa_setup】

shell> mysql_ssl_rsa_setup [options]

在这里插入图片描述
2. 用openssl创建
参考相关资料
要查看SSL证书的内容

openssl x509-text -in ca.pem
openssl x509 -text -in server-cert.pem
openssl x509 -text -in client-cert.pem
or
mysql> SHOW STATUS LIKE 'Ssl_server_not%';

用户管理

MySQL帐户名由用户名和主机名组成。这样可以为具有相同名称的用户创建可从不同主机连接的帐户。
MySQL将帐户存储在系统数据库的user表中 mysql。根据用户名和用户可以从其连接到服务器的客户端主机或主机来定义帐户
创建用户
After connecting to the server as root, you can add new accounts. The following example uses CREATE USER andGRANT statements to set up four accounts:

mysql> CREATE USER 'finley'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'finley'@'localhost'
    ->     WITH GRANT OPTION;
mysql> CREATE USER 'finley'@'%' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'finley'@'%'
    ->     WITH GRANT OPTION;
mysql> CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT RELOAD,PROCESS ON *.* TO 'admin'@'localhost';
mysql> CREATE USER 'dummy'@'localhost';
实例
mysql> CREATE USER 'custom'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
    ->     ON bankaccount.*
    ->     TO 'custom'@'localhost';
mysql> CREATE USER 'custom'@'host47.example.com' IDENTIFIED BY 'password';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
    ->     ON expenses.*
    ->     TO 'custom'@'host47.example.com';
mysql> CREATE USER 'custom'@'%.example.com' IDENTIFIED BY 'password';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
    ->     ON customer.*
    ->     TO 'custom'@'%.example.com';

•	第一个帐户可以访问 bankaccount数据库,但只能从本地主机访问。
•	第二个帐户可以访问expenses 数据库,但只能从主机访问 host47.example.com。
•	第三个帐户可以customer 从example.com 域中的任何主机访问数据库。由于使用%帐户名称的主机部分中的通配符,该帐户可以访问域中所有计算机。
删除用户:
DROP USER 'jeffrey'@'localhost';
帐户资源限制
•	帐户每小时可以发出的查询数量
•	帐户每小时可更新的次数
•	帐户可以每小时连接到服务器的次数
•	一个帐户同时连接到服务器的数量
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;
要修改现有帐户的限制,请使用 ALTER USER语句
ALTER USER 'francis'@'localhost' WITH MAX_QUERIES_PER_HOUR 100;
要删除限制,请将其值设置为零。
ALTER USER 'francis'@'localhost' WITH MAX_CONNECTIONS_PER_HOUR 0;



4. 修改用户密码
确定mysql.user表中存在哪些帐户 以及它们的密码是否为空:
•	从MySQL 5.7.6起,使用以下语句:
•	MySQL的> SELECT User, Host, HEX(authentication_string) FROM mysql.user;
•	该语句使用,HEX() 因为存储在authentication_string列中的密码 可能包含不能很好地显示的二进制数据。
•	在MySQL 5.7.6之前,使用这个语句:
•	MySQL的> SELECT User, Host, Password FROM mysql.user;
修改密码:
•	从MySQL 5.7.6起,使用ALTER USER:
•	MySQL的> ALTER USER user IDENTIFIED BY 'new_password';
•	5.7.6之前,请使用SET PASSWORD:
•	MySQL的> SET PASSWORD FOR user = PASSWORD('new_password');

============  忘记root密码  ================
mysqld  --verbose --help |grep skip      //查询帮助
【进入/etc/my.cnf配置文件把 validate_password=off参数注释掉。(如果有的话)】
# systemctl stop mysqld //停止服务
systemctl  set-environment MYSQLD_OPTS="--skip-grant-tables" //跳过授权表
systemctl start mysqld //启动数据库
mysql -u root 
直接进行登陆
mysql>
# skip-grant-tables 模式下不能使用  SET or alter
update mysql.user set authentication_string=password('MyMysqlPS2017!') where user='root' and host='localhost';
flush PRIVILEGES;//刷新权限
systemctl unset-environment MYSQLD_OPTS //去掉设置的配置
systemctl restart mysqld
mysql -uroot -p'MyNewPass4!'

问题:1045 access denied for user 'root'@'localhost' using password yes
使用上面的方式修改root密码,重新登录。


权限管理

在这里插入图片描述
在这里插入图片描述

授权表
•	user:用户帐户,全局权限和其他非特权列
•	db:数据库级特权
•	tables_priv:表级特权
•	columns_priv:列级权限
•	procs_priv:存储过程和功能特权
•	proxies_priv:代理用户权限
授权生效
When mysqld starts, it reads all grant table contents into memory. The in-memory tables become effective for access control at that point.

If you modify the grant tables indirectly using account-management statements such as GRANT, REVOKE, SET PASSWORD, or RENAME USER, the server notices these changes and loads the grant tables into memory again immediately.

If you modify the grant tables directly using statements such as INSERT, UPDATE, or DELETE, your changes have no effect on privilege checking until you either restart the server or tell it to reload the tables. If you change the grant tables directly but forget to reload them, your changes have no effect until you restart the server. This may leave you wondering why your changes seem to make no difference!

To tell the server to reload the grant tables, perform a flush-privileges operation. This can be done by issuing aFLUSH PRIVILEGES statement or by executing a mysqladmin flush-privileges or mysqladmin reload command.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值