layout: post
title: Mysql数据复制—-主从复制、级联复制、半同步复制、主主复制、复制过滤器、主从复制ssl加密
date: 2018-02-27
tags: [“Mariadb”,”软件服务”]


一、Mysql复制的概念

MySQL主从复制是其最重要的功能之一。主从复制是指一台服务器充当主数据库服务器,另一台或多台服务器充当从数据库服务器,主服务器中的数据自动复制到从服务器之中。对于多级复制,数据库服务器即可充当主机,也可充当从机。MySQL主从复制的基础是主服务器对数据库修改记录二进制日志,从服务器通过主服务器的二进制日志自动执行更新。

二、Mysql主从复制的原理

主从复制线程:

主节点:

dump Thread:为每个Slave的I/O Thread启动一个dump线程,用于向其发送binary log events

从节点:

I/O Thread:向Master请求二进制日志事件,并保存于中继日志中
SQL Thread:从中继日志中读取日志事件,在本地完成重放

跟复制功能相关的文件:

master.info:用于保存slave连接至master时的相关信息,例如账号、密码、服务器地址等
relay-log.info:保存在当前slave节点上已经复制的当前二进制日志和本地replay log日志的对应关系

主从复制的特点:

异步复制: 主从数据不一致比较常见,但是主服务器响应速度块
同步复制:主从数据不一致的数据比较少,主服务器响应速度慢

制需要考虑二进制日志事件记录格式:

STATEMENT
ROW
MIXED

 

三、Mysql主从复制的架构

复制架构:

Master/Slave, Master/Master
环状复制
一主多从
从服务器还可以再有从服务器
一从多主:适用于多个不同数据库

 

四、演示mysql的复制环境配置

1、需要3台Linux服务器(Centos7系统,10.2.版本mariadb)
2、服务器ip配置:

A:172.18.30.1(主)
B:172.18.30.2(1从)
C:172.18.30.3(2从)

五、配置主从之前的预数据处理

1、二进制日志记录格式设置

  1. show variables like '%binlog_format%';

二进制日志的格式有三种:

基于”语句”记录:statement,记录语句,默认模式
基于”行”记录:row,记录数据,日志量较大
混合模式:mixed, 让系统自行判定该基于哪种方式进行

可以在配置文件中设置

  1. vim /etc/my.cnf.d/server.cnf
  2.  
  3. [mysqld]下面添加
  4. binlog_format=
  2、为主服务器做备份并恢复到从服务器上 不论做哪种数据复制,在安装好我们slave-mysql之后,我们首先需要将主server端的数据做一个完整备份,并将其恢复到从服务器,并记录binlog的position,当然,如果主从服务器都是新安装的没有数据的服务器,则不需要此步骤了。 数据库安装的步骤这里不再演示 在A机器上进行操作:
  1. mysqldump -A -F --master-data=1 --single-transaction > /backup/mysql-`date +%F_%T`.sql #--master-data=1 该选项会记录,当前binlog的position
  2.  
  3. scp /backup/mysql-2018-02-27_16:15:02.sql 172.18.30.2:/backup/ #将A机器的备份文件复制到B机器
  4. scp /backup/mysql-2018-02-27_16:15:02.sql 172.18.30.3:/backup/ #将A机器的备份文件复制到C机器

六、配置主从复制

主从复制需要最少两台机器,我们这里只使用AB两台机器来做

1、登录A机器,并做配置

  1. 修改mysql配置文件
  2. vim /etc/my.cnf.d/server.cnf
  3.  
  4. [mysqld] 下添加如下选项
  5. innodb_file_per_table
  6. log-bin
  7. server_id=1
  8.  
  9. 登录数据库并做配置
  10.  
  11. [root@localhost backup]# mysql
  12. MariaDB [(none)]> set global server_id=1; #添加server编号
  13. MariaDB [(none)]> grant replication slave on *.* to repluser@'172.18.30.2' identified by '123123'; #添加主从复制用户
  14. MariaDB [(none)]> flush privileges;

2、登录B机器,并做配置

修改B机器的配置文件

  1. vim /etc/my.cnf.d/server.cnf
  2.  
  3. [mysqld] 下添加如下选项
  4. innodb_file_per_table
  5. read-only
  6. server_id=2

启动B机器,并使用A机器的备份恢复完整数据

  1. MariaDB [test]> source /backup/mysql-2018-02-27_16:15:02.sql;
  2. MariaDB [(none)]> CHANGE MASTER TO
  3. -> MASTER_HOST='172.18.30.1',
  4. -> MASTER_USER='repluser',
  5. -> MASTER_PASSWORD='123123'

开启B机器的slave

  1. MariaDB [test]> show slave status \G
  2. *************************** 1. row ***************************
  3. Slave_IO_State:
  4. Master_Host: 172.18.30.1
  5. Master_User: repluser
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: localhost-bin.000003 #完全备份binlog的位置
  9. Read_Master_Log_Pos: 393 #完全备份binlog的position
  10. Relay_Log_File: localhost-relay-bin.000001
  11. Relay_Log_Pos: 4
  12. Relay_Master_Log_File: localhost-bin.000003
  13. Slave_IO_Running: No
  14. Slave_SQL_Running: No
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB:
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 393
  25. Relay_Log_Space: 256
  26. Until_Condition: None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: NULL
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 1
  43. Master_SSL_Crl:
  44. Master_SSL_Crlpath:
  45. Using_Gtid: No
  46. Gtid_IO_Pos:
  47. Replicate_Do_Domain_Ids:
  48. Replicate_Ignore_Domain_Ids:
  49. Parallel_Mode: conservative
  50. SQL_Delay: 0
  51. SQL_Remaining_Delay: NULL
  52. Slave_SQL_Running_State:
  53. 1 row in set (0.01 sec)
  1. MariaDB [test]> start slave;
  2. MariaDB [test]> show slave status \G
  3. *************************** 1. row ***************************
  4. Slave_IO_State: Waiting for master to send event
  5. Master_Host: 172.18.30.1
  6. Master_User: repluser
  7. Master_Port: 3306
  8. Connect_Retry: 60
  9. Master_Log_File: localhost-bin.000003
  10. Read_Master_Log_Pos: 714
  11. Relay_Log_File: localhost-relay-bin.000002
  12. Relay_Log_Pos: 880
  13. Relay_Master_Log_File: localhost-bin.000003
  14. Slave_IO_Running: Yes #线程已启动
  15. Slave_SQL_Running: Yes #线程已启动
  16. Replicate_Do_DB:
  17. Replicate_Ignore_DB:
  18. Replicate_Do_Table:
  19. Replicate_Ignore_Table:
  20. Replicate_Wild_Do_Table:
  21. Replicate_Wild_Ignore_Table:
  22. Last_Errno: 0
  23. Last_Error:
  24. Skip_Counter: 0
  25. Exec_Master_Log_Pos: 714
  26. Relay_Log_Space: 1193
  27. Until_Condition: None
  28. Until_Log_File:
  29. Until_Log_Pos: 0
  30. Master_SSL_Allowed: No
  31. Master_SSL_CA_File:
  32. Master_SSL_CA_Path:
  33. Master_SSL_Cert:
  34. Master_SSL_Cipher:
  35. Master_SSL_Key:
  36. Seconds_Behind_Master: 0
  37. Master_SSL_Verify_Server_Cert: No
  38. Last_IO_Errno: 0
  39. Last_IO_Error:
  40. Last_SQL_Errno: 0
  41. Last_SQL_Error:
  42. Replicate_Ignore_Server_Ids:
  43. Master_Server_Id: 1
  44. Master_SSL_Crl:
  45. Master_SSL_Crlpath:
  46. Using_Gtid: No
  47. Gtid_IO_Pos:
  48. Replicate_Do_Domain_Ids:
  49. Replicate_Ignore_Domain_Ids:
  50. Parallel_Mode: conservative
  51. SQL_Delay: 0
  52. SQL_Remaining_Delay: NULL
  53. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  54. 1 row in set (0.00 sec)

3、在A机器修改数据,查看B机器是否同步成功

登录A机器插入一条数据

  1. [root@localhost backup]# mysql -e "insert into test.test (runoob_title,runoob_author,submission_date) values ('1122','2233',now());"

登录B机器查看是否同步成功

  1. [root@localhost backup]# mysql
  2. MariaDB [(none)]> use test;
  3. MariaDB [test]> select * from test;
  4. +-----------+--------------+---------------+-----------------+
  5. ' runoob_id ' runoob_title ' runoob_author ' submission_date '
  6. +-----------+--------------+---------------+-----------------+
  7. ' 1 ' test1 ' stuff ' 2018-02-23 '
  8. ' 2 ' test2 ' stuff ' 2018-02-23 '
  9. ' 3 ' test3 ' Goodluck ' 2018-02-23 '
  10. ' 4 ' Thanks a lot ' sun ' 2018-02-23 '
  11. ' 5 ' ok ' ook ' 2018-02-26 '
  12. ' 6 ' 1122 ' 2233 ' 2018-02-27 '
  13. +-----------+--------------+---------------+-----------------+
  14. 6 rows in set (0.00 sec)
  15.  
  16. MariaDB [test]>

同步完成

七、基于主从复制的级联复制

我们使用ABC三台主机来做实验

其表现形式:

A(主)<—B(辅主)<—C(辅)

也就是说,B从A上复制数据,C从B上复制数据

基于上个实验主从复制,我们只需要在B机器上更改一些配置,在C机器上配置从B机器复制数据

1、配置B机器的配置

  1. [root@localhost backup]# vim /etc/my.cnf.d/server.cnf
  2.  
  3. 删除read_only
  4.  
  5. 在[mysqld] 下添加
  6. log_slave_updates
  7. log-bin

重启B机器上的服务

  1. [root@localhost backup]# systemctl restart mariadb

登录mysql添加授权用户

  1. [root@localhost backup]# mysql
  2. MariaDB [(none)]> grant all on *.* to 'repluser'@'172.18.30.3' identified by '123123';
  3. MariaDB [(none)]> flush privileges;

备份B机器的数据

  1. mysqldump -A -F --master-data=1 --single-transaction > /backup/mysql-`date +%F_%T`.sql

2、配置C机器

  1. innodb_file_per_table
  2. read-only
  3. server-id=3

登录C机器恢复从B机器拷贝的数据

  1. [root@localhost yum.repos.d]# systemctl start mariadb
  2. [root@localhost yum.repos.d]# scp 172.18.30.2:/backup/mysql-2018-02-27_19:44:09.sql /backup/
  3. [root@localhost yum.repos.d]# mysql
  4. MariaDB [test]> source /backup/mysql-2018-02-27_19:44:09.sql

开启数据同步

  1. MariaDB [test]> CHANGE MASTER TO MASTER_HOST='172.18.30.2', MASTER_USER='repluser', MASTER_PASSWORD='123123';
  2.  
  3. MariaDB [(none)]> show slave status\G
  4. *************************** 1. row ***************************
  5. Slave_IO_State:
  6. Master_Host: 172.18.30.2
  7. Master_User: repluser
  8. Master_Port: 3306
  9. Connect_Retry: 60
  10. Master_Log_File:
  11. Read_Master_Log_Pos: 4
  12. Relay_Log_File: localhost-relay-bin.000001
  13. Relay_Log_Pos: 4
  14. Relay_Master_Log_File:
  15. Slave_IO_Running: No
  16. Slave_SQL_Running: No
  17. Replicate_Do_DB:
  18. Replicate_Ignore_DB:
  19. Replicate_Do_Table:
  20. Replicate_Ignore_Table:
  21. Replicate_Wild_Do_Table:
  22. Replicate_Wild_Ignore_Table:
  23. Last_Errno: 0
  24. Last_Error:
  25. Skip_Counter: 0
  26. Exec_Master_Log_Pos: 0
  27. Relay_Log_Space: 256
  28. Until_Condition: None
  29. Until_Log_File:
  30. Until_Log_Pos: 0
  31. Master_SSL_Allowed: No
  32. Master_SSL_CA_File:
  33. Master_SSL_CA_Path:
  34. Master_SSL_Cert:
  35. Master_SSL_Cipher:
  36. Master_SSL_Key:
  37. Seconds_Behind_Master: NULL
  38. Master_SSL_Verify_Server_Cert: No
  39. Last_IO_Errno: 0
  40. Last_IO_Error:
  41. Last_SQL_Errno: 0
  42. Last_SQL_Error:
  43. Replicate_Ignore_Server_Ids:
  44. Master_Server_Id: 0
  45. Master_SSL_Crl:
  46. Master_SSL_Crlpath:
  47. Using_Gtid: No
  48. Gtid_IO_Pos:
  49. Replicate_Do_Domain_Ids:
  50. Replicate_Ignore_Domain_Ids:
  51. Parallel_Mode: conservative
  52. SQL_Delay: 0
  53. SQL_Remaining_Delay: NULL
  54. Slave_SQL_Running_State:
  55. 1 row in set (0.00 sec)
  1. MariaDB [(none)]> start slave;
  2. MariaDB [(none)]> show slave status\G
  3. *************************** 1. row ***************************
  4. Slave_IO_State: Waiting for master to send event
  5. Master_Host: 172.18.30.2
  6. Master_User: repluser
  7. Master_Port: 3306
  8. Connect_Retry: 60
  9. Master_Log_File: localhost-bin.000009
  10. Read_Master_Log_Pos: 409
  11. Relay_Log_File: localhost-relay-bin.000015
  12. Relay_Log_Pos: 712
  13. Relay_Master_Log_File: localhost-bin.000009
  14. Slave_IO_Running: Yes #以开启传输
  15. Slave_SQL_Running: Yes #以开启传输
  16. Replicate_Do_DB:
  17. Replicate_Ignore_DB:
  18. Replicate_Do_Table:
  19. Replicate_Ignore_Table:
  20. Replicate_Wild_Do_Table:
  21. Replicate_Wild_Ignore_Table:
  22. Last_Errno: 0
  23. Last_Error:
  24. Skip_Counter: 0
  25. Exec_Master_Log_Pos: 409
  26. Relay_Log_Space: 1076
  27. Until_Condition: None
  28. Until_Log_File:
  29. Until_Log_Pos: 0
  30. Master_SSL_Allowed: No
  31. Master_SSL_CA_File:
  32. Master_SSL_CA_Path:
  33. Master_SSL_Cert:
  34. Master_SSL_Cipher:
  35. Master_SSL_Key:
  36. Seconds_Behind_Master: 0
  37. Master_SSL_Verify_Server_Cert: No
  38. Last_IO_Errno: 0
  39. Last_IO_Error:
  40. Last_SQL_Errno: 0
  41. Last_SQL_Error:
  42. Replicate_Ignore_Server_Ids:
  43. Master_Server_Id: 2
  44. Master_SSL_Crl:
  45. Master_SSL_Crlpath:
  46. Using_Gtid: No
  47. Gtid_IO_Pos:
  48. Replicate_Do_Domain_Ids:
  49. Replicate_Ignore_Domain_Ids:
  50. Parallel_Mode: conservative
  51. SQL_Delay: 0
  52. SQL_Remaining_Delay: NULL
  53. Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  54. 1 row in set (0.00 sec)

级联复制完成

八、基于主从复制的半同步复制

默认情况下,MySQL的复制功能是异步的,异步复制可以提供最佳的性能,主库把binlog日志发送给从库即结束,并不验证从库是否接收完毕。这意味着当主服务器或从服务器端发生
故障时,有可能从服务器没有接收到主服务器发送过来的binlog日志,这就会造成主服务器和从服务器的数据不一致,甚至在恢复时造成数据的丢失。

1、主服务器配置(A机器)

  1. MariaDB [test]> install plugin rpl_semi_sync_master SONAME 'semisync_master.so';
  2. MariaDB [test]> set global rpl_semi_sync_master_enabled=1;
  3. MariaDB [test]> show variables like '%rpl%'
  4. +------------------------------------+--------------+
  5. ' Variable_name ' Value '
  6. +------------------------------------+--------------+
  7. ' rpl_semi_sync_master_enabled ' ON '
  8. ' rpl_semi_sync_master_timeout ' 10000 '
  9. ' rpl_semi_sync_master_trace_level ' 32 '
  10. ' rpl_semi_sync_master_wait_no_slave ' ON '
  11. ' rpl_semi_sync_master_wait_point ' AFTER_COMMIT '
  12. +------------------------------------+--------------+
  13. MariaDB [test]> show status like '%semi%'
  14. +--------------------------------------------+-------+
  15. ' Variable_name ' Value '
  16. +--------------------------------------------+-------+
  17. ' Rpl_semi_sync_master_clients ' 0 '
  18. ' Rpl_semi_sync_master_net_avg_wait_time ' 0 '
  19. ' Rpl_semi_sync_master_net_wait_time ' 0 '
  20. ' Rpl_semi_sync_master_net_waits ' 0 '
  21. ' Rpl_semi_sync_master_no_times ' 0 '
  22. ' Rpl_semi_sync_master_no_tx ' 0 '
  23. ' Rpl_semi_sync_master_status ' ON '
  24. ' Rpl_semi_sync_master_timefunc_failures ' 0 '
  25. ' Rpl_semi_sync_master_tx_avg_wait_time ' 0 '
  26. ' Rpl_semi_sync_master_tx_wait_time ' 0 '
  27. ' Rpl_semi_sync_master_tx_waits ' 0 '
  28. ' Rpl_semi_sync_master_wait_pos_backtraverse ' 0 '
  29. ' Rpl_semi_sync_master_wait_sessions ' 0 '
  30. ' Rpl_semi_sync_master_yes_tx ' 0 '
  31. +--------------------------------------------+-------+

2、从服务器配置(B机器)

  1. MariaDB [(none)]> install plugin rpl_semi_sync_slave SONAME 'semisync_slave.so';
  2. MariaDB [(none)]> set global rpl_semi_sync_slave_enabled=1;
  3. MariaDB [(none)]> show variables like '%rpl%';
  4. +---------------------------------+-------+
  5. ' Variable_name ' Value '
  6. +---------------------------------+-------+
  7. ' rpl_semi_sync_slave_enabled ' ON '
  8. ' rpl_semi_sync_slave_trace_level ' 32 '
  9. +---------------------------------+-------+
  10.  
  11. MariaDB [(none)]> show status like '%semi%';
  12. +----------------------------+-------+
  13. ' Variable_name ' Value '
  14. +----------------------------+-------+
  15. ' Rpl_semi_sync_slave_status ' OFF '
  16. +----------------------------+-------+

我们看到”Rpl_semi_sync_slave_status”的状态是”OFF’,我们需要停止之前的同步,并重启开启

  1. MariaDB [(none)]> stop slave;
  2. MariaDB [(none)]> start slave;
  3.  
  4. MariaDB [(none)]> show status like '%semi%';
  5. +----------------------------+-------+
  6. ' Variable_name ' Value '
  7. +----------------------------+-------+
  8. ' Rpl_semi_sync_slave_status ' ON '
  9. +----------------------------+-------+

OK半同步配置完成

九、基于主从复制的双主复制

我们使用AB两台主机来做实验

所谓双主复制,即两台server互为主备

其实现原理就是互相同步数据,由于表中数据有些字段是会自动递增的,所以主主复制模式需要配置自动递增数据的步长。

在真正的生产环境中不推荐使用主主复制,因为会产生数据不一致的问题。

1、在A机器上配置(删除之前添加的选项,重新配置)

  1. vim /etc/my.cnf.d/server.cnf
  2.  
  3. [mysqld] 下添加如下选项
  4. log-bin
  5. server_id=1
  6. auto_increment_offset=1 #递增字段起始的偏移量
  7. auto_increment_increment=2 #递增字段的不长
  8. innodb_file_per_table
  9.  
  10. systemctl restart mariadb

2、在B机器上配置(删除之前添加的选项,重新配置)

  1. vim /etc/my.cnf.d/server.cnf
  2. [mysqld] 下添加如下选项
  3. log-bin
  4. server_id=2
  5. auto_increment_offset=2 #递增字段其实偏移量
  6. auto_increment_increment=2 #递增字段递增步长
  7. innodb_file_per_table
  8.  
  9. systemctl restart mariadb

3、因为现在两台机器已经互为主备,并且都已经有数据了,如果我们在这种情况下做双主,那么需要确定各自的binlog和position才能开启同步,否则会出现数据不一致的问题(再加一点,隔离外界环境,禁止外界增删改数据)

A机器的binlog和position:

  1. [root@localhost yum.repos.d]# mysql -e 'show master logs;'
  2. +----------------------+-----------+
  3. ' Log_name ' File_size '
  4. +----------------------+-----------+
  5. ' localhost-bin.000001 ' 510 '
  6. ' localhost-bin.000002 ' 1532 '
  7. ' localhost-bin.000003 ' 989 '
  8. +----------------------+-----------+

B机器的binlog和position

  1. [root@localhost backup]# mysql -e 'show master logs;'
  2. +----------------------+-----------+
  3. ' Log_name ' File_size '
  4. +----------------------+-----------+
  5. ' localhost-bin.000001 ' 1038982 '
  6. ' localhost-bin.000002 ' 444 '
  7. ' localhost-bin.000003 ' 444 '
  8. ' localhost-bin.000004 ' 444 '
  9. ' localhost-bin.000005 ' 1558861 '
  10. ' localhost-bin.000006 ' 385 '
  11. ' localhost-bin.000007 ' 385 '
  12. ' localhost-bin.000008 ' 722 '
  13. ' localhost-bin.000009 ' 409 '
  14. +----------------------+-----------+
  15. [root@localhost backup]#

4、在A机器上设置同步

  1. MariaDB [(none)]> grant replication slave on *.* to repluser@'172.18.30.2' identified by '123123';
  2. MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.18.30.2',MASTER_USER='repluser',MASTER_PASSWORD='123123',MASTER_LOG_FILE='localhost-bin.000009',MASTER_LOG_POS=409;

5、在B机器上设置同步

  1. MariaDB [(none)]> grant replication slave on *.* to repluser@'172.18.30.1' identified by '123123';
  2. MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.18.30.1',MASTER_USER='repluser',MASTER_PASSWORD='123123',MASTER_LOG_FILE='localhost-bin.000003',MASTER_LOG_POS=989;

6、两台机器同时开启同步

  1. MariaDB [(none)]> start slave;

7、测试

在A机器插入一条数据

  1. MariaDB [(none)]> insert into test.test (runoob_title,runoob_author,submission_date) values ('A_a','A_a',now());

在B机器插入一条数据

  1. MariaDB [(none)]> insert into test.test (runoob_title,runoob_author,submission_date) values ('B_b','B_b',now());

在A机器上查询数据

  1. MariaDB [test]> select * from test.test;
  2. +-----------+--------------+---------------+-----------------+
  3. ' runoob_id ' runoob_title ' runoob_author ' submission_date '
  4. +-----------+--------------+---------------+-----------------+
  5. ' 1 ' test1 ' stuff ' 2018-02-23 '
  6. ' 2 ' test2 ' stuff ' 2018-02-23 '
  7. ' 3 ' test3 ' Goodluck ' 2018-02-23 '
  8. ' 4 ' Thanks a lot ' sun ' 2018-02-23 '
  9. ' 5 ' ok ' ook ' 2018-02-26 '
  10. ' 6 ' 1122 ' 2233 ' 2018-02-27 '
  11. ' 7 ' A_a ' A_a ' 2018-02-27 '
  12. ' 8 ' B_b ' B_b ' 2018-02-27 '
  13. +-----------+--------------+---------------+-----------------+

在B机器上查询数据

  1. MariaDB [(none)]> select * from test.test;
  2. +-----------+--------------+---------------+-----------------+
  3. ' runoob_id ' runoob_title ' runoob_author ' submission_date '
  4. +-----------+--------------+---------------+-----------------+
  5. ' 1 ' test1 ' stuff ' 2018-02-23 '
  6. ' 2 ' test2 ' stuff ' 2018-02-23 '
  7. ' 3 ' test3 ' Goodluck ' 2018-02-23 '
  8. ' 4 ' Thanks a lot ' sun ' 2018-02-23 '
  9. ' 5 ' ok ' ook ' 2018-02-26 '
  10. ' 6 ' 1122 ' 2233 ' 2018-02-27 '
  11. ' 7 ' A_a ' A_a ' 2018-02-27 '
  12. ' 8 ' B_b ' B_b ' 2018-02-27 '
  13. +-----------+--------------+---------------+-----------------+

实验完成

十、复制过滤器

复制过滤器让从节点仅复制指定的数据库,或指定数据库的指定表

两种实现方式:

1、主服务器仅向二进制日志中记录与特定数据库(特定表)相关的事件

问题:时间还原无法实现;不建议使用
binlog_do_db= 数据库白名单列表,用逗号分隔
binlog_ignore_db= 数据库黑名单列表,和前项不要同时使用

2、从服务器SQL_THREAD在replay中继日志中的事件时,仅读取与特定数据库(特定表)相关的事件并应用于本地

问题:会造成网络及磁盘IO浪费

复制过滤器从服务器上的相关设置
replicate_do_db= 指定复制库的白名单
replicate_ignore_db= 指定复制库黑名单
replicate_do_table= 指定复制表的白名单
replicate_ignore_table= 指定复制表的黑名单
replicate_wild_do_table= foo%.bar% 支持通配符
replicate_wild_ignore_table=

复制过滤器在这里不再演示

十一、主从复制SSL加密

通信加密必然用到证书

参考文献:https://mariadb.com/kb/en/library/replication-with-secure-connections/

在默认的主从复制过程或远程连接到MySQL/MariaDB所有的链接通信中的数据都是明文的,外网里访问数据或则复制,存在安全隐患。通过SSL/TLS加密的方式进行复制的方法,来进一步提高数据的安全性

1、CA的配置

  1. mkdir /etc/my.cnf.d/ssl
  2. cd /etc/my.cnf.d/ssl
  3. openssl genrsa 2048 > cakey.pem
  4. chmod 600 cakey.pem
  5. openssl req -new -x509 -key cakey.pem -days 3650 -out cacert.pem #自签名证书
  1. openssl req -newkey rsa:1024 -days 365 -nodes -keyout master.key > master.csr #生成master的key和请求文件
  2. openssl x509 -req -in master.csr -CA cacert.pem -CAkey cakey.pem -set_serial 01 > master.crt #签名证书
  3. openssl req -newkey rsa:1024 -days 365 -nodes -keyout slave.key > slave.csr #生成slave的key和请求文件
  4. openssl x509 -req -in slave.csr -CA cacert.pem -CAkey cakey.pem -set_serial 02 > slave.crt #签名证书

将生成好的证书复制到每台机器

  1. scp -r /etc/my.cnf.d/ssl/ 172.18.30.1:/etc/my.cnf.d/
  2. scp -r /etc/my.cnf.d/ssl/ 172.18.30.2:/etc/my.cnf.d/

2、Master端的配置(A机器)

  1. vim /etc/my.cnf.d/server.cnf
  2.  
  3. 在主从配置的基础上添加如下设置
  4.  
  5. ssl
  6. ssl-ca=/etc/my.cnf.d/ssl/cacert.pem
  7. ssl-cert=/etc/my.cnf.d/ssl/master.crt
  8. ssl-key=/etc/my.cnf.d/ssl/master.key
  9.  
  10. 重启服务
  11. systemctl restart mariadb

登录maraidb进行配置用户权限

  1. MariaDB [(none)]> grant replication slave on *.* to repluser@'172.18.30.2' identified by '123123' require ssl;
  2. MariaDB [(none)]> flush privileges;

3、Slave端的配置(B机器)

  1. vim /etc/my.cnf.d/server.cnf
  2.  
  3. 在主从配置的基础上添加如下选项
  4.  
  5. ssl
  6.  
  7. 重启服务
  8. systemctl restart mariadb

登录mariadb进行配置

  1. [root@localhost backup]# mysql
    MariaDB [(none)]> stop slave;

  2. MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=’172.18.30.1’, MASTER_USER=’repluser’, MASTER_PASSWORD=’123123’, MASTER_SSL=1, MASTER_SSL_CA = ‘/etc/my.cnf.d/ssl/cacert.pem’, MASTER_SSL_CERT = ‘/etc/my.cnf.d/ssl/slave.crt’, MASTER_SSL_KEY = ‘/etc/my.cnf.d/ssl/slave.key’;

  3. MariaDB [(none)]> start slave;


 

 

文档更新时间: 2020-05-25 12:16   作者:张尚