MySQL-based encrypted master-slave replication using SSL

Source: Internet
Author: User

We all know that MySQL master-slave replication is transmitted in plain text, which is not allowed for some special businesses. Next we will try to build an SSL-based master-slave Replication

Environment: RHEL5.8 SELinux disabled, iptables disabled, MySQL 5.5.28-i686 tar package initialization and installation (non-compilation) plan:
 
 
  1. master: 172.16.1.18  master.laoguang.me 
  2. slave:  172.16.1.19  slave.laoguang.me 
Preparations: the hostname is consistent with the plan. Configure/etc/hosts for resolution. The time must be synchronized and the process will not be described in detail. For details, refer to the details section. 1. Configure the CA server on the master and issue a certificate to the master and slave.1.1 master to establish a CA server, the process see the http://laoguang.blog.51cto.com/6013350/10356081.2 master MySQL Certificate Application
 
 
  1. Mkdir/data/mydata/ssl
  2. Cd/data/mydata/ssl
  3. Openssl genrsa 1024> mysql. key
  4. Openssl req-new-key mysql. key-out mysql. csr-days 3650
  5. # The following input is the same as that when the CA is created. The common name is master. laoguang. me.
  6. Openssl ca-in mysql. csr-out mysql. crt # MySQL visa
  7. Cp/etc/pki/CA/cacert. pem. # copy the CA certificate.
  8. Chown mysql: mysql *
  9. Chmod 600 *
1.3 apply for a certificate on slave
 
 
  1. Mkdir/data/mydata/ssl
  2. Cd/data/mydata/ssl
  3. Openssl genrsa 1024> mysql. key
  4. Openssl req-new-key mysql. key-out mysql. csr-days 3650
  5. # The following input is the same as that when the CA is created. The common name is slave. laoguang. me.
  6. Scp mysql. csr master:/root
1.4 The master node issues the Server Load balancer instance
 
 
  1. cd /root 
  2. openssl ca -in mysql.csr -out mysql.crt 
  3. scp mysql.crt slave:/data/mydata/ssl 
  4. scp /etc/pki/CA/cacert.pem slave:/data/mydata/ssl 
1.5 Change permissions and owner on slave
 
 
  1. chown mysql:mysql mysql.* 
  2. chmod 600 mysql.* 
Ii. Compile/etc/my. cnf on the Master to enable ssl, And set Master/Slave2.1 Modify/etc/my. cnf
 
 
  1. [Mysqld]
  2. Log-bin = mysql-bin
  3. Sync_binlog = 1 # binary log
  4. Server-id = 1 # This id must be globally unique
  5. Innodb_flush_log_at_trx_commit = 1 # immediately fl transaction logs to the disk every second
  6. Ssl ## ssl is disabled by default. in mysql, view show variables like '% ssl % '.
  7. Ssl_ca =/data/mydata/ssl/cacert. pem # location of the ca File
  8. Ssl_cert =/data/mydata/ssl/mysql. crt # Certificate file location
  9. Ssl_key =/data/mydata/ssl/mysql. key # location of the private key file
2.2 start mysql and view ssl information
 
 
  1. service mysqld start 
  2. mysql 
  3. mysql> show variables like '%ssl%'; 
  4. +---------------+-----------------------------+ 
  5. | Variable_name | Value                       | 
  6. +---------------+-----------------------------+ 
  7. | have_openssl  | YES                         | 
  8. | have_ssl      | YES                         | 
  9. | ssl_ca        | /data/mydata/ssl/cacert.pem | 
  10. | ssl_capath    |                             | 
  11. | ssl_cert      | /data/mydata/ssl/mysql.crt  | 
  12. | ssl_cipher    |                             | 
  13. | ssl_key       | /data/mydata/ssl/mysql.key  | 
  14. +---------------+-----------------------------+ 
2.3 create a minimum permission account for synchronization and require ssl
 
 
  1. mysql> create user 'backup_ssl'@'172.16.1.19' identified by 'redhat'; 
  2. mysql> revoke all privileges,grant option from 'backup_ssl'@'172.16.1.19'; 
  3. mysql> grant replication slave,replication client on *.* to 'backup_ssl'@'172.16.1.19' require ssl; 
  4. mysql> flush privileges; 
3. Compile/etc/my. cnf on Slave, enable ssl, and set Master/Slave3.1 editing/etc/my. cnf
 
 
  1. [Mysqld]
  2. Server-id = 2 # This id must be globally unique
    # Log-bin = mysql-bin # comment out, slave server does not need binary logs
  3. Relay-log = mysql-ralay # relay logs
  4. Relay-log-index = mysql-ralay.index # relay directory
  5. Read-only = 1 # slave server read-only
  6. Ssl ## ssl is disabled by default. in mysql, view show variables like '% ssl % '.
  7. Ssl_ca =/data/mydata/ssl/cacert. pem # location of the ca File
  8. Ssl_cert =/data/mydata/ssl/mysql. crt # Certificate file location
  9. Ssl_key =/data/mydata/ssl/mysql. key # location of the private key file
3.2 enable mysqld and view ssl information
 
 
  1. servie mysqld start 
  2. mysql> show variables like '%ssl%'; 
  3. +---------------+-----------------------------+ 
  4. | Variable_name | Value                       | 
  5. +---------------+-----------------------------+ 
  6. | have_openssl  | YES                         | 
  7. | have_ssl      | YES                         | 
  8. | ssl_ca        | /data/mydata/ssl/cacert.pem | 
  9. | ssl_capath    |                             | 
  10. | ssl_cert      | /data/mydata/ssl/mysql.crt  | 
  11. | ssl_cipher    |                             | 
  12. | ssl_key       | /data/mydata/ssl/mysql.key  | 
  13. +---------------+-----------------------------+ 
3.3 start the slave synchronization process and connect to the master server
 
 
  1. Mysql> change master
  2. -> Master_host = '2017. 16.1.18 ',
  3. -> Master_user = 'backup _ ssl ',
  4. -> Master_password = 'redhat ',
  5. -> Master_log_file = 'mysql-bin.000001 ',
  6. -> Master_ssl = 1,
  7. -> Master_ssl_ca = '/data/mydata/ssl/cacert. pem ',
  8. -> Master_ssl_cert = '/data/mydata/ssl/mysql. crt ',
  9. -> Master_ssl_key = '/data/mydata/ssl/mysql. key ';
  10. Mysql> start slave
  11. Mysql> show slave status \ G; # view slave status
Follow these parameters:
 
 
  1. Slave_IO_Running: Yes # Whether IOthread is running. If No is set, the slave is not running properly.
  2. Slave_ SQL _Running: Yes ## whether SQLthread is running. If No is set, slave is not running properly.
  3. Master_SSL_CA_File:/data/mydata/ssl/cacert. pem # whether ssl is enabled
  4. Master_SSL_Cert:/data/mydata/ssl/mysql. crt
  5. Master_SSL_Key:/data/mydata/ssl/mysql. key
  6. Master_Log_File: mysql-bin.000023 # binary of the last received master server
  7. Exec_Master_Log_Pos: 1087 # the last execution location. Check whether the location is in the master.
  8. Last_IO_Errno: 0 # Is there an error in the last IOthread?
If it seems like tired, slave is basically normal, the following test Iv. Test4.1 create a database on the master server
 
 
  1. mysql> create database testssl; 
4.2 check whether synchronization has been performed on the server
 
 
  1. mysql> show databases; 
If the synchronization is successful, no error occurs. 4.3 The slave server mysql connects to the master server based on ssl and checks whether the connection status is encrypted.
 
 
  1. mysql -ubackup_ssl -predhat -h172.16.1.18 --ssl-cert=/data/mydata/ssl/mysql.crt \
  2. --ssl-key=/data/mydata/ssl/mysql.key 
View connection status
 
 
  1. mysql> status; 
  2. Current user:       backup_ssl@slave.laoguang.me 
  3. SSL:            Cipher in use is DHE-RSA-AES256-SHA 
We can see that the connection is encrypted. You can use tcpdump to capture packets and test that the SSL-based mysql master-slave synchronization has been built. If your slave server is newly added, first, recover the last full backup of the master server to the slave server, and synchronize the binary logs after the full backup, that is, add master_log_op = n when changing the master, n represents the binary location after the complete backup, and the others are basically the same. Note: Today, I tried to only issue a certificate to slave. The master has a CA certificate. Theoretically, it should be successful, but it cannot be connected, so I will give up temporarily, then try to name the master certificate as master. crt, slave certificate is slave. the crt results cannot be connected. Later, google named the master and slave certificates and private keys mysql. crt, mysql. the key is successfully completed. If you know why the document cannot be used, the two certificates cannot be used for different names. Thank you!

This article from the "Free Linux, Share Linux" blog, please be sure to keep this source http://laoguang.blog.51cto.com/6013350/1079787

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.