MySQL启用SSL连接

1.手动创建自认证证书

1.1 创建CA证书

openssl genrsa 2048 > ca-key.pem
openssl req -new -x509 -nodes -days 3600 \
 -key ca-key.pem -out ca.pem 

1.2 创建服务端证书,去除加密,使用CA签名

    server-cert.pem = public key, server-key.pem = private key
openssl req -newkey rsa:2048 -days 3600 \
 -nodes -keyout server-key.pem -out server-req.pem
openssl rsa -in server-key.pem -out server-key.pem
openssl x509 -req -in server-req.pem -days 3600 \
 -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem

1.3 创建客户端证书,去除加密,使用CA签名

    client-cert.pem = public key, client-key.pem = private key
openssl req -newkey rsa:2048 -days 3600 \
 -nodes -keyout client-key.pem -out client-req.pem
openssl rsa -in client-key.pem -out client-key.pem
openssl x509 -req -in client-req.pem -days 3600 \
 -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem

1.4 证书生成后,进行校验:

openssl verify -CAfile ca.pem server-cert.pem client-cert.pem

2.修改my.cnf文件,mysqld下面增加如下配置项

[mysqld]
ssl-ca=/home/mysql/sslconfig/ca.pem
ssl-cert=/home/mysql/sslconfig/server-cert.pem
ssl-key=/home/mysql/sslconfig/server-key.pem

说明:pem配置文件中在test目录下有,但是最好自己生成
/home/mysql/mysql-5.5.35-linux2.6-x86_64/mysql-test/std_data

3.重启数据库

/etc/init.d/mysqld restart

4.登陆验证

4.1查看服务器变量have_ssl,是否支持SSL

SHOW VARIABLES LIKE 'have_%ssl';
SHOW VARIABLES LIKE '%ssl%';
如果为yes,表示服务端已经开启SSL
查看服务ssl等状态
SHOW STATUS LIKE 'Ssl_cipher';
STATUS或者\s
SSL: Cipher in use is DHE-RSA-AES256-SHA表示客户端已经 使用SSL连接

4.2客户端登陆

客户端无SSL登陆:

mysql -uroot -pzdh1234 或者 mysql -uroot -pzdh1234 --ssl=0

客户端开启SSL登陆:

mysql -uroot -hlocalhost -pzdh1234 --ssl-ca=/home/mysql/sslconfig/ca.pem

4.3创建用户强制使用SSL登陆:

GRANT ALL PRIVILEGES ON . TO 'ssluser'@'%' IDENTIFIED BY 'zdh1234' REQUIRE SSL;
本机登陆需要单独新增localhost
GRANT ALL PRIVILEGES ON . TO 'ssluser'@'localhost' IDENTIFIED BY 'zdh1234' REQUIRE SSL;
查看用户:
select host,user,password from mysql.user;
修改密码,需要重启mysql:

update user set password=password("zdh1234") where user="root" and host='zdh-11';

客户端无SSL登陆:

mysql -ussluser -pzdh1234

返回错误:ERROR 1045 (28000): Access denied for user 'ssluser'@'localhost' (using password: YES)
客户端开启SSL登陆:

mysql -ussluser -hlocalhost -pzdh1234 \
 --ssl-ca=/home/mysql/sslconfig/ca.pem

如果不填密码或者--ssl-ca都无法登陆

mysql -ussluser -hlocalhost -pzdh1234 \
 --ssl-cert=/home/mysql/sslconfig/client-cert.pem \
 --ssl-key=/home/mysql/sslconfig/client-key.pem
 
mysql -ussluser -hlocalhost -pzdh1234 \
 --ssl-ca=/home/mysql/sslconfig/ca.pem \
 --ssl-cert=/home/mysql/sslconfig/client-cert.pem \
 --ssl-key=/home/mysql/sslconfig/client-key.pem

4.4客户端连接时校验服务端认证:

由于创建server使用的服务器域名为zdh-11
校验失败,无法登陆:

mysql -uroot -hlocalhost -pzdh1234  --ssl-ca=/home/mysql/sslconfig/ca.pem --ssl-verify-server-cert

校验成功,可以登陆:

mysql -uroot -hzdh-11 -pzdh1234 --ssl-ca=/home/mysql/sslconfig/ca.pem --ssl-verify-server-cert

你可能感兴趣的:(MySQL启用SSL连接)