Extreme Thinking
MYSQL SSL

2017-07-11


show variables like '%ssl%';

Create 憑證

mkdir /etc/newcerts
cd /etc/newcerts
# CA key
openssl genrsa 2048 > ca-key.pem
openssl req -new -x509 -nodes -days 3600 -key ca-key.pem -out ca-cert.pem
# server 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-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
# client 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-cert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
# check key ok
openssl verify -CAfile ca-cert.pem server-cert.pem client-cert.pem

KEY info 不可都一樣,不然會在連線時發生

ERROR 2026 (HY000): SSL connection error

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

KEY info 一樣 會出現 ….

error 18 at 0 depth lookup:self signed certificate

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

正常會出現

server-cert.pem: OK
client-cert.pem: OK

編輯 /etc/my.cnf

[mysqld]
ssl-ca=/etc/newcerts/ca-cert.pem
ssl-cert=/etc/newcerts/server-cert.pem
ssl-key=/etc/newcerts/server-key.pem

在看一次 SSL

mysql -u root -p -e "show variables like '%ssl%';"

+---------------+-------------------------------+
| Variable_name | Value |
+---------------+-------------------------------+
| have_openssl | YES |
| have_ssl | YES |
| ssl_ca | /etc/newcerts/ca-cert.pem |
| ssl_capath | |
| ssl_cert | /etc/newcerts/server-cert.pem |
| ssl_cipher | DHE-RSA-AES256-SHA |
| ssl_key | /etc/newcerts/server-key.pem |
+---------------+-------------------------------+

Server 端設定 Client 授權改為

grant all on *.* to 'cross'@'192.168.0.17' identified by '123456'  require ssl;

Client 端

從 server 端產生的 ca-cert 及 client-cert 與 client-key 放到 client 端

編輯 /etc/my.cnf

[client]
ssl-ca=/etc/newcerts/ca-cert.pem
ssl-cert=/etc/newcerts/client-cert.pem
ssl-key=/etc/newcerts/client-key.pem

測試

mysql -u cross -p123456 -h 192.168.0.14 -e 'status'

出現 :

SSL:                    Cipher in use is DHE-RSA-AES256-SHA

不設定 my.cnf 可直接連用 :

mysql --ssl-ca=/etc/newcerts/ca-cert.pem --ssl-cert=/etc/newcerts/client-cert.pem --ssl-key=/etc/newcerts/client-key.pem -u cross -p123456 -h 192.168.0.14 -e 'status'