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

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

                       作者:尹正杰 

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

 

一.复制过滤器概述

1>.复制器过滤器功能

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

2>.两种实现方式

方案一:
  主服务器仅向二进制日志中记录与特定数据库相关的事件   此项和binlog_format相关,详情请参考官网说明:https:
//mariadb.com/kb/en/library/mysqld-options/#-binlog-ignore-db     binlog_do_db = 数据库白名单列表,多个数据库需多行实现     binlog_ignore_db = 数据库黑名单列表   问题:
    基于二进制还原将无法实现(因为记录的日志只有部分数据库信息,可能存在部分数据无法还原的现象);不建议使用

方案二:
  从服务器SQL_THREAD在replay中继日志中的事件时,仅读取与特定数据库(特定表)相关的事件并应用于本地   问题:
    由于将master节点的所有数据都copy至slave节点,但SQL_THREAD线程仅读取与特定数据库(特定表)相关的事件并应用于本地,也就是说部分数据传过来也不去使用,这会造成网络及磁盘IO浪费。

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

MariaDB [(none)]> SHOW VARIABLES LIKE '%replicate%';
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| replicate_annotate_row_events    | OFF       |
| replicate_do_db                  |           |
| replicate_do_table               |           |
| replicate_events_marked_for_skip | replicate |
| replicate_ignore_db              |           |
| replicate_ignore_table           |           |
| replicate_wild_do_table          |           |
| replicate_wild_ignore_table      |           |
+----------------------------------+-----------+
8 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SHOW VARIABLES LIKE '%replicate%';
replicate_do_db= 
    指定复制库的白名单
replicate_ignore_db= 
    指定复制库黑名单
replicate_do_table= 
    指定复制表的白名单
replicate_ignore_table= 
    指定复制表的黑名单
replicate_wild_do_table= foo%.bar% 
    解决跨库更新的问题支持通配符
replicate_wild_ignore_table=
  同上

 

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

1>.master服务器配置

[root@node102.yinzhengjie.org.cn ~]# cat /etc/my.cnf
[mysqld]
server-id                    = 102
binlog_format                = row
log_bin                      = /data/mysql/logbin/master-102
character-set-server         = utf8mb4
default_storage_engine       = InnoDB
datadir                      = /var/lib/mysql
socket                       = /var/lib/mysql/mysql.sock

[mysqld_safe]
log-error                    = /var/log/mariadb/mariadb.log
pid-file                     = /var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/my.cnf    #查看配置文件
[root@node102.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
total 0
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
total 0
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl start mariadb
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
total 37852
-rw-rw---- 1 mysql mysql    16384 Nov 10 11:03 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov 10 11:03 aria_log_control
-rw-rw---- 1 mysql mysql 18874368 Nov 10 11:03 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Nov 10 11:03 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Nov 10 11:03 ib_logfile1
drwx------ 2 mysql mysql     4096 Nov 10 11:03 mysql
srwxrwxrwx 1 mysql mysql        0 Nov 10 11:03 mysql.sock
drwx------ 2 mysql mysql     4096 Nov 10 11:03 performance_schema
drwx------ 2 mysql mysql        6 Nov 10 11:03 test
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
total 940
-rw-rw---- 1 mysql mysql  26813 Nov 10 11:03 master-102.000001
-rw-rw---- 1 mysql mysql 921736 Nov 10 11:03 master-102.000002
-rw-rw---- 1 mysql mysql    245 Nov 10 11:03 master-102.000003
-rw-rw---- 1 mysql mysql    111 Nov 10 11:03 master-102.index
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl start mariadb  #启动数据库实例
[root@node102.yinzhengjie.org.cn ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
+------+----------------------------+----------+
| user | host                       | password |
+------+----------------------------+----------+
| root | localhost                  |          |
| root | node102.yinzhengjie.org.cn |          |
| root | 127.0.0.1                  |          |
| root | ::1                        |          |
|      | localhost                  |          |
|      | node102.yinzhengjie.org.cn |          |
+------+----------------------------+----------+
6 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
+------+----------------------------+-------------------------------------------+
| user | host                       | password                                  |
+------+----------------------------+-------------------------------------------+
| root | localhost                  |                                           |
| root | node102.yinzhengjie.org.cn |                                           |
| root | 127.0.0.1                  |                                           |
| root | ::1                        |                                           |
|      | localhost                  |                                           |
|      | node102.yinzhengjie.org.cn |                                           |
| copy | 172.30.1.10%               | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
+------+----------------------------+-------------------------------------------+
7 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> QUIT
Bye
[root@node102.yinzhengjie.org.cn ~]# 
在master节点上创建有复制权限的用户账号
[root@node102.yinzhengjie.org.cn ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW MASTER LOGS;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-102.000001 |     26813 |
| master-102.000002 |    921736 |
| master-102.000003 |       403 |
+-------------------+-----------+
3 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-102.000003 |      403 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> QUIT
Bye
[root@node102.yinzhengjie.org.cn ~]# 
MariaDB [(none)]> SHOW MASTER STATUS;      #查看master当前二进制日志状态信息

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

[root@node103.yinzhengjie.org.cn ~]# cat /etc/my.cnf      #查看配置文件
[mysqld]
server-id                   = 103
binlog_format               = row
read-only                   = on
replicate_do_db             = devops
relay_log                   = relay-log-103
relay_log_index             = relay-log-103.index
character-set-server        = utf8mb4
default_storage_engine      = InnoDB
datadir                     = /var/lib/mysql
socket                      = /var/lib/mysql/mysql.sock

[mysqld_safe]
log-error                   = /var/log/mariadb/mariadb.log
pid-file                    = /var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# cat /etc/my.cnf  #编辑配置文件,指定值复制devops这个数据库
[root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
total 0
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# systemctl start mariadb
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
total 37852
-rw-rw---- 1 mysql mysql    16384 Nov 10 11:14 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov 10 11:14 aria_log_control
-rw-rw---- 1 mysql mysql 18874368 Nov 10 11:14 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Nov 10 11:14 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Nov 10 11:14 ib_logfile1
drwx------ 2 mysql mysql     4096 Nov 10 11:14 mysql
srwxrwxrwx 1 mysql mysql        0 Nov 10 11:14 mysql.sock
drwx------ 2 mysql mysql     4096 Nov 10 11:14 performance_schema
drwx------ 2 mysql mysql        6 Nov 10 11:14 test
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# systemctl start mariadb
[root@node103.yinzhengjie.org.cn ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CHANGE MASTER TO 
    -> MASTER_HOST='172.30.1.102', 
    -> MASTER_USER='copy', 
    -> MASTER_PASSWORD='yinzhengjie', 
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='master-102.000003', 
    -> MASTER_LOG_POS=403,
    -> MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.30.1.102
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: master-102.000003
          Read_Master_Log_Pos: 403
               Relay_Log_File: relay-log-103.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-102.000003
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: devops
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 403
              Relay_Log_Space: 245
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
1 row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.30.1.102
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: master-102.000003
          Read_Master_Log_Pos: 403
               Relay_Log_File: relay-log-103.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-102.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: devops
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 403
              Relay_Log_Space: 822
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 102
1 row in set (0.00 sec)

MariaDB [(none)]> 
slave配置主从复制详细过程

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

[root@node102.yinzhengjie.org.cn ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> CREATE DATABASE db1;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> CREATE DATABASE db2;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> CREATE DATABASE db3;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> CREATE DATABASE devops;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| db3                |
| devops             |
| mysql              |
| performance_schema |
| test               |
+--------------------+
rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> USE devops
Database changed
MariaDB [devops]> 
MariaDB [devops]> CREATE TABLE students(id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR(30) NOT NULL,sex ENUM('bo
y','girl') DEFAULT 'boy',age TINYINT UNSIGNED,mobile CHAR(11),address VARCHAR(50));Query OK, 0 rows affected (0.01 sec)

MariaDB [devops]> 
MariaDB [devops]> INSERT INTO students (name,age,mobile,address) VALUES ('Jason Yin',18,10000,'beijing'),('Jay','40',1008
6,'Taiwan');Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [devops]> 
MariaDB [devops]> SHOW TABLES;
+------------------+
| Tables_in_devops |
+------------------+
| students         |
+------------------+
1 row in set (0.00 sec)

MariaDB [devops]> 
MariaDB [devops]> SELECT * FROM students;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   18 | 10000  | beijing |
|  2 | Jay       | boy  |   40 | 10086  | Taiwan  |
+----+-----------+------+------+--------+---------+
2 rows in set (0.00 sec)

MariaDB [devops]> 
MariaDB [devops]> 
master节点创建多个测试数据库
[root@node103.yinzhengjie.org.cn ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| devops             |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> USE devops
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [devops]> SHOW TABLES;
+------------------+
| Tables_in_devops |
+------------------+
| students         |
+------------------+
1 row in set (0.00 sec)

MariaDB [devops]> 
MariaDB [devops]> SELECT * FROM students;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   18 | 10000  | beijing |
|  2 | Jay       | boy  |   40 | 10086  | Taiwan  |
+----+-----------+------+------+--------+---------+
2 rows in set (0.01 sec)

MariaDB [devops]> 
MariaDB [devops]> 
MariaDB [(none)]> QUIT
Bye
[root@node103.yinzhengjie.org.cn ~]# 
slave节点发现只有一个数据库的信息过来啦

 

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

1>.master服务器配置

[root@node102.yinzhengjie.org.cn ~]# cat /etc/my.cnf
[mysqld]
server-id                    = 102
binlog_format                = row
log_bin                      = /data/mysql/logbin/master-102
binlog_do_db                 = db2      #此处我们只记录db2的日志
binlog_do_db                 = devops     #和上面一行累加起来,就是只记录db2和devops数据库的内容
character-set-server         = utf8mb4
default_storage_engine       = InnoDB
datadir                      = /var/lib/mysql
socket                       = /var/lib/mysql/mysql.sock

[mysqld_safe]
log-error                    = /var/log/mariadb/mariadb.log
pid-file                     = /var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# cat /etc/my.cnf
[root@node102.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
total 0
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
total 0
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl start mariadb
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
total 37852
-rw-rw---- 1 mysql mysql    16384 Nov 10 11:44 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov 10 11:44 aria_log_control
-rw-rw---- 1 mysql mysql 18874368 Nov 10 11:44 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Nov 10 11:44 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Nov 10 11:44 ib_logfile1
drwx------ 2 mysql mysql     4096 Nov 10 11:44 mysql
srwxrwxrwx 1 mysql mysql        0 Nov 10 11:44 mysql.sock
drwx------ 2 mysql mysql     4096 Nov 10 11:44 performance_schema
drwx------ 2 mysql mysql        6 Nov 10 11:44 test
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ll /data/mysql/logbin/
total 16
-rw-rw---- 1 mysql mysql 264 Nov 10 11:44 master-102.000001
-rw-rw---- 1 mysql mysql 264 Nov 10 11:44 master-102.000002
-rw-rw---- 1 mysql mysql 245 Nov 10 11:44 master-102.000003
-rw-rw---- 1 mysql mysql 111 Nov 10 11:44 master-102.index
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl start mariadb
[root@node102.yinzhengjie.org.cn ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
+------+----------------------------+----------+
| user | host                       | password |
+------+----------------------------+----------+
| root | localhost                  |          |
| root | node102.yinzhengjie.org.cn |          |
| root | 127.0.0.1                  |          |
| root | ::1                        |          |
|      | localhost                  |          |
|      | node102.yinzhengjie.org.cn |          |
+------+----------------------------+----------+
rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
+------+----------------------------+-------------------------------------------+
| user | host                       | password                                  |
+------+----------------------------+-------------------------------------------+
| root | localhost                  |                                           |
| root | node102.yinzhengjie.org.cn |                                           |
| root | 127.0.0.1                  |                                           |
| root | ::1                        |                                           |
|      | localhost                  |                                           |
|      | node102.yinzhengjie.org.cn |                                           |
| copy | 172.30.1.10%               | *BD0B1F48FDC55BD27555FC2F22FF29A68A25A1D7 |
+------+----------------------------+-------------------------------------------+
rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> QUIT
Bye
[root@node102.yinzhengjie.org.cn ~]# 
在master节点上创建有复制权限的用户账号
[root@node102.yinzhengjie.org.cn ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW MASTER LOGS;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-102.000001 |     26813 |
| master-102.000002 |    921736 |
| master-102.000003 |       403 |
+-------------------+-----------+
rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-102.000003 |      403 |              |                  |
+-------------------+----------+--------------+------------------+
row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> QUIT
Bye
[root@node102.yinzhengjie.org.cn ~]# 
MariaDB [(none)]> SHOW MASTER STATUS;      #查看master当前二进制日志状态信息

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

[root@node103.yinzhengjie.org.cn ~]# cat /etc/my.cnf      #最好将之前配置的从节点白名单删除,以免影响试验结果
[mysqld]
server-id                   = 103
binlog_format               = row
read-only                   = on
relay_log                   = relay-log-103
relay_log_index             = relay-log-103.index
character-set-server        = utf8mb4
default_storage_engine      = InnoDB
datadir                     = /var/lib/mysql
socket                      = /var/lib/mysql/mysql.sock

[mysqld_safe]
log-error                   = /var/log/mariadb/mariadb.log
pid-file                    = /var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# cat /etc/my.cnf      #最好将之前配置的从节点白名单删除
[root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
total 0
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# systemctl start mariadb
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# ll /var/lib/mysql/
total 37852
-rw-rw---- 1 mysql mysql    16384 Nov 10 11:48 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Nov 10 11:48 aria_log_control
-rw-rw---- 1 mysql mysql 18874368 Nov 10 11:48 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Nov 10 11:48 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Nov 10 11:48 ib_logfile1
drwx------ 2 mysql mysql     4096 Nov 10 11:48 mysql
srwxrwxrwx 1 mysql mysql        0 Nov 10 11:48 mysql.sock
drwx------ 2 mysql mysql     4096 Nov 10 11:48 performance_schema
drwx------ 2 mysql mysql        6 Nov 10 11:48 test
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# systemctl start mariadb
[root@node103.yinzhengjie.org.cn ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CHANGE MASTER TO 
    -> MASTER_HOST='172.30.1.102', 
    -> MASTER_USER='copy', 
    -> MASTER_PASSWORD='yinzhengjie', 
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='master-102.000003', 
    -> MASTER_LOG_POS=403,
    -> MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.30.1.102
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: master-102.000003
          Read_Master_Log_Pos: 403
               Relay_Log_File: relay-log-103.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-102.000003
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: devops
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 403
              Relay_Log_Space: 245
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.30.1.102
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: master-102.000003
          Read_Master_Log_Pos: 403
               Relay_Log_File: relay-log-103.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-102.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: devops
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 403
              Relay_Log_Space: 822
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 102
row in set (0.00 sec)

MariaDB [(none)]> 
slave配置主从复制详细过程

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

[root@node102.yinzhengjie.org.cn ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> CREATE DATABASE db1;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> CREATE DATABASE db2;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> CREATE DATABASE db3;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> CREATE DATABASE devops;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> USE devops
Database changed
MariaDB [devops]> 
MariaDB [devops]> CREATE TABLE students(id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR(30) NOT NULL,sex ENUM('boy','girl') DEFAULT 'boy',age TINYINT UNSIGNED,mobile CHAR(11),address VARCHAR(50));
Query OK, 0 rows affected (0.01 sec)

MariaDB [devops]> 
MariaDB [devops]> INSERT INTO students (name,age,mobile,address) VALUES ('Jason Yin',18,10000,'beijing'),('Jay','40',10086,'Taiwan');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [devops]> 
MariaDB [devops]> SELECT * FROM students;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   18 | 10000  | beijing |
|  2 | Jay       | boy  |   40 | 10086  | Taiwan  |
+----+-----------+------+------+--------+---------+
rows in set (0.00 sec)

MariaDB [devops]> 
MariaDB [devops]> USE db2
Database changed
MariaDB [db2]> 
MariaDB [db2]> CREATE TABLE test SELECT * FROM devops.students;
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [db2]> 
MariaDB [db2]> SELECT * FROM test;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   18 | 10000  | beijing |
|  2 | Jay       | boy  |   40 | 10086  | Taiwan  |
+----+-----------+------+------+--------+---------+
rows in set (0.00 sec)

MariaDB [db2]> 
MariaDB [db2]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| db3                |
| devops             |
| mysql              |
| performance_schema |
| test               |
+--------------------+
8 rows in set (0.00 sec)

MariaDB [db2]> 
MariaDB [db2]> QUIT
Bye
[root@node102.yinzhengjie.org.cn ~]# 
master节点创建多个测试数据库
[root@node103.yinzhengjie.org.cn ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db2                |
| devops             |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> USE devops
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [devops]> 
MariaDB [devops]> SHOW TABLES;
+------------------+
| Tables_in_devops |
+------------------+
| students         |
+------------------+
1 row in set (0.00 sec)

MariaDB [devops]> 
MariaDB [devops]> SELECT * FROM students;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   18 | 10000  | beijing |
|  2 | Jay       | boy  |   40 | 10086  | Taiwan  |
+----+-----------+------+------+--------+---------+
2 rows in set (0.00 sec)

MariaDB [devops]> 
MariaDB [devops]> USE db2
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [db2]> 
MariaDB [db2]> SHOW TABLES;
+---------------+
| Tables_in_db2 |
+---------------+
| test          |
+---------------+
1 row in set (0.00 sec)

MariaDB [db2]> 
MariaDB [db2]> SELECT * FROM test;
+----+-----------+------+------+--------+---------+
| id | name      | sex  | age  | mobile | address |
+----+-----------+------+------+--------+---------+
|  1 | Jason Yin | boy  |   18 | 10000  | beijing |
|  2 | Jay       | boy  |   40 | 10086  | Taiwan  |
+----+-----------+------+------+--------+---------+
2 rows in set (0.00 sec)

MariaDB [db2]> 
MariaDB [db2]> QUIT
Bye
[root@node103.yinzhengjie.org.cn ~]# 
slave节点发现只有db2和devops两个数据库的信息过来啦

 

posted @ 2019-11-10 05:32  尹正杰  阅读(324)  评论(0编辑  收藏  举报