MySQL/MariaDB数据库的复制过滤器

                       作者:尹正杰 

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.复制过滤器概述

1>.复制器过滤器功能

  1.   让从节点仅复制指定的数据库,或指定数据库的指定表。

2>.两种实现方式

  1. 方案一:
      主服务器仅向二进制日志中记录与特定数据库相关的事件
  2.   此项和binlog_format相关,详情请参考官网说明:https://mariadb.com/kb/en/library/mysqld-options/#-binlog-ignore-db
  3.     binlog_do_db = 数据库白名单列表,多个数据库需多行实现
  4.     binlog_ignore_db = 数据库黑名单列表
  5.   问题:
        基于二进制还原将无法实现(因为记录的日志只有部分数据库信息,可能存在部分数据无法还原的现象);不建议使用
  6.  
  7. 方案二:
      从服务器SQL_THREADreplay中继日志中的事件时,仅读取与特定数据库(特定表)相关的事件并应用于本地
  8.   问题:
        由于将master节点的所有数据都copyslave节点,但SQL_THREAD线程仅读取与特定数据库(特定表)相关的事件并应用于本地,也就是说部分数据传过来也不去使用,这会造成网络及磁盘IO浪费。

3>.从服务器上的复制过滤器相关变量

  1. MariaDB [(none)]> SHOW VARIABLES LIKE '%replicate%';
  2. +----------------------------------+-----------+
  3. | Variable_name | Value |
  4. +----------------------------------+-----------+
  5. | replicate_annotate_row_events | OFF |
  6. | replicate_do_db | |
  7. | replicate_do_table | |
  8. | replicate_events_marked_for_skip | replicate |
  9. | replicate_ignore_db | |
  10. | replicate_ignore_table | |
  11. | replicate_wild_do_table | |
  12. | replicate_wild_ignore_table | |
  13. +----------------------------------+-----------+
  14. rows in set (0.00 sec)
  15.  
  16. MariaDB [(none)]>

MariaDB [(none)]> SHOW VARIABLES LIKE '%replicate%';

  1. replicate_do_db=
  2. 指定复制库的白名单
  3. replicate_ignore_db=
  4. 指定复制库黑名单
  5. replicate_do_table=
  6. 指定复制表的白名单
  7. replicate_ignore_table=
  8. 指定复制表的黑名单
  9. replicate_wild_do_table= foo%.bar%
  10. 解决跨库更新的问题支持通配符
  11. replicate_wild_ignore_table=
      同上

二.复制过滤器在slave节点定义白名单案例

1>.master服务器配置

  1. [root@node102.yinzhengjie.org.cn ~]# cat /etc/my.cnf
  2. [mysqld]
  3. server-id =
  4. binlog_format = row
  5. log_bin = /data/mysql/logbin/master-
  6. character-set-server = utf8mb4
  7. default_storage_engine = InnoDB
  8. datadir = /var/lib/mysql
  9. socket = /var/lib/mysql/mysql.sock
  10.  
  11. [mysqld_safe]
  12. log-error = /var/log/mariadb/mariadb.log
  13. pid-file = /var/run/mariadb/mariadb.pid
  14.  
  15. !includedir /etc/my.cnf.d
  16. [root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# cat /etc/my.cnf    #查看配置文件

  1. [root@node102.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
  2. total
  3. [root@node102.yinzhengjie.org.cn ~]#
  4. [root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
  5. total
  6. [root@node102.yinzhengjie.org.cn ~]#
  7. [root@node102.yinzhengjie.org.cn ~]# systemctl start mariadb
  8. [root@node102.yinzhengjie.org.cn ~]#
  9. [root@node102.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
  10. total
  11. -rw-rw---- mysql mysql Nov : aria_log.
  12. -rw-rw---- mysql mysql Nov : aria_log_control
  13. -rw-rw---- mysql mysql Nov : ibdata1
  14. -rw-rw---- mysql mysql Nov : ib_logfile0
  15. -rw-rw---- mysql mysql Nov : ib_logfile1
  16. drwx------ mysql mysql Nov : mysql
  17. srwxrwxrwx mysql mysql Nov : mysql.sock
  18. drwx------ mysql mysql Nov : performance_schema
  19. drwx------ mysql mysql Nov : test
  20. [root@node102.yinzhengjie.org.cn ~]#
  21. [root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
  22. total
  23. -rw-rw---- mysql mysql Nov : master-102.000001
  24. -rw-rw---- mysql mysql Nov : master-102.000002
  25. -rw-rw---- mysql mysql Nov : master-102.000003
  26. -rw-rw---- mysql mysql Nov : master-.index
  27. [root@node102.yinzhengjie.org.cn ~]#
  28. [root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# systemctl start mariadb  #启动数据库实例

  1. [root@node102.yinzhengjie.org.cn ~]# mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is
  4. Server version: 5.5.-MariaDB MariaDB Server
  5.  
  6. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]>
  11. MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
  12. +------+----------------------------+----------+
  13. | user | host | password |
  14. +------+----------------------------+----------+
  15. | root | localhost | |
  16. | root | node102.yinzhengjie.org.cn | |
  17. | root | 127.0.0.1 | |
  18. | root | :: | |
  19. | | localhost | |
  20. | | node102.yinzhengjie.org.cn | |
  21. +------+----------------------------+----------+
  22. rows in set (0.00 sec)
  23.  
  24. MariaDB [(none)]>
  25. MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie';
  26. Query OK, rows affected (0.00 sec)
  27.  
  28. MariaDB [(none)]>
  29. MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
  30. +------+----------------------------+-------------------------------------------+
  31. | user | host | password |
  32. +------+----------------------------+-------------------------------------------+
  33. | root | localhost | |
  34. | root | node102.yinzhengjie.org.cn | |
  35. | root | 127.0.0.1 | |
  36. | root | :: | |
  37. | | localhost | |
  38. | | node102.yinzhengjie.org.cn | |
  39. | copy | 172.30.1.10% | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
  40. +------+----------------------------+-------------------------------------------+
  41. rows in set (0.00 sec)
  42.  
  43. MariaDB [(none)]>
  44. MariaDB [(none)]> QUIT
  45. Bye
  46. [root@node102.yinzhengjie.org.cn ~]#

在master节点上创建有复制权限的用户账号

  1. [root@node102.yinzhengjie.org.cn ~]# mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is
  4. Server version: 5.5.-MariaDB MariaDB Server
  5.  
  6. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]>
  11. MariaDB [(none)]> SHOW MASTER LOGS;
  12. +-------------------+-----------+
  13. | Log_name | File_size |
  14. +-------------------+-----------+
  15. | master-102.000001 | |
  16. | master-102.000002 | |
  17. | master-102.000003 | |
  18. +-------------------+-----------+
  19. rows in set (0.00 sec)
  20.  
  21. MariaDB [(none)]>
  22. MariaDB [(none)]> SHOW MASTER STATUS;
  23. +-------------------+----------+--------------+------------------+
  24. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  25. +-------------------+----------+--------------+------------------+
  26. | master-102.000003 | | | |
  27. +-------------------+----------+--------------+------------------+
  28. row in set (0.00 sec)
  29.  
  30. MariaDB [(none)]>
  31. MariaDB [(none)]> QUIT
  32. Bye
  33. [root@node102.yinzhengjie.org.cn ~]#

MariaDB [(none)]> SHOW MASTER STATUS;      #查看master当前二进制日志状态信息

2>.slave节点配置主从复制

  1. [root@node103.yinzhengjie.org.cn ~]# cat /etc/my.cnf      #查看配置文件
  2. [mysqld]
  3. server-id =
  4. binlog_format = row
  5. read-only = on
  6. replicate_do_db = devops
  7. relay_log = relay-log-
  8. relay_log_index = relay-log-.index
  9. character-set-server = utf8mb4
  10. default_storage_engine = InnoDB
  11. datadir = /var/lib/mysql
  12. socket = /var/lib/mysql/mysql.sock
  13.  
  14. [mysqld_safe]
  15. log-error = /var/log/mariadb/mariadb.log
  16. pid-file = /var/run/mariadb/mariadb.pid
  17.  
  18. !includedir /etc/my.cnf.d
  19. [root@node103.yinzhengjie.org.cn ~]#

[root@node103.yinzhengjie.org.cn ~]# cat /etc/my.cnf  #编辑配置文件,指定值复制devops这个数据库

  1. [root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
  2. total
  3. [root@node103.yinzhengjie.org.cn ~]#
  4. [root@node103.yinzhengjie.org.cn ~]# systemctl start mariadb
  5. [root@node103.yinzhengjie.org.cn ~]#
  6. [root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
  7. total
  8. -rw-rw---- mysql mysql Nov : aria_log.
  9. -rw-rw---- mysql mysql Nov : aria_log_control
  10. -rw-rw---- mysql mysql Nov : ibdata1
  11. -rw-rw---- mysql mysql Nov : ib_logfile0
  12. -rw-rw---- mysql mysql Nov : ib_logfile1
  13. drwx------ mysql mysql Nov : mysql
  14. srwxrwxrwx mysql mysql Nov : mysql.sock
  15. drwx------ mysql mysql Nov : performance_schema
  16. drwx------ mysql mysql Nov : test
  17. [root@node103.yinzhengjie.org.cn ~]#

[root@node103.yinzhengjie.org.cn ~]# systemctl start mariadb

  1. [root@node103.yinzhengjie.org.cn ~]# mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is
  4. Server version: 5.5.-MariaDB MariaDB Server
  5.  
  6. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]> CHANGE MASTER TO
  11. -> MASTER_HOST='172.30.1.102',
  12. -> MASTER_USER='copy',
  13. -> MASTER_PASSWORD='yinzhengjie',
  14. -> MASTER_PORT=,
  15. -> MASTER_LOG_FILE='master-102.000003',
  16. -> MASTER_LOG_POS=,
  17. -> MASTER_CONNECT_RETRY=;
  18. Query OK, rows affected (0.01 sec)
  19.  
  20. MariaDB [(none)]>
  21. MariaDB [(none)]> SHOW SLAVE STATUS\G
  22. *************************** . row ***************************
  23. Slave_IO_State:
  24. Master_Host: 172.30.1.102
  25. Master_User: copy
  26. Master_Port:
  27. Connect_Retry:
  28. Master_Log_File: master-102.000003
  29. Read_Master_Log_Pos:
  30. Relay_Log_File: relay-log-103.000001
  31. Relay_Log_Pos:
  32. Relay_Master_Log_File: master-102.000003
  33. Slave_IO_Running: No
  34. Slave_SQL_Running: No
  35. Replicate_Do_DB: devops
  36. Replicate_Ignore_DB:
  37. Replicate_Do_Table:
  38. Replicate_Ignore_Table:
  39. Replicate_Wild_Do_Table:
  40. Replicate_Wild_Ignore_Table:
  41. Last_Errno:
  42. Last_Error:
  43. Skip_Counter:
  44. Exec_Master_Log_Pos:
  45. Relay_Log_Space:
  46. Until_Condition: None
  47. Until_Log_File:
  48. Until_Log_Pos:
  49. Master_SSL_Allowed: No
  50. Master_SSL_CA_File:
  51. Master_SSL_CA_Path:
  52. Master_SSL_Cert:
  53. Master_SSL_Cipher:
  54. Master_SSL_Key:
  55. Seconds_Behind_Master: NULL
  56. Master_SSL_Verify_Server_Cert: No
  57. Last_IO_Errno:
  58. Last_IO_Error:
  59. Last_SQL_Errno:
  60. Last_SQL_Error:
  61. Replicate_Ignore_Server_Ids:
  62. Master_Server_Id:
  63. row in set (0.00 sec)
  64.  
  65. MariaDB [(none)]>
  66. MariaDB [(none)]> START SLAVE;
  67. Query OK, rows affected (0.00 sec)
  68.  
  69. MariaDB [(none)]>
  70. MariaDB [(none)]> SHOW SLAVE STATUS\G
  71. *************************** . row ***************************
  72. Slave_IO_State: Waiting for master to send event
  73. Master_Host: 172.30.1.102
  74. Master_User: copy
  75. Master_Port:
  76. Connect_Retry:
  77. Master_Log_File: master-102.000003
  78. Read_Master_Log_Pos:
  79. Relay_Log_File: relay-log-103.000002
  80. Relay_Log_Pos:
  81. Relay_Master_Log_File: master-102.000003
  82. Slave_IO_Running: Yes
  83. Slave_SQL_Running: Yes
  84. Replicate_Do_DB: devops
  85. Replicate_Ignore_DB:
  86. Replicate_Do_Table:
  87. Replicate_Ignore_Table:
  88. Replicate_Wild_Do_Table:
  89. Replicate_Wild_Ignore_Table:
  90. Last_Errno:
  91. Last_Error:
  92. Skip_Counter:
  93. Exec_Master_Log_Pos:
  94. Relay_Log_Space:
  95. Until_Condition: None
  96. Until_Log_File:
  97. Until_Log_Pos:
  98. Master_SSL_Allowed: No
  99. Master_SSL_CA_File:
  100. Master_SSL_CA_Path:
  101. Master_SSL_Cert:
  102. Master_SSL_Cipher:
  103. Master_SSL_Key:
  104. Seconds_Behind_Master:
  105. Master_SSL_Verify_Server_Cert: No
  106. Last_IO_Errno:
  107. Last_IO_Error:
  108. Last_SQL_Errno:
  109. Last_SQL_Error:
  110. Replicate_Ignore_Server_Ids:
  111. Master_Server_Id:
  112. row in set (0.00 sec)
  113.  
  114. MariaDB [(none)]>

slave配置主从复制详细过程

3>.验证数据库配置是否生效

  1. [root@node102.yinzhengjie.org.cn ~]# mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is
  4. Server version: 5.5.-MariaDB MariaDB Server
  5.  
  6. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]> SHOW DATABASES;
  11. +--------------------+
  12. | Database |
  13. +--------------------+
  14. | information_schema |
  15. | mysql |
  16. | performance_schema |
  17. | test |
  18. +--------------------+
  19. rows in set (0.00 sec)
  20.  
  21. MariaDB [(none)]>
  22. MariaDB [(none)]> CREATE DATABASE db1;
  23. Query OK, row affected (0.00 sec)
  24.  
  25. MariaDB [(none)]> CREATE DATABASE db2;
  26. Query OK, row affected (0.00 sec)
  27.  
  28. MariaDB [(none)]>
  29. MariaDB [(none)]> CREATE DATABASE db3;
  30. Query OK, row affected (0.00 sec)
  31.  
  32. MariaDB [(none)]>
  33. MariaDB [(none)]> CREATE DATABASE devops;
  34. Query OK, row affected (0.00 sec)
  35.  
  36. MariaDB [(none)]>
  37. MariaDB [(none)]> SHOW DATABASES;
  38. +--------------------+
  39. | Database |
  40. +--------------------+
  41. | information_schema |
  42. | db1 |
  43. | db2 |
  44. | db3 |
  45. | devops |
  46. | mysql |
  47. | performance_schema |
  48. | test |
  49. +--------------------+
  50. rows in set (0.00 sec)
  51.  
  52. MariaDB [(none)]>
  53. MariaDB [(none)]> USE devops
  54. Database changed
  55. MariaDB [devops]>
  56. MariaDB [devops]> CREATE TABLE students(id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR() NOT NULL,sex ENUM('bo
  57. y','girl') DEFAULT 'boy',age TINYINT UNSIGNED,mobile CHAR(11),address VARCHAR(50));Query OK, 0 rows affected (0.01 sec)
  58.  
  59. MariaDB [devops]>
  60. MariaDB [devops]> INSERT INTO students (name,age,mobile,address) VALUES ('Jason Yin',,,'beijing'),('Jay','',
  61. ,'Taiwan');Query OK, rows affected (0.01 sec)
  62. Records: Duplicates: Warnings:
  63.  
  64. MariaDB [devops]>
  65. MariaDB [devops]> SHOW TABLES;
  66. +------------------+
  67. | Tables_in_devops |
  68. +------------------+
  69. | students |
  70. +------------------+
  71. row in set (0.00 sec)
  72.  
  73. MariaDB [devops]>
  74. MariaDB [devops]> SELECT * FROM students;
  75. +----+-----------+------+------+--------+---------+
  76. | id | name | sex | age | mobile | address |
  77. +----+-----------+------+------+--------+---------+
  78. | | Jason Yin | boy | | | beijing |
  79. | | Jay | boy | | | Taiwan |
  80. +----+-----------+------+------+--------+---------+
  81. rows in set (0.00 sec)
  82.  
  83. MariaDB [devops]>
  84. MariaDB [devops]>

master节点创建多个测试数据库

  1. [root@node103.yinzhengjie.org.cn ~]# mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is
  4. Server version: 5.5.-MariaDB MariaDB Server
  5.  
  6. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]>
  11. MariaDB [(none)]> SHOW DATABASES;
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | information_schema |
  16. | devops |
  17. | mysql |
  18. | performance_schema |
  19. | test |
  20. +--------------------+
  21. rows in set (0.00 sec)
  22.  
  23. MariaDB [(none)]>
  24. MariaDB [(none)]> USE devops
  25. Reading table information for completion of table and column names
  26. You can turn off this feature to get a quicker startup with -A
  27.  
  28. Database changed
  29. MariaDB [devops]> SHOW TABLES;
  30. +------------------+
  31. | Tables_in_devops |
  32. +------------------+
  33. | students |
  34. +------------------+
  35. row in set (0.00 sec)
  36.  
  37. MariaDB [devops]>
  38. MariaDB [devops]> SELECT * FROM students;
  39. +----+-----------+------+------+--------+---------+
  40. | id | name | sex | age | mobile | address |
  41. +----+-----------+------+------+--------+---------+
  42. | | Jason Yin | boy | | | beijing |
  43. | | Jay | boy | | | Taiwan |
  44. +----+-----------+------+------+--------+---------+
  45. rows in set (0.01 sec)
  46.  
  47. MariaDB [devops]>
  48. MariaDB [devops]>
  49. MariaDB [(none)]> QUIT
  50. Bye
  51. [root@node103.yinzhengjie.org.cn ~]#

slave节点发现只有一个数据库的信息过来啦

三.复制过滤器在master节点定义白名单案例

1>.master服务器配置

  1. [root@node102.yinzhengjie.org.cn ~]# cat /etc/my.cnf
  2. [mysqld]
  3. server-id =
  4. binlog_format = row
  5. log_bin = /data/mysql/logbin/master-
  6. binlog_do_db = db2      #此处我们只记录db2的日志
  7. binlog_do_db = devops     #和上面一行累加起来,就是只记录db2和devops数据库的内容
  8. character-set-server = utf8mb4
  9. default_storage_engine = InnoDB
  10. datadir = /var/lib/mysql
  11. socket = /var/lib/mysql/mysql.sock
  12.  
  13. [mysqld_safe]
  14. log-error = /var/log/mariadb/mariadb.log
  15. pid-file = /var/run/mariadb/mariadb.pid
  16.  
  17. !includedir /etc/my.cnf.d
  18. [root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# cat /etc/my.cnf

  1. [root@node102.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
  2. total
  3. [root@node102.yinzhengjie.org.cn ~]#
  4. [root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
  5. total
  6. [root@node102.yinzhengjie.org.cn ~]#
  7. [root@node102.yinzhengjie.org.cn ~]# systemctl start mariadb
  8. [root@node102.yinzhengjie.org.cn ~]#
  9. [root@node102.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
  10. total
  11. -rw-rw---- mysql mysql Nov : aria_log.
  12. -rw-rw---- mysql mysql Nov : aria_log_control
  13. -rw-rw---- mysql mysql Nov : ibdata1
  14. -rw-rw---- mysql mysql Nov : ib_logfile0
  15. -rw-rw---- mysql mysql Nov : ib_logfile1
  16. drwx------ mysql mysql Nov : mysql
  17. srwxrwxrwx mysql mysql Nov : mysql.sock
  18. drwx------ mysql mysql Nov : performance_schema
  19. drwx------ mysql mysql Nov : test
  20. [root@node102.yinzhengjie.org.cn ~]#
  21. [root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
  22. total
  23. -rw-rw---- mysql mysql Nov : master-102.000001
  24. -rw-rw---- mysql mysql Nov : master-102.000002
  25. -rw-rw---- mysql mysql Nov : master-102.000003
  26. -rw-rw---- mysql mysql Nov : master-.index
  27. [root@node102.yinzhengjie.org.cn ~]#

[root@node102.yinzhengjie.org.cn ~]# systemctl start mariadb

  1. [root@node102.yinzhengjie.org.cn ~]# mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is
  4. Server version: 5.5.-MariaDB MariaDB Server
  5.  
  6. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]>
  11. MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
  12. +------+----------------------------+----------+
  13. | user | host | password |
  14. +------+----------------------------+----------+
  15. | root | localhost | |
  16. | root | node102.yinzhengjie.org.cn | |
  17. | root | 127.0.0.1 | |
  18. | root | :: | |
  19. | | localhost | |
  20. | | node102.yinzhengjie.org.cn | |
  21. +------+----------------------------+----------+
  22. rows in set (0.00 sec)
  23.  
  24. MariaDB [(none)]>
  25. MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie';
  26. Query OK, rows affected (0.00 sec)
  27.  
  28. MariaDB [(none)]>
  29. MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
  30. +------+----------------------------+-------------------------------------------+
  31. | user | host | password |
  32. +------+----------------------------+-------------------------------------------+
  33. | root | localhost | |
  34. | root | node102.yinzhengjie.org.cn | |
  35. | root | 127.0.0.1 | |
  36. | root | :: | |
  37. | | localhost | |
  38. | | node102.yinzhengjie.org.cn | |
  39. | copy | 172.30.1.10% | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
  40. +------+----------------------------+-------------------------------------------+
  41. rows in set (0.00 sec)
  42.  
  43. MariaDB [(none)]>
  44. MariaDB [(none)]> QUIT
  45. Bye
  46. [root@node102.yinzhengjie.org.cn ~]#

在master节点上创建有复制权限的用户账号

  1. [root@node102.yinzhengjie.org.cn ~]# mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is
  4. Server version: 5.5.-MariaDB MariaDB Server
  5.  
  6. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]>
  11. MariaDB [(none)]> SHOW MASTER LOGS;
  12. +-------------------+-----------+
  13. | Log_name | File_size |
  14. +-------------------+-----------+
  15. | master-102.000001 | |
  16. | master-102.000002 | |
  17. | master-102.000003 | |
  18. +-------------------+-----------+
  19. rows in set (0.00 sec)
  20.  
  21. MariaDB [(none)]>
  22. MariaDB [(none)]> SHOW MASTER STATUS;
  23. +-------------------+----------+--------------+------------------+
  24. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  25. +-------------------+----------+--------------+------------------+
  26. | master-102.000003 | | | |
  27. +-------------------+----------+--------------+------------------+
  28. row in set (0.00 sec)
  29.  
  30. MariaDB [(none)]>
  31. MariaDB [(none)]> QUIT
  32. Bye
  33. [root@node102.yinzhengjie.org.cn ~]#

MariaDB [(none)]> SHOW MASTER STATUS;      #查看master当前二进制日志状态信息

2>.slave节点配置主从复制

  1. [root@node103.yinzhengjie.org.cn ~]# cat /etc/my.cnf      #最好将之前配置的从节点白名单删除,以免影响试验结果
  2. [mysqld]
  3. server-id =
  4. binlog_format = row
  5. read-only = on
  6. relay_log = relay-log-
  7. relay_log_index = relay-log-.index
  8. character-set-server = utf8mb4
  9. default_storage_engine = InnoDB
  10. datadir = /var/lib/mysql
  11. socket = /var/lib/mysql/mysql.sock
  12.  
  13. [mysqld_safe]
  14. log-error = /var/log/mariadb/mariadb.log
  15. pid-file = /var/run/mariadb/mariadb.pid
  16.  
  17. !includedir /etc/my.cnf.d
  18. [root@node103.yinzhengjie.org.cn ~]#
  19. [root@node103.yinzhengjie.org.cn ~]#

[root@node103.yinzhengjie.org.cn ~]# cat /etc/my.cnf      #最好将之前配置的从节点白名单删除

  1. [root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
  2. total
  3. [root@node103.yinzhengjie.org.cn ~]#
  4. [root@node103.yinzhengjie.org.cn ~]# systemctl start mariadb
  5. [root@node103.yinzhengjie.org.cn ~]#
  6. [root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
  7. total
  8. -rw-rw---- mysql mysql Nov : aria_log.
  9. -rw-rw---- mysql mysql Nov : aria_log_control
  10. -rw-rw---- mysql mysql Nov : ibdata1
  11. -rw-rw---- mysql mysql Nov : ib_logfile0
  12. -rw-rw---- mysql mysql Nov : ib_logfile1
  13. drwx------ mysql mysql Nov : mysql
  14. srwxrwxrwx mysql mysql Nov : mysql.sock
  15. drwx------ mysql mysql Nov : performance_schema
  16. drwx------ mysql mysql Nov : test
  17. [root@node103.yinzhengjie.org.cn ~]#
  18. [root@node103.yinzhengjie.org.cn ~]#

[root@node103.yinzhengjie.org.cn ~]# systemctl start mariadb

  1. [root@node103.yinzhengjie.org.cn ~]# mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is
  4. Server version: 5.5.-MariaDB MariaDB Server
  5.  
  6. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]> CHANGE MASTER TO
  11. -> MASTER_HOST='172.30.1.102',
  12. -> MASTER_USER='copy',
  13. -> MASTER_PASSWORD='yinzhengjie',
  14. -> MASTER_PORT=,
  15. -> MASTER_LOG_FILE='master-102.000003',
  16. -> MASTER_LOG_POS=,
  17. -> MASTER_CONNECT_RETRY=;
  18. Query OK, rows affected (0.01 sec)
  19.  
  20. MariaDB [(none)]>
  21. MariaDB [(none)]> SHOW SLAVE STATUS\G
  22. *************************** . row ***************************
  23. Slave_IO_State:
  24. Master_Host: 172.30.1.102
  25. Master_User: copy
  26. Master_Port:
  27. Connect_Retry:
  28. Master_Log_File: master-102.000003
  29. Read_Master_Log_Pos:
  30. Relay_Log_File: relay-log-103.000001
  31. Relay_Log_Pos:
  32. Relay_Master_Log_File: master-102.000003
  33. Slave_IO_Running: No
  34. Slave_SQL_Running: No
  35. Replicate_Do_DB: devops
  36. Replicate_Ignore_DB:
  37. Replicate_Do_Table:
  38. Replicate_Ignore_Table:
  39. Replicate_Wild_Do_Table:
  40. Replicate_Wild_Ignore_Table:
  41. Last_Errno:
  42. Last_Error:
  43. Skip_Counter:
  44. Exec_Master_Log_Pos:
  45. Relay_Log_Space:
  46. Until_Condition: None
  47. Until_Log_File:
  48. Until_Log_Pos:
  49. Master_SSL_Allowed: No
  50. Master_SSL_CA_File:
  51. Master_SSL_CA_Path:
  52. Master_SSL_Cert:
  53. Master_SSL_Cipher:
  54. Master_SSL_Key:
  55. Seconds_Behind_Master: NULL
  56. Master_SSL_Verify_Server_Cert: No
  57. Last_IO_Errno:
  58. Last_IO_Error:
  59. Last_SQL_Errno:
  60. Last_SQL_Error:
  61. Replicate_Ignore_Server_Ids:
  62. Master_Server_Id:
  63. row in set (0.00 sec)
  64.  
  65. MariaDB [(none)]>
  66. MariaDB [(none)]> START SLAVE;
  67. Query OK, rows affected (0.00 sec)
  68.  
  69. MariaDB [(none)]>
  70. MariaDB [(none)]> SHOW SLAVE STATUS\G
  71. *************************** . row ***************************
  72. Slave_IO_State: Waiting for master to send event
  73. Master_Host: 172.30.1.102
  74. Master_User: copy
  75. Master_Port:
  76. Connect_Retry:
  77. Master_Log_File: master-102.000003
  78. Read_Master_Log_Pos:
  79. Relay_Log_File: relay-log-103.000002
  80. Relay_Log_Pos:
  81. Relay_Master_Log_File: master-102.000003
  82. Slave_IO_Running: Yes
  83. Slave_SQL_Running: Yes
  84. Replicate_Do_DB: devops
  85. Replicate_Ignore_DB:
  86. Replicate_Do_Table:
  87. Replicate_Ignore_Table:
  88. Replicate_Wild_Do_Table:
  89. Replicate_Wild_Ignore_Table:
  90. Last_Errno:
  91. Last_Error:
  92. Skip_Counter:
  93. Exec_Master_Log_Pos:
  94. Relay_Log_Space:
  95. Until_Condition: None
  96. Until_Log_File:
  97. Until_Log_Pos:
  98. Master_SSL_Allowed: No
  99. Master_SSL_CA_File:
  100. Master_SSL_CA_Path:
  101. Master_SSL_Cert:
  102. Master_SSL_Cipher:
  103. Master_SSL_Key:
  104. Seconds_Behind_Master:
  105. Master_SSL_Verify_Server_Cert: No
  106. Last_IO_Errno:
  107. Last_IO_Error:
  108. Last_SQL_Errno:
  109. Last_SQL_Error:
  110. Replicate_Ignore_Server_Ids:
  111. Master_Server_Id:
  112. row in set (0.00 sec)
  113.  
  114. MariaDB [(none)]>

slave配置主从复制详细过程

3>.验证数据库配置是否生效

  1. [root@node102.yinzhengjie.org.cn ~]# mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is
  4. Server version: 5.5.-MariaDB MariaDB Server
  5.  
  6. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]>
  11. MariaDB [(none)]> SHOW DATABASES;
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | information_schema |
  16. | mysql |
  17. | performance_schema |
  18. | test |
  19. +--------------------+
  20. rows in set (0.00 sec)
  21.  
  22. MariaDB [(none)]>
  23. MariaDB [(none)]> CREATE DATABASE db1;
  24. Query OK, row affected (0.00 sec)
  25.  
  26. MariaDB [(none)]>
  27. MariaDB [(none)]> CREATE DATABASE db2;
  28. Query OK, row affected (0.00 sec)
  29.  
  30. MariaDB [(none)]> CREATE DATABASE db3;
  31. Query OK, row affected (0.00 sec)
  32.  
  33. MariaDB [(none)]> CREATE DATABASE devops;
  34. Query OK, row affected (0.00 sec)
  35.  
  36. MariaDB [(none)]> USE devops
  37. Database changed
  38. MariaDB [devops]>
  39. MariaDB [devops]> CREATE TABLE students(id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR() NOT NULL,sex ENUM('boy','girl') DEFAULT 'boy',age TINYINT UNSIGNED,mobile CHAR(),address VARCHAR());
  40. Query OK, rows affected (0.01 sec)
  41.  
  42. MariaDB [devops]>
  43. MariaDB [devops]> INSERT INTO students (name,age,mobile,address) VALUES ('Jason Yin',,,'beijing'),('Jay','',,'Taiwan');
  44. Query OK, rows affected (0.01 sec)
  45. Records: Duplicates: Warnings:
  46.  
  47. MariaDB [devops]>
  48. MariaDB [devops]> SELECT * FROM students;
  49. +----+-----------+------+------+--------+---------+
  50. | id | name | sex | age | mobile | address |
  51. +----+-----------+------+------+--------+---------+
  52. | | Jason Yin | boy | | | beijing |
  53. | | Jay | boy | | | Taiwan |
  54. +----+-----------+------+------+--------+---------+
  55. rows in set (0.00 sec)
  56.  
  57. MariaDB [devops]>
  58. MariaDB [devops]> USE db2
  59. Database changed
  60. MariaDB [db2]>
  61. MariaDB [db2]> CREATE TABLE test SELECT * FROM devops.students;
  62. Query OK, rows affected (0.00 sec)
  63. Records: Duplicates: Warnings:
  64.  
  65. MariaDB [db2]>
  66. MariaDB [db2]> SELECT * FROM test;
  67. +----+-----------+------+------+--------+---------+
  68. | id | name | sex | age | mobile | address |
  69. +----+-----------+------+------+--------+---------+
  70. | | Jason Yin | boy | | | beijing |
  71. | | Jay | boy | | | Taiwan |
  72. +----+-----------+------+------+--------+---------+
  73. rows in set (0.00 sec)
  74.  
  75. MariaDB [db2]>
  76. MariaDB [db2]> SHOW DATABASES;
  77. +--------------------+
  78. | Database |
  79. +--------------------+
  80. | information_schema |
  81. | db1 |
  82. | db2 |
  83. | db3 |
  84. | devops |
  85. | mysql |
  86. | performance_schema |
  87. | test |
  88. +--------------------+
  89. rows in set (0.00 sec)
  90.  
  91. MariaDB [db2]>
  92. MariaDB [db2]> QUIT
  93. Bye
  94. [root@node102.yinzhengjie.org.cn ~]#

master节点创建多个测试数据库

  1. [root@node103.yinzhengjie.org.cn ~]# mysql
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is
  4. Server version: 5.5.-MariaDB MariaDB Server
  5.  
  6. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  7.  
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9.  
  10. MariaDB [(none)]>
  11. MariaDB [(none)]> SHOW DATABASES;
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | information_schema |
  16. | db2 |
  17. | devops |
  18. | mysql |
  19. | performance_schema |
  20. | test |
  21. +--------------------+
  22. rows in set (0.00 sec)
  23.  
  24. MariaDB [(none)]>
  25. MariaDB [(none)]> USE devops
  26. Reading table information for completion of table and column names
  27. You can turn off this feature to get a quicker startup with -A
  28.  
  29. Database changed
  30. MariaDB [devops]>
  31. MariaDB [devops]> SHOW TABLES;
  32. +------------------+
  33. | Tables_in_devops |
  34. +------------------+
  35. | students |
  36. +------------------+
  37. row in set (0.00 sec)
  38.  
  39. MariaDB [devops]>
  40. MariaDB [devops]> SELECT * FROM students;
  41. +----+-----------+------+------+--------+---------+
  42. | id | name | sex | age | mobile | address |
  43. +----+-----------+------+------+--------+---------+
  44. | | Jason Yin | boy | | | beijing |
  45. | | Jay | boy | | | Taiwan |
  46. +----+-----------+------+------+--------+---------+
  47. rows in set (0.00 sec)
  48.  
  49. MariaDB [devops]>
  50. MariaDB [devops]> USE db2
  51. Reading table information for completion of table and column names
  52. You can turn off this feature to get a quicker startup with -A
  53.  
  54. Database changed
  55. MariaDB [db2]>
  56. MariaDB [db2]> SHOW TABLES;
  57. +---------------+
  58. | Tables_in_db2 |
  59. +---------------+
  60. | test |
  61. +---------------+
  62. row in set (0.00 sec)
  63.  
  64. MariaDB [db2]>
  65. MariaDB [db2]> SELECT * FROM test;
  66. +----+-----------+------+------+--------+---------+
  67. | id | name | sex | age | mobile | address |
  68. +----+-----------+------+------+--------+---------+
  69. | | Jason Yin | boy | | | beijing |
  70. | | Jay | boy | | | Taiwan |
  71. +----+-----------+------+------+--------+---------+
  72. rows in set (0.00 sec)
  73.  
  74. MariaDB [db2]>
  75. MariaDB [db2]> QUIT
  76. Bye
  77. [root@node103.yinzhengjie.org.cn ~]#

slave节点发现只有db2和devops两个数据库的信息过来啦

MySQL/MariaDB数据库的复制过滤器的更多相关文章

  1. MySQL/MariaDB数据库的复制监控和维护

      MySQL/MariaDB数据库的复制监控和维护 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.清理日志 1>.删除指定日志文件名称之前的日志(也可用基于时间) M ...

  2. MySQL/MariaDB数据库的复制加密

      MySQL/MariaDB数据库的复制加密 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL的安全问题 1>.基于SSL复制 在默认的主从复制过程或远程连接 ...

  3. MySQL/MariaDB数据库的半同步复制

      MySQL/MariaDB数据库的半同步复制 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL半同步复制概述 1>.MySQL默认的异步复制 默认情况下,M ...

  4. MySQL/MariaDB数据库的主主复制

      MySQL/MariaDB数据库的主主复制 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.主主复制概述 1>.什么是主主复制 所谓的主主复制,说白了就是两台节点互为 ...

  5. MySQL/MariaDB数据库的主从级联复制

      MySQL/MariaDB数据库的主从级联复制 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.主从复制类型概述 1>.主从复制 博主推荐阅读: https://ww ...

  6. MySQL/MariaDB数据库的Galera高可用性集群实战

      MySQL/MariaDB数据库的Galera高可用性集群实战 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Galera Cluster概述 1>.什么是Gale ...

  7. MySQL/MariaDB数据库的MHA实现高可用实战

      MySQL/MariaDB数据库的MHA实现高可用实战 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL高可用常见的解决方案 1>.Multi-Master ...

  8. MySQL/MariaDB数据库的PROXY实现读写分离

    MySQL/MariaDB数据库的PROXY实现读写分离 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ProxySQL概述 1>.各家互联网公司读写分离的解决方案 m ...

  9. MySQL/MariaDB数据库的主从复制

     MySQL/MariaDB数据库的主从复制  作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL复制概述 1>.传统扩展方式 垂直扩展(也叫向上扩展,Sacle ...

随机推荐

  1. sql 在查询到的语句基础上添加行号

    正常查询语句: SELECT TagName FROM ps_status a WHERE a.TagName LIKE "DTmk_zybf%1bxxjcqh.PV" 查询结果: ...

  2. Linux whereis、find和locate命令区别以及应用场景

    查找某个文件是我们在使用使用linux中非常常用的一个命令. linux中有多个查找文件的指令:whereis.find.locate都有类似查找的功能,下面将讲解这些指令之间的区别. whereis ...

  3. [Docker] 六步运行一个 sentry 实例

    # 6步, https://hub.docker.com/_/sentry/ # 依赖Redisdocker run -d --name sentry-redis redis:3.2.12 # 依赖p ...

  4. Java的三大版本

    Java的三大版本 Write Once.Run Anywhere JavaSE:标准版(桌面程序,控制台开发......) JavaME:嵌入式开发(手机,小家电......) JavaEE:E企业 ...

  5. dell服务器在bios中指定raid5的热备盘

    一.创建raid5 二.指定热备盘   选择第15块磁盘作为上面创建的raid5的热备盘 选中 选中我们刚创建的raid5,点击OK

  6. PAT 1098

    1098 Insertion or Heap Sort (25 分)   According to Wikipedia: Insertion sort iterates, consuming one ...

  7. SQL Server sp_monitor使用

    SQL Server提供了sp_monitor存储过程可以方便我们查看SQL Server性能统计信息,包括CPU/Network/IO,通过这些信息可以对自己的数据库性能状况有一个大致的了解. 下面 ...

  8. 服务器BMC资料整理

    1. 现在服务器都有BMC管理了,可以直接连上服务器进行处理. bios里面进行简单设置就可以了, 连接上IPMI的口进行管理. 2. 可以使用 远程控制安装操作系统. 安装系统时 比较清楚的能够看到 ...

  9. Centos 7搭建Gitlab服务器超详细

    一. 安装并配置必要的依赖关系在CentOS系统上安装所需的依赖:ssh,防火墙,postfix(用于邮件通知) ,wget,以下这些命令也会打开系统防火墙中的HTTP和SSH端口访问. 1.安装ss ...

  10. Go基础编程实践(四)—— 数组和map

    数组去重 package main import "fmt" func main(){ intSlice := []int{1,5,5,5,5,7,8,6,6, 6} fmt.Pr ...