手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL主从复制/SSL加密

[日期:2012-08-11] 来源:Linux社区  作者:xinghuiru [字体:大 中 小]

四、基于SSL实现数据库复制

要求主从服务器各自都要有证书和私钥;默认情况下主从服务器的SSL功能是没有启用的,需要先启用。

1、配置Master为CA证书服务器

  1. #vim /etc/pki/tls/openssl.cnf  
  2. 将dir             = ../../cA修改为  
  3. dir             = /etc/pki/CA     
  4. #cd /etc/pki/CA  
  5. #(umask 077; openssl genrsa 1024 > private/cakey.pem)  
  6. #openssl req -new -x509 -key private/cakey.pem -out cacert.pem  
  7.  
  8. Country Name (2 letter code) [GB]:CN  
  9. State or Province Name (full name) [Berkshire]:HN  
  10. Locality Name (eg, city) [Newbury]:ZZ  
  11. Organization Name (eg, company) [My Company Ltd]:Forman  
  12. Organizational Unit Name (eg, section) []:Tech  
  13. Common Name (eg, your name or your server's hostname) []:node1.forman.com  
  14. Email Address []:  
  15. #mkdir newcerts certs crl  
  16. #touch index.txt  
  17. #echo 01 > serial  
  18.  

 

4.2 为Master上的MySQL准备私钥以及颁发证书

  1. #mkdir /usr/local/mysql/ssl  
  2. #cd /usr/local/mysql/ssl  
  3. #(umask 077; openssl genrsa 1024 > mysql.key)  
  4. #openssl req -new -kdy mysql.key -out mysql.csr  
  5. CN  
  6. HN  
  7. ZZ  
  8. Forman 
  9. Tech  
  10. node1.forman.com  
  11.  
  12. #openssl ca -in mysql.csr -out mysql.crt  
  13. #cp /etc/pki/CA/cacert.pem /usr/local/mysql/ssl  
  14. #cd /usr/local/mysql  
  15. #chown -R mysql:mysql ssl/  
  16.  

 


4.3 为Slave上的mysql准私钥,证书申请、在Master上为Slave签发证书

  1. #mkdir /usr/local/mysql/ssl  
  2. #cd /usr/local/mysql/ssl  
  3. #(umask 077; openssl genrsa 1024 > mysql.key)  
  4. #openssl req -new -key mysql.key -out mysql.csr  
  5. CN  
  6. HN  
  7. ZZ  
  8. Forman  
  9. Tech  
  10. node1.forman.com  
  11.  
  12. #scp ./mysql.csr node1:/root  
  13.  
  14. Master上为Slave签发证书  
  15. #pwd  
  16. /root  
  17. #openssl ca -in mysql.csr -out mysql.crt  
  18. #scp ./mysql.crt node2:/usr/local/mysql/ssl  
  19. #scp /etc/pki/CA/cacert.pem node2:/usr/local/mysql/ssl  
  20.  
  21. 保证主从服务器上/usr/lcoal/mysql/ssl目录下的文件的属主,属组为mysql  
  22. #ll /usrl/local/mysql/ssl  
  23. -rw-r--r-- 1 mysql mysql 1119 Aug 20 20:13 cacert.pem  
  24. -rw-r--r-- 1 mysql mysql 3067 Aug 20 20:04 mysql.crt  
  25. -rw-r--r-- 1 mysql mysql  643 Aug 20 20:03 mysql.csr  
  26. -rw------- 1 mysql mysql  887 Aug 20 20:02 mysql.key  
  27.  

 

4.4 开启主从服务器mysql的SSL功能

  1. Master:  
  2. #vim /etc/my.cnf  
  3.  
  4. 在[mysqld]和[mysqldump]之间添加如下内容:  
  5. ssl  
  6. ssl-ca=/usr/local/mysql/ssl/cacert.pem  
  7. ssl-cert=/usr/local/mysql/ssl/mysql.crt  
  8. ssl-key=/usr/local/mysql/ssl/mysql.key  
  9. 保存退出  
  10. #service mysqld restart  
  11.  
  12. mysql> show global variables like '%ssl%';  
  13. +---------------+---------------------------------+  
  14. | Variable_name | Value                           |  
  15. +---------------+---------------------------------+  
  16. | have_openssl  | YES                             |  
  17. | have_ssl      | YES                             |  
  18. | ssl_ca        | /usr/local/mysql/ssl/cacert.pem |  
  19. | ssl_capath    |                                 |  
  20. | ssl_cert      | /usr/local/mysql/ssl/mysql.crt  |  
  21. | ssl_cipher    |                                 |  
  22. | ssl_key       | /usr/local/mysql/ssl/mysql.key  |  
  23. +---------------+---------------------------------+  
  24. mysql> show master status;  
  25. +------------------+----------+--------------+------------------+  
  26. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
  27. +------------------+----------+--------------+------------------+  
  28. | mysql-bin.000003 |      107 | testdb       |                  |  
  29. +------------------+----------+--------------+------------------+  
  30.  
  31. Slave:(建议不要在配置文件中指定,而是在change to命令中指定)  
  32. #vim /etc/my.cnf  
  33. 在[mysqld]和[mysqldump]之间添加如下内容:  
  34. ssl  
  35. #service mysqld restart  
  36.  
  37. 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';  
  38. mysql> show global variables like '%ssl%';  
  39. +---------------+---------------------------------+  
  40. | Variable_name | Value                           |  
  41. +---------------+---------------------------------+  
  42. | have_openssl  | YES                             |  
  43. | have_ssl      | YES                             |  
  44. | ssl_ca        | /usr/local/mysql/ssl/cacert.pem |  
  45. | ssl_capath    |                                 |  
  46. | ssl_cert      | /usr/local/mysql/ssl/mysql.crt  |  
  47. | ssl_cipher    |                                 |  
  48. | ssl_key       | /usr/local/mysql/ssl/mysql.key  |  
  49. +---------------+---------------------------------+  
  50. msyql> show slave status\G  
  51. 输出的信息有:  
  52. Slave_IO_Running: Yes  
  53. Slave_SQL_Running: Yes  
  54. Master_SSL_Allowed: Yes  
  55. 说明,基于SSL的配置已经成功  
  56.   

4.5 验证是否使用了SSL加密

  1. Master上添加ssl的复制用户  
  2. mysql> grant replication slave,replication client on *.* to ssluser@'172.16.220.12' identified by 'redhat' require ssl;  
  3. mysql> flush privileges;  
  4.  
  5. Slave测试  
  6.  
  7. # /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  
  8. Enter password  
  9. mysql>\s  
  10. 输出信息中有这么一项  
  11. SSL:   Cipher in use is DHE-RSA-AES256-SHA  
  12.  
  13. 说明是加密连接了  
  14.  

操作的过程中也遇到了诸多的问题: 
 

  1. 问题  
  2. 1、在2.2步骤中  
  3. mysql>show slave status\G 时遇到这样的问题  
  4.  Slave_IO_Running: No  
  5.  Slave_SQL_Running: Yes  
  6. 原因:退出了mysql,而后登陆再执行的这条命令,就出现了这样的问题;不退出mysql,直接执行,就不会有问题了 
linux
相关资讯       SSL  MySQL主从复制 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款