四、基于SSL实现数据库复制
要求主从服务器各自都要有证书和私钥;默认情况下主从服务器的SSL功能是没有启用的,需要先启用。
1、配置Master为CA证书服务器
- #vim /etc/pki/tls/openssl.cnf
- 将dir = ../../cA修改为
- dir = /etc/pki/CA
- #cd /etc/pki/CA
- #(umask 077; openssl genrsa 1024 > private/cakey.pem)
- #openssl req -new -x509 -key private/cakey.pem -out cacert.pem
- Country Name (2 letter code) [GB]:CN
- State or Province Name (full name) [Berkshire]:HN
- Locality Name (eg, city) [Newbury]:ZZ
- Organization Name (eg, company) [My Company Ltd]:Forman
- Organizational Unit Name (eg, section) []:Tech
- Common Name (eg, your name or your server's hostname) []:node1.forman.com
- Email Address []:
- #mkdir newcerts certs crl
- #touch index.txt
- #echo 01 > serial
4.2 为Master上的MySQL准备私钥以及颁发证书
- #mkdir /usr/local/mysql/ssl
- #cd /usr/local/mysql/ssl
- #(umask 077; openssl genrsa 1024 > mysql.key)
- #openssl req -new -kdy mysql.key -out mysql.csr
- CN
- HN
- ZZ
- Forman
- Tech
- node1.forman.com
- #openssl ca -in mysql.csr -out mysql.crt
- #cp /etc/pki/CA/cacert.pem /usr/local/mysql/ssl
- #cd /usr/local/mysql
- #chown -R mysql:mysql ssl/
4.3 为Slave上的mysql准私钥,证书申请、在Master上为Slave签发证书
- #mkdir /usr/local/mysql/ssl
- #cd /usr/local/mysql/ssl
- #(umask 077; openssl genrsa 1024 > mysql.key)
- #openssl req -new -key mysql.key -out mysql.csr
- CN
- HN
- ZZ
- Forman
- Tech
- node1.forman.com
- #scp ./mysql.csr node1:/root
- Master上为Slave签发证书
- #pwd
- /root
- #openssl ca -in mysql.csr -out mysql.crt
- #scp ./mysql.crt node2:/usr/local/mysql/ssl
- #scp /etc/pki/CA/cacert.pem node2:/usr/local/mysql/ssl
- 保证主从服务器上/usr/lcoal/mysql/ssl目录下的文件的属主,属组为mysql
- #ll /usrl/local/mysql/ssl
- -rw-r--r-- 1 mysql mysql 1119 Aug 20 20:13 cacert.pem
- -rw-r--r-- 1 mysql mysql 3067 Aug 20 20:04 mysql.crt
- -rw-r--r-- 1 mysql mysql 643 Aug 20 20:03 mysql.csr
- -rw------- 1 mysql mysql 887 Aug 20 20:02 mysql.key
4.4 开启主从服务器mysql的SSL功能
- Master:
- #vim /etc/my.cnf
- 在[mysqld]和[mysqldump]之间添加如下内容:
- ssl
- ssl-ca=/usr/local/mysql/ssl/cacert.pem
- ssl-cert=/usr/local/mysql/ssl/mysql.crt
- ssl-key=/usr/local/mysql/ssl/mysql.key
- 保存退出
- #service mysqld restart
- mysql> show global variables like '%ssl%';
- +---------------+---------------------------------+
- | Variable_name | Value |
- +---------------+---------------------------------+
- | have_openssl | YES |
- | have_ssl | YES |
- | ssl_ca | /usr/local/mysql/ssl/cacert.pem |
- | ssl_capath | |
- | ssl_cert | /usr/local/mysql/ssl/mysql.crt |
- | ssl_cipher | |
- | ssl_key | /usr/local/mysql/ssl/mysql.key |
- +---------------+---------------------------------+
- mysql> show master status;
- +------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +------------------+----------+--------------+------------------+
- | mysql-bin.000003 | 107 | testdb | |
- +------------------+----------+--------------+------------------+
- Slave:(建议不要在配置文件中指定,而是在change to命令中指定)
- #vim /etc/my.cnf
- 在[mysqld]和[mysqldump]之间添加如下内容:
- ssl
- #service mysqld restart
- mysql> change master to master_host='172.16.220.11',master_user='repluser',master_password='RedHat',master_log_file='mysql-bin.000003',master_log_pos=107,master_ssl=1,master_ssl_ca='/usr/local/mysql/ssl/cacert.pem',master_ssl_cert='/usr/local/mysql/ssl/mysql.crt',master_ssl_key='/usr/local/mysql/ssl/mysql.key';
- mysql> show global variables like '%ssl%';
- +---------------+---------------------------------+
- | Variable_name | Value |
- +---------------+---------------------------------+
- | have_openssl | YES |
- | have_ssl | YES |
- | ssl_ca | /usr/local/mysql/ssl/cacert.pem |
- | ssl_capath | |
- | ssl_cert | /usr/local/mysql/ssl/mysql.crt |
- | ssl_cipher | |
- | ssl_key | /usr/local/mysql/ssl/mysql.key |
- +---------------+---------------------------------+
- msyql> show slave status\G
- 输出的信息有:
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- Master_SSL_Allowed: Yes
- 说明,基于SSL的配置已经成功
4.5 验证是否使用了SSL加密
- Master上添加ssl的复制用户
- mysql> grant replication slave,replication client on *.* to ssluser@'172.16.220.12' identified by 'redhat' require ssl;
- mysql> flush privileges;
- Slave测试
- # /usr/local/mysql/bin/mysql --ssl-ca=/usr/local/mysql/ssl/cacert.pem --ssl-cert=/usr/local/mysql/ssl/mysql.crt --ssl-key=/usr/local/mysql/ssl/mysql.key -ussluser -h 172.16.220.11 -p
- Enter password
- mysql>\s
- 输出信息中有这么一项
- SSL: Cipher in use is DHE-RSA-AES256-SHA
- 说明是加密连接了
操作的过程中也遇到了诸多的问题:
- 问题
- 1、在2.2步骤中
- mysql>show slave status\G 时遇到这样的问题
- Slave_IO_Running: No
- Slave_SQL_Running: Yes
- 原因:退出了mysql,而后登陆再执行的这条命令,就出现了这样的问题;不退出mysql,直接执行,就不会有问题了
