一、前言
备份数据库是生产环境中的首要任务,重中之重,有时候不得不通过网络进行数据库的复制,这样就需要保证数据在网络传输过程中的安全性,因此使用基于SSL的复制会大加强数据的安全性
二、准备工作
1、主从服务器时间同步
1.[root@master ~]# crontab -e
2.*/30 * * * * /usr/sbin/ntpdate 172.16.0.1 &>/dev/null
2、mysql说明
(1)主服务器
hostname:master IP:172.16.7.202
(2)从服务器
hostname:slave IP:172.16.7.250
(3)数据目录
/mydata/data
(4)二进制日志目录
/mydata/binlogs
(5)中继日志目录
/mydata/relaylogs
三、SSL主从同步的实现
1、master(172.16.7.202)安装后配置文件
01.thread_concurrency = 4
02.datadir = /mydata/data #数据目录
03.log-bin=/mydata/binlogs/master-bin
04.relay-log=/mydata/relaylogs/relay
05.sync_binlog = 1 #设定每1秒钟同步一次缓冲中的数据到日志文件中
06.binlog_format=mixed #二进制日志格式为混合模式
07.server-id = 1 #主服务器的server-id=1,从的等于2
08.#
09.#
10.#slave(172.16.7.250)同master
2、将master(172.16.7.202)做为CA服务器
01.[root@master ~]# cd /etc/pki/CA
02.[root@master CA]#
03.[root@master CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
04.Generating RSA private key, 2048 bit long modulus
05...........................................+++
06...................+++
07.e is 65537 (0x10001)
08.[root@master CA]#
09.[root@master CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 36500
10.You are about to be asked to enter information that will be incorporated
11.into your certificate request.
12.What you are about to enter is what is called a Distinguished Name or a DN.
13.There are quite a few fields but you can leave some blank
14.For some fields there will be a default value,
15.If you enter '.', the field will be left blank.
16.-----
17.Country Name (2 letter code) [XX]:CN
18.State or Province Name (full name) []:Beijing
19.Locality Name (eg, city) [Default City]:Beijing
20.Organization Name (eg, company) [Default Company Ltd]:sina
21.Organizational Unit Name (eg, section) []:mysql
22.Common Name (eg, your name or your server's hostname) []:master.sina.com
23.Email Address []:
24.[root@master CA]# touch index.txt serial crlnumber
25.[root@master CA]# echo 01 > serial
3、为master(172.16.7.202)签发证书
01.[root@master CA]# mkdir /usr/local/mysql/ssl
02.[root@master CA]# cd /usr/local/mysql/ssl
03.[root@master ssl]# (umask 077;openssl genrsa -out master.key 2048)
04.Generating RSA private key, 2048 bit long modulus
05...........+++
06.............................................................+++
07.e is 65537 (0x10001)
08.[root@master ssl]#
09.[root@master ssl]# openssl req -new -key master.key -out master.csr -days 36500
10.You are about to be asked to enter information that will be incorporated
11.into your certificate request.
12.What you are about to enter is what is called a Distinguished Name or a DN.
13.There are quite a few fields but you can leave some blank
14.For some fields there will be a default value,
15.If you enter '.', the field will be left blank.
16.-----
17.Country Name (2 letter code) [XX]:CN
18.State or Province Name (full name) []:Beijing
19.Locality Name (eg, city) [Default City]:Beijing
20.Organization Name (eg, company) [Default Company Ltd]:sina
21.Organizational Unit Name (eg, section) []:mysql
22.Common Name (eg, your name or your server's hostname) []:master.sina.com
23.Email Address []:
24.Please enter the following 'extra' attributes
25.to be sent with your certificate request
26.A challenge pass<a href="http://www.it165.net/edu/ebg/" target="_blank" class="keylink">word</a> []:
27.An optional company name []:
28.[root@master ssl]#
29.[root@master ssl]# openssl ca -in master.csr -out master.crt -days 36500
30.Using configuration from /etc/pki/tls/openssl.cnf
31.Check that the request matches the signature
32.Signature ok
33.Certificate Details:
34.Serial Number: 1 (0x1)
35.Validity
36.Not Before: May 3 13:34:58 2014 GMT
37.Not After : Apr 9 13:34:58 2114 GMT
38.Subject:
39.countryName = CN
40.stateOrProvinceName = Beijing
41.organizationName = sina
42.organizationalUnitName = mysql
43.commonName = master.sina.com
44.X509v3 extensions:
45.X509v3 Basic Constraints:
46.CA:FALSE
47.Netscape Comment:
48.OpenSSL Generated Certificate
49.X509v3 Subject Key Identifier:
50.62:EF:37:1D:96:FF:8A:89:47:09:2D:93:74:42:14:BF:8E:AC:51:49
51.X509v3 Authority Key Identifier:
52.keyid:6B:73:D6:FE:81:13:2C:0E:EC:61:EE:F7:6F:92:91:6D:82:37:A0:11
53.Certificate is to be certified until Apr 9 13:34:58 2114 GMT (36500 days)
54.Sign the certificate? [y/n]:y
55.1 out of 1 certificate requests certified, commit? [y/n]y
56.Write out database with 1 new entries
57.Data Base Updated
4、slave生成证书申请请求
01.[root@slave ~]# mkdir /usr/local/mysql/ssl
02.[root@slave ~]# cd /usr/local/mysql/ssl
03.[root@slave ssl]# (umask 077;openssl genrsa -out slave.key 2048)
04.Generating RSA private key, 2048 bit long modulus
05......................................................+++
06.........................................+++
07.e is 65537 (0x10001)
08.[root@slave ssl]#
09.[root@slave ssl]# openssl req -new -key slave.key -out slave.csr -days 36500
10.You are about to be asked to enter information that will be incorporated
11.into your certificate request.
12.What you are about to enter is what is called a Distinguished Name or a DN.
13.There are quite a few fields but you can leave some blank
14.For some fields there will be a default value,
15.If you enter '.', the field will be left blank.
16.-----
17.Country Name (2 letter code) [XX]:CN
18.State or Province Name (full name) []:Beijing
19.Locality Name (eg, city) [Default City]:Beijing
20.Organization Name (eg, company) [Default Company Ltd]:sina
21.Organizational Unit Name (eg, section) []:mysql
22.Common Name (eg, your name or your server's hostname) []:slave.sina.com
23.Email Address []:
24.Please enter the following 'extra' attributes
25.to be sent with your certificate request
26.A challenge pass<a href="http://www.it165.net/edu/ebg/" target="_blank" class="keylink">word</a> []:
27.An optional company name []:
28.[root@slave ssl]#
29.[root@slave ssl]#
30.[root@slave ssl]# scp slave.csr root@172.16.7.202:/root
5、为slave(172.16.7.250)签发证书
01.[root@master ~]# openssl ca -in slave.csr -out slave.crt -days 36500
02.Using configuration from /etc/pki/tls/openssl.cnf
03.Check that the request matches the signature
04.Signature ok
05.Certificate Details:
06.Serial Number: 2 (0x2)
07.Validity
08.Not Before: May 3 13:43:28 2014 GMT
09.Not After : Apr 9 13:43:28 2114 GMT
10.Subject:
11.countryName = CN
12.stateOrProvinceName = Beijing
13.organizationName = sina
14.organizationalUnitName = mysql
15.commonName = slave.sina.com
16.X509v3 extensions:
17.X509v3 Basic Constraints:
18.CA:FALSE
19.Netscape Comment:
20.OpenSSL Generated Certificate
21.X509v3 Subject Key Identifier:
22.20:CB:55:9C:D0:7A:F0:25:70:AC:84:2B:8E:F4:24:FB:1F:51:48:9D
23.X509v3 Authority Key Identifier:
24.keyid:6B:73:D6:FE:81:13:2C:0E:EC:61:EE:F7:6F:92:91:6D:82:37:A0:11
25.Certificate is to be certified until Apr 9 13:43:28 2114 GMT (36500 days)
26.Sign the certificate? [y/n]:y
27.1 out of 1 certificate requests certified, commit? [y/n]y
28.Write out database with 1 new entries
29.Data Base Updated
30.[root@master ~]#
31.[root@master ~]#
32.[root@master ~]# scp slave.crt root@172.16.7.250:/usr/local/mysql/ssl/
6、为master及slave提供CA的证书
1.[root@master ~]# cp /etc/pki/CA/cacert.pem /usr/local/mysql/ssl/
2.[root@master ~]# scp /etc/pki/CA/cacert.pem root@172.16.7.250:/usr/local/mysql/ssl/
7、修改master和slave的属主、属组为"mysql"
01.[root@master ~]# chown -R mysql.mysql /usr/local/mysql/ssl/
02.[root@master ~]# ll /usr/local/mysql/ssl/
03.total 20
04.-rw-r--r-- 1 mysql mysql 1330 May 3 21:48 cacert.pem
05.-rw-r--r-- 1 mysql mysql 4465 May 3 21:35 master.crt
06.-rw-r--r-- 1 mysql mysql 1009 May 3 21:33 master.csr
07.-rw------- 1 mysql mysql 1675 May 3 21:32 master.key
08.###
09.###
10.[root@slave ssl]# chown -R mysql.mysql /usr/local/mysql/ssl/
11.[root@slave ssl]# ll /usr/local/mysql/ssl/
12.total 20
13.-rw-r--r-- 1 mysql mysql 1330 May 3 21:49 cacert.pem
14.-rw-r--r-- 1 mysql mysql 4460 May 3 21:44 slave.crt
15.-rw-r--r-- 1 mysql mysql 1005 May 3 21:40 slave.csr
16.-rw------- 1 mysql mysql 1679 May 3 21:38 slave.key
8、修改mysql配置文件开启SSL加密功能
01.[root@master ~]# vim /etc/my.cnf
02.[mysqld]
03.ssl
04.ssl_ca = /usr/local/mysql/ssl/cacert.pem
05.ssl_key = /usr/local/mysql/ssl/master.key
06.ssl_cert = /usr/local/mysql/ssl/master.crt
07.[root@master ~]# service mysqld restart
08.#
09.#
10.[root@slave ~]# vim /etc/my.cnf
11.[mysqld]
12.ssl
13.ssl_ca = /usr/local/mysql/ssl/cacert.pem
14.ssl_key = /usr/local/mysql/ssl/slave.key
15.ssl_cert = /usr/local/mysql/ssl/slave.crt
16.[root@slave ~]# service mysqld restart
9、在master上验证SSL加密功能开启并创建基于密钥认证用户
01.[root@master ~]# mysql
02.MariaDB [(none)]> show variables like '%ssl%';
03.+---------------+---------------------------------+
04.| Variable_name | Value |
05.+---------------+---------------------------------+
06.| have_openssl | NO |
07.| have_ssl | YES |
08.| ssl_ca | /usr/local/mysql/ssl/cacert.pem |
09.| ssl_capath | |
10.| ssl_cert | /usr/local/mysql/ssl/master.crt |
11.| ssl_cipher | |
12.| ssl_crl | |
13.| ssl_crlpath | |
14.| ssl_key | /usr/local/mysql/ssl/master.key |
15.+---------------+---------------------------------+
16.MariaDB [(none)]>
17.MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.%.%' identified by 'repluser' require ssl;
18.MariaDB [(none)]> flush privileges;
10、查看master状态信息
1.MariaDB [(none)]> show master status;
2.+-------------------+----------+--------------+------------------+
3.| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
4.+-------------------+----------+--------------+------------------+
5.| master-bin.000002 | 652 | | |
6.+-------------------+----------+--------------+------------------+
11、验证slave开启SSL加密功能
01.[root@slave ~]# mysql
02.MariaDB [(none)]>
03.MariaDB [(none)]> show variables like '%ssl%';
04.+---------------+---------------------------------+
05.| Variable_name | Value |
06.+---------------+---------------------------------+
07.| have_openssl | NO |
08.| have_ssl | YES |
09.| ssl_ca | /usr/local/mysql/ssl/cacert.pem |
10.| ssl_capath | |
11.| ssl_cert | /usr/local/mysql/ssl/slave.crt |
12.| ssl_cipher | |
13.| ssl_crl | |
14.| ssl_crlpath | |
15.| ssl_key | /usr/local/mysql/ssl/slave.key |
16.+---------------+---------------------------------+
12、slave连接master
01.MariaDB [(none)]> change master to master_host='172.16.7.202',master_user='repluser',master_password='repluser',master_log_file='master-bin.000002',master_log_pos=652,master_ssl=1,master_ssl_ca='/usr/local/mysql/ssl/cacert.pem',master_ssl_cert='/usr/local/mysql/ssl/slave.crt',master_ssl_key='/usr/local/mysql/ssl/slave.key';
02.Query OK, 0 rows affected (0.06 sec)
03.MariaDB [(none)]>
04.MariaDB [(none)]>
05.MariaDB [(none)]> start slave;
06.Query OK, 0 rows affected (0.04 sec)
07.MariaDB [(none)]>
08.MariaDB [(none)]>
09.MariaDB [(none)]> show slave status\G;
10.*************************** 1. row ***************************
11.Slave_IO_State: Waiting for master to send event
12.Master_Host: 172.16.7.202
13.Master_User: repluser
14.Master_Port: 3306
15.Connect_Retry: 60
16.Master_Log_File: master-bin.000002
17.Read_Master_Log_Pos: 652
18.Relay_Log_File: relay.000002
19.Relay_Log_Pos: 536
20.Relay_Master_Log_File: master-bin.000002
21.Slave_IO_Running: Yes
22.Slave_SQL_Running: Yes
23.Replicate_Do_DB:
24.Replicate_Ignore_DB:
25.Replicate_Do_Table:
26.Replicate_Ignore_Table:
27.Replicate_Wild_Do_Table:
28.Replicate_Wild_Ignore_Table:
29.Last_Errno: 0
30.Last_Error:
31.Skip_Counter: 0
32.Exec_Master_Log_Pos: 652
33.Relay_Log_Space: 823
34.Until_Condition: None
35.Until_Log_File:
36.Until_Log_Pos: 0
37.Master_SSL_Allowed: Yes
38.Master_SSL_CA_File: /usr/local/mysql/ssl/cacert.pem
39.Master_SSL_CA_Path:
40.Master_SSL_Cert: /usr/local/mysql/ssl/slave.crt
41.Master_SSL_Cipher:
42.Master_SSL_Key: /usr/local/mysql/ssl/slave.key
43.Seconds_Behind_Master: 0
44.Master_SSL_Verify_Server_Cert: No
45.Last_IO_Errno: 0
46.Last_IO_Error:
47.Last_SQL_Errno: 0
48.Last_SQL_Error:
49.Replicate_Ignore_Server_Ids:
50.Master_Server_Id: 1
51.Master_SSL_Crl: /usr/local/mysql/ssl/cacert.pem
52.Master_SSL_Crlpath:
53.Using_Gtid: No
54.Gtid_IO_Pos:
55.1 row in set (0.00 sec)
四、同步验证
1、在master上新建数据库hlbrc
01.MariaDB [(none)]> create database hlbrc;
02.Query OK, 1 row affected (0.01 sec)
03.MariaDB [(none)]> show databases;
04.+--------------------+
05.| Database |
06.+--------------------+
07.| hlbrc |
08.| information_schema |
09.| mysql |
10.| performance_schema |
11.| test |
12.+--------------------+
2、在slave上验证
01.MariaDB [(none)]> show databases;
02.+--------------------+
03.| Database |
04.+--------------------+
05.| hlbrc |
06.| information_schema |
07.| mysql |
08.| performance_schema |
09.| test |
10.+--------------------+
--转自