当然,启用MySQL SSL之后,由于每个数据包都需要加密和解密,这个对MySQL的性能是有不小影响的,读者们在使用的时候,要根据实际情况斟酌。
MySQL客户端登录服务器时候的密码不是明文传输,有加密策略处理。
笔者是在 ubuntu12.04 系统上使用MySQL 5.5版本测试的,其他环境请读者自行匹配。
配置MySQL服务器证书
编辑 /etc/mysql/my.cnf 文件
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem
把上面三行默认证书配置注释打开,使用自己的证书。笔者就使用上次在搭建自己的CA服务 – OpenSSL CA 实战文章中生成的证书
ssl-ca=/home/yunweipai/user_certs/ca_cert.cer
ssl-cert=/home/yunweipai/user_certs/web.cer
ssl-key=/home/yunweipai/user_certs/web_key_plain.pem
这里需要注意的是,在ubuntu上,配置证书后如果不生效,参考这里解决方法
注意上面配置的 web_key_plain.pem 文件,由于MySQL不支持加密后的私钥,因此我们使用命令
openssl rsa -in web_key.pem -passin pass:Yunweipai@123 -out web_key_plain.pem
将私钥解密。MySQL不支持私钥加密的原因是从安全性角度考虑,因为如果要用户传递一个加密的私钥,那么必须要用户传密码,那么MySQL怎么存储这个密码呢?这就引出了我们在密码存储和传输的安全建议里面提到的一系列问题了。
所以MySQL为了简化实现,就不支持私钥加密。
指定客户端连接方式
MySQL服务端在对客户端授权的时候,可以通过选项指定客户端连接MySQL 服务器的SSL级别,参考MySQL赋权的 REQUIRE值:
ssl_option
SSL: 不认证客户端,客户端不需要提供证书
X509: 客户端必须发送一个有效的X509证书
issuer: 客户端的证书是否是服务器所配置的CA颁发的(在我们场景下是ca_cert.cer颁发的证书)
subject: 认证证书的subject(关于证书的subject在之前的文章有介绍)
cipher: 指定加密算法
这些选项可以叠加使用,如:X509|issuser
客户端连接(SSL方式)
mysql 客户端连接
mysql -u root -pChangeme_123 -P 3306 --ssl-ca=/home/yunweipai/user_certs/ca_cert.cer
Welcome to the MySQL monitor. Commands end with or \g.
Your MySQL connection id is 36
Server version: 5.5.43-0ubuntu0.12.04.1 (Ubuntu)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
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>\s
--------------
mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (i686) using readline 6.2
Connection id: 36
Current database:
Current user: root@localhost
SSL:Cipher in use is DHE-RSA-AES256-SHA
Current pager: stdout
Using outfile: ''
Using delimiter:
Server version: 5.5.43-0ubuntu0.12.04.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset:latin1
Db characterset:latin1
Client characterset:latin1
Conn. characterset:latin1
UNIX socket:/var/run/mysqld/mysqld.sock
Uptime: 29 sec
Threads: 1 Questions: 109 Slow queries: 0 Opens: 48 Flush tables: 1 Open tables: 41 Queries per second avg: 3.758
--------------
JDBC连接
在jdbc字符串中增加下面参数
useSSL=true&verifyServerCertificate=false
这么就不需要客户端配置证书了,配置就简单很多。因为mysql本身有账号口令认证,因此不需要证书认证。
创建Server SSL证书和私钥首先,创建一个临时的工作目录,我们将把私钥和证书文件放在该目录下。
$ sudo mkdir ~/cert
$ cd ~/cert
确保OpenSSL已安装在运行MySQL服务器的系统上。通常,所有Linux发行版在默认情况下都安装了OpenSSL。想检查一下OpenSSL有没有安装,不妨使用下面这个命令。
$ openssl version
OpenSSL 1.0.1f 6 Jan 2014
现在,继续创建CA私钥和证书。下面这些命令将创建ca-key.pem和ca-cert.pem。
$ openssl genrsa 2048 >ca-key.pem
$ openssl req -sha1 -new -x509 -nodes -days 3650 -key ca-key.pem >ca-cert.pem
第二个命令会询问你几个问题。你在这些字段里填入什么并不重要。只管填好那些字段。
下一步是为服务器创建私钥。
$ openssl req -sha1 -newkey rsa:2048 -days 730 -nodes -keyout server-key.pem >server-req.pem
这个命令会再次询问几个问题,你可以填写上一步中提供的相同答案。
下一步,使用下面这个命令,将服务器的私钥导出成RSA类型的密钥。
$ openssl rsa -in server-key.pem -out server-key.pem
最后,使用CA证书,创建服务器证书。
$ openssl x509 -sha1 -req -in server-req.pem -days 730 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 >server-cert.pem
配置MySQL服务器上的SSL
完成上述过程后,我们应该有了CA证书、服务器的私钥及其证书。下一步就是配置MySQL服务器,以
MySql安装
yum -y install libaio
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
cd /usr/local/soft/mysql/src
mkdir ../5.7.24_3306 &&tar -xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C ../5.7.24_3306 --strip-components 1
mkdir -p /usr/local/soft/mysql/5.7.24_3306/{run,data,logs,bin_log,sock,cert}
groupadd mysql &&useradd -r -g mysql -s /bin/false mysql
cd /usr/local/soft/mysql/5.7.24_3306/bin &&./mysqld --initialize
--explicit_defaults_for_timestamp
--user=mysql
--basedir=/usr/local/soft/mysql/5.7.24_3306
--datadir=/usr/local/soft/mysql/5.7.24_3306/data
--lc_messages_dir=/usr/local/soft/mysql/5.7.24_3306/share
--lc_messages=en_US
2018-12-01T09:56:45.934627Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-12-01T09:56:46.056837Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-12-01T09:56:46.127141Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 69d028f9-f54f-11e8-9c4a-00163e08e793.
2018-12-01T09:56:46.130249Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-12-01T09:56:46.131377Z 1 [Note] A temporary password is generated for root@localhost: sDdPx3>.,oBi
cd /usr/local/soft/mysql/5.7.24_3306/bin &&./mysql_ssl_rsa_setup --datadir=/usr/local/soft/mysql/5.7.24_3306/cert
修改证书权限为644
cd /usr/local/soft/mysql/5.7.24_3306/bin &&./mysql -uroot -S /usr/local/soft/mysql/5.7.24_3306/sock/mysql.sock
use mysql
update user set host = '%', authentication_string = password('123456') where user = 'root'
select host,user,authentication_string from user
flush privileges
grant all privileges on . to 'root'@'%'
flush privileges
cat >/etc/systemd/system/3306.service <<EOF
[Unit]
Description=MySQL 3306
Documentation= http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/soft/mysql/5.7.24_3306/run/mysql.pid
ExecStart=/usr/local/soft/mysql/5.7.24_3306/support-files/mysql.server start
ExecReload=/usr/local/soft/mysql/5.7.24_3306/support-files/mysql.server restart
ExecStop=/usr/local/soft/mysql/5.7.24_3306/support-files/mysql.server stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl enable 3306.service
1、
cd /usr/local/soft/mysql/5.7.24.3306/bin &&./mysqld_safe --skip-grant-tables &
cd /usr/local/soft/mysql/5.7.24.3306/bin &&./mysqld_safe --skip-grant-tables --skip-networking &
2、
./mysql -uroot -p -S /usr/local/soft/mysql/5.7.24.3306/sock/mysql.sock
3、
use mysql
update mysql.user set authentication_string=password('123456') where user='root'
flush privileges
mkdir -p /usr/local/soft/mysql/back/5.7.23_3306 &&cp -Rf /usr/local/soft/mysql/5.7.23_3306/* /usr/local/soft/mysql/back/5.7.23_3306
mkdir -p /usr/local/soft/mysql/back/5.7.24_3306 &&cp -Rf /usr/local/soft/mysql/5.7.24_3306/* /usr/local/soft/mysql/back/5.7.24_3306
CREATE USER 'sdyc'@'%' IDENTIFIED BY '123456'
GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE ON sd_data.* TO 'sdyc'@'%'
GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE ON card_data.* TO 'sdyc'@'%'
SHOW GRANTS FOR sdyc
CREATE USER 'back'@'localhost' IDENTIFIED BY '123456'
GRANT SELECT, RELOAD, PROCESS, SHOW DATABASES, SUPER, LOCK TABLES, REPLICATION CLIENT ON . TO 'back'@'localhost'
SHOW GRANTS FOR back
REVOKE SELECT, INSERT, UPDATE, DELETE, EXECUTE ON sd_data.* FROM 'sdyc'@'%'
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)