生产环境中一台mysql主机存在单点故障,所以我们要确保mysql的高可用性,即两台MySQL服务器如果其中有一台MySQL服务器挂掉后,另外一台能立马接替其进行工作。
主从复制的原理
master记录二进制日志,在每个事务更新数据完成之前,master在二日志记录这些改变。MySQL将事务写入二进制日志,在事件写入二进制日志完成后,master通知存储引擎提交事务。 下一步就是slave将master的binary log拷贝到它自己的中继日志。首先,slave开始一个工作线程——I/O线程,I/O线程在master上打开一个普通的连接,然后开启binlog dump process。Binlog dump process从master的二进制日志中读取事件,如果已经同步了master,它会睡眠并等待master产生新的事件,I/O线程将这些事件写入中继日志。 SQL slave thread(SQL从线程)处理该过程的最后一步。SQL线程从中继日志读取事件,并重放其中的事件而更新slave的数据,使其与master中的数据一致。只要该线程与I/O线程保持一致,中继日志通常会位于OS的缓存中,所以中继日志的开销很小。
环境准备:打开两台MySQL服务器,部署网络环境。
部署master
1. 主机创建 SSL/RSA 文件
1. [root@master ~]# cd /usr/local/mysql/bin/ 2. [root@master bin]# mysql_ssl_rsa_setup --user=mysql --basedir=/usr/llocal/mysql --datadir=/usr/local/mysql/data
2. 赋予权限并重启。
1. [root@master bin]# chmod +r /usr/local/mysql/data/server-key.pem 2. [root@master bin]# service mysqld restart 3. Shutting down MySQL.. [ 确定 ] 4. Starting MySQL. [ 确定 ]
3. 登录mysql查看ssl是否开启,并创建一个复制用户。
注:启用 mysql 支持 ssl 安全连接主要用于 mysql 主从复制(局域网可以非 ssh 连接即明文复制,但 internet 复制建议采用 ssl 连接)
1. mysql> grant replication slave on *.* to rep@'192.168.8.3' identified by '123'; 2. Query OK, 0 rows affected, 1 warning (0.07 sec)
4. master开启二进制日志,重启后查看二进制日志文件。
需要注意的是server_id必须唯一。
1. [root@master ~]# vim /etc/my.cnf 2. #添加下面内容 3. log-bin=mysql-bin 4. service_id=1 5. [root@master ~]# service mysqld restart 6. Shutting down MySQL.. [ 确定 ] 7. Starting MySQL. [ 确定 ] 8. [root@master ~]# mysql -uroot -p123 -e "show master status" 9. mysql: [Warning] Using a password on the command line interface can be insecure. 10. +------------------+----------+--------------+------------------+-------------------+ 11. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | 12. +------------------+----------+--------------+------------------+-------------------+ 13. | mysql-bin.000001 | 154 | | | | 14. +------------------+----------+--------------+------------------+-------------------+
5. 防火墙配置,实验环境中可以关闭防火墙,生产环境中需要配置防火墙规则,允许3306端口。
1. [root@master ~]# firewall-cmd --permanent --add-port=3306/tcp 2. success 3. [root@master ~]# firewall-cmd --reload 4. success
6. 把ssl文件复制到slave
1. [root@master data]# scp ca.pem client-cert.pem client-key.pem root@192.168.8.3:/usr/local/mysql/data 2. The authenticity of host '192.168.8.3 (192.168.8.3)' can't be established. 3. ECDSA key fingerprint is SHA256:LFby9KMDz/kkPfOESbeJ7Qh+3hmQaX2W5gkDDMwSGHA. 4. ECDSA key fingerprint is MD5:03:32:64:b4:c2:5b:6c:a4:e2:f0:7f:df:7a:35:19:80. 5. Are you sure you want to continue connecting (yes/no)? yes 6. Warning: Permanently added '192.168.8.3' (ECDSA) to the list of known hosts. 7. root@192.168.8.3's password: 8. ca.pem 100% 1112 232.5KB/s 00:00 9. client-cert.pem 100% 1112 240.4KB/s 00:00 10. client-key.pem 100% 1676 205.0KB/s 00:00
部署slave
1. 开启ssl、中继日志,赋予ssl文件读的权限并重启mysql。
1. [root@slave ~]# vim /etc/my.cnf 2. #添加下面内容 3. server_id=2 4. relay-log=relay-log 5. ssl_ca=ca.pem 6. ssl_cert=client-cert.pem 7. ssl_key=client-key.pem 8. 9. [root@slave ~]# cd /usr/local/mysql/data 10. [root@slave data]# ll ca.pem client-cert.pem client-key.pem 11. -rw-r--r--. 1 mysql mysql 1112 3月 31 14:31 ca.pem 12. -rw-r--r--. 1 mysql mysql 1112 3月 31 14:31 client-cert.pem 13. -rw-------. 1 mysql mysql 1676 3月 31 14:31 client-key.pem 14. [root@slave data]# chmod +r client-key.pem 15. [root@slave ~]# service mysqld restart 16. Shutting down MySQL.. [ 确定 ] 17. Starting MySQL. [ 确定 ]
2. 确认ssl开启成功
1. [root@slave ~]# mysql -uroot -p123 -e "show variables like '%ssl%'" 2. mysql: [Warning] Using a password on the command line interface can be insecure. 3. +-------------------------------------+-----------------+ 4. | Variable_name | Value | 5. +-------------------------------------+-----------------+ 6. | have_openssl | YES | 7. | have_ssl | YES | 8. | performance_schema_show_processlist | OFF | 9. | ssl_ca | ca.pem | 10. | ssl_capath | | 11. | ssl_cert | client-cert.pem | 12. | ssl_cipher | | 13. | ssl_crl | | 14. | ssl_crlpath | | 15. | ssl_key | client-key.pem | 16. +-------------------------------------+-----------------+
3. 在配置主从复制之前可以在从 mysql 上用 SSL 连接主服务器试试。
注意分清IP,8.2是master的IP,可以看到ssl协议Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
1. [root@slave ~]# cd /usr/local/mysql/data 2. [root@slave data]# mysql --ssl-ca=ca.pem --ssl-cert=client-cert.pem --ssl-key=client-key.pem -u rep -p123 -h 192.168.8.2 3. mysql: [Warning] Using a password on the command line interface can be insecure. 4. Welcome to the MySQL monitor. Commands end with ; or \g. 5. Your MySQL connection id is 3 6. Server version: 5.7.40-log MySQL Community Server (GPL) 7. 8. Copyright (c) 2000, 2022, Oracle and/or its affiliates. 9. 10. Oracle is a registered trademark of Oracle Corporation and/or its 11. affiliates. Other names may be trademarks of their respective 12. owners. 13. 14. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 15. 16. mysql> 17. mysql> \s 18. -------------- 19. mysql Ver 14.14 Distrib 5.7.40, for linux-glibc2.12 (x86_64) using EditLine wrapper 20. 21. Connection id: 3 22. Current database: 23. Current user: rep@192.168.8.3 24. SSL: Cipher in use is ECDHE-RSA-AES128-GCM-SHA256 25. Current pager: stdout 26. Using outfile: '' 27. Using delimiter: ; 28. Server version: 5.7.40-log MySQL Community Server (GPL) 29. Protocol version: 10 30. Connection: 192.168.8.2 via TCP/IP 31. Server characterset: latin1 32. Db characterset: latin1 33. Client characterset: utf8 34. Conn. characterset: utf8 35. TCP port: 3306 36. Uptime: 22 min 19 sec 37. 38. Threads: 1 Questions: 8 Slow queries: 0 Opens: 109 Flush tables: 1 Open tables: 102 Queries per second avg: 0.005 39. --------------
- 登录slave服务器,配置主从 replicate
记得先退出连接,登录slave的mysql服务。
1. mysql> exit 2. Bye 3. [root@slave data]# mysql -uroot -p123 4. #省略部分登录信息 5. mysql> change master to 6. -> master_host='192.168.8.2', #masterIP 7. -> master_user='rep', #master用户 8. -> master_password='123', #master密码 9. -> master_log_file='mysql-bin.000001', #master二进制日志文件 10. -> master_log_pos=154, #master位置 11. -> master_ssl=1, #masterssl 12. -> master_ssl_cert='client-cert.pem', 13. -> master_ssl_key='client-key.pem', 14. -> master_ssl_ca='ca.pem'; 15. Query OK, 0 rows affected, 2 warnings (0.07 sec) 16. 17. mysql> start slave; #启用从 18. Query OK, 0 rows affected (0.02 sec)
确认启用成功。
测试SSL主从复制
1. 登录master,写入一些数据
1. [root@master ~]# mysql -uroot -p123 2. #省略部分内容 3. mysql> create database bbs; 4. Query OK, 1 row affected (0.01 sec) 5. 6. mysql> use bbs; 7. Database changed 8. mysql> create table tb1(id int, 9. -> name varchar(20)); 10. Query OK, 0 rows affected (0.02 sec) 11. 12. mysql> insert into tb1 values(1,'z3'); 13. Query OK, 1 row affected (0.02 sec)
2. 登录slave,查看数据
1. [root@slave ~]# mysql -uroot -p123 2. #省略部分内容 3. mysql> select * from bbs.tb1; 4. +------+------+ 5. | id | name | 6. +------+------+ 7. | 1 | z3 | 8. +------+------+ 9. 1 row in set (0.01 sec)
最后可以查看到z3,主从成功。
结语:
SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议。复制默认是明文进行传输的,通过SSL加密可以大大提高数据的安全性。