CentOS6.6 X86_64
使用脚本工具生成密钥文件
/usr/bin/mysql_ssl_rsa_setup
执行完成后,data目录下生成密钥文件
-rw-r--r--. 1 root root 451 11月 28 12:26 public_key.pem
-rw-------. 1 root root 1.7K 11月 28 12:26 private_key.pem
-rw-r--r--. 1 root root 1.1K 11月 28 12:26 client-cert.pem
-rw-------. 1 root root 1.7K 11月 28 12:26 client-key.pem
-rw-r--r--. 1 root root 1.1K 11月 28 12:26 server-cert.pem
-rw-------. 1 root root 1.7K 11月 28 12:26 server-key.pem
-rw-r--r--. 1 root root 1.1K 11月 28 12:26 ca.pem
-rw-------. 1 root root 1.7K 11月 28 12:26 ca-key.pem
生成密钥文件后注意权限,特别是root运行此脚本,mysql用户启动
chown -R mysql:mysql *.pem
文件说明:
ca.pem Self-signed CA certificate
ca-key.pem CA private key
server-cert.pem Server certificate
server-key.pem Server private key
client-cert.pem Client certificate
client-key.pem Client private key
启动时产生RSA密钥对
private_key.pem Private member of private/public key pair
public_key.pem Public member of private/public key pair
参考http://dev.mysql.com/doc/refman/5.7/en/creating-ssl-rsa-files-using-mysql.html
使用ssl连接http://dev.mysql.com/doc/refman/5.7/en/using-ssl-connections.html
客户端证书导出
# sz ca.pem
# sz client-cert.pem
# sz client-key.pem
添加ssl参数到my.cnf配置文件
[mysqld]
ssl-ca=/var/lib/mysql/ca.pem
ssl-cert=/var/lib/mysql/server-cert.pem
ssl-key=/var/lib/mysql/server-key.pem
#【关闭ssl】
#skip_ssl
[client]
ssl-ca=/var/lib/mysql/ca.pem
ssl-cert=/var/lib/mysql/client-cert.pem
ssl-key=/var/lib/mysql/client-key.pem
启动mysql server
bin/mysqld_safe &
注意观察日志文件,是否添加ssl参数后报错,ssl无法启动。
登入mysql服务器,查看have_ssl为YES即可。
mysql> show variables like '%ssl%';
+---------------+-----------------------------------+
| Variable_name | Value |
+---------------+-----------------------------------+
| have_openssl | YES |
| have_ssl | YES |
| ssl_ca | /opt/mysql01/data/ca.pem |
| ssl_capath | |
| ssl_cert | /opt/mysql01/data/server-cert.pem |
| ssl_cipher | |
| ssl_crl | |
| ssl_crlpath | |
| ssl_key | /opt/mysql01/data/server-key.pem |
+---------------+-----------------------------------+
9 rows in set (0.00 sec)
客户端连接
bin/mysql -uroot -h 192.168.31.84 -p
(最好使用远程连接进行测试,localhost或者-S unix socket连接,这种有可能不会用ssl。)
mysql> status
--------------
bin/mysql Ver 14.14 Distrib 5.7.9, for Linux (x86_64) using EditLine wrapper
Connection id: 10
Current database:
Current user: [email protected]
SSL: Cipher in use is DHE-RSA-AES256-SHA
Current pager: stdout
Using outfile: ''
Using delimiter: ;
status中SSL中显示Cipher in use,表明当前连接使用ssl
或者查看状态Ssl_cipher也可以,Value不为空,表明客户端连接启用ssl
mysql> show status like 'ssl_cipher';
+---------------+--------------------+
| Variable_name | Value |
+---------------+--------------------+
| Ssl_cipher | DHE-RSA-AES256-SHA |
+---------------+--------------------+
1 row in set (0.00 sec)
如果客户不想使用ssl连接,可以在mysql连接参数中使用-ssl=0来禁用ssl连接,执行效果请自行使用status查看
bin/mysql -uroot -h 192.168.31.84 --ssl=0 -p
如果服务器不强制要求用户使用ssl连接,则用户可以使用普通方式,也可以使用ssl方式连接服务器。某些安全场合,可以限制用户必须ssl登录。
GRANT ALL PRIVILEGES ON *.* TO 'ssltest'@'%' IDENTIFIED BY 'foodsafe' REQUIRE SSL;
FLUSH PRIVILEGES;
创建ssltest测试用户,密码为foodsafe,必须ssl登录。
测试
[root@vtfsdb1 mysql01]# bin/mysql -u ssltest -h 192.168.31.84 --ssl=0 -p
Enter password:
ERROR 1045 (28000): Access denied for user 'ssltest'@'192.168.31.84' (using password: YES)
如果不使用ssl登录,尽管密码正确,也会提示Access denied。
系统权限表mysql.user中,表现为ssl_type有值,如果不要求则为空。
alter user 'ssltest'@'%' require none;
mysql> select user,ssl_type,ssl_cipher from mysql.user where user='ssltest';
+---------+----------+------------+
| user | ssl_type | ssl_cipher |
+---------+----------+------------+
| ssltest | | |
+---------+----------+------------+
1 row in set (0.00 sec)
alter user 'ssltest'@'%' require ssl;
mysql> select user,ssl_type,ssl_cipher from mysql.user where user='ssltest';
+---------+----------+------------+
| user | ssl_type | ssl_cipher |
+---------+----------+------------+
| ssltest | ANY | |
+---------+----------+------------+
1 row in set (0.00 sec)
修改完成记得flush privileges;
【故障】:Mysql 启动报错 “mysqld.service holdoff time over, scheduling restart”
【解决】:# chown mysql:mysql -R /var/lib/mysql
# service mysqld restart
【故障】: yum install -y cyrus-sasl-sql.x86_64
报错需要:libmysqlclient.so.18(libmysqlclient_18)(64bit)
【解决】:rpm -ivh /home/mysql/mysql-community-libs-compat-5.7.22-1.el7.x86_64.rpm
yum install -y cyrus-sasl-sql.x86_64