MariaDB Replication

MariaDB Replication

Replication是mariadb支持上万台mariadb server,千万级以上用户和PB级以上数据的关键
Replication是一个异步复制过程,可能会出现同一时间主从不同步的情况
从master到slave的复制涉及到3个相关的线程,其中sql和i/o线程在slave端,另外一个i/o线程在master端


环境:
CentOS 7.1 x64
mariadb-10.1.13

slave提升为master
1.检查日志是否全部应用
SHOW PROCESSLIST;
2.停止slave,去掉read_only,重置slave
STOP SLAVE;
SET GLOBAL read_only=0;
RESET SLAVE;
SHOW MASTER STATUS;



一.一主多从
MariaDB <wbr>Replication
db00: 192.168.8.100(master)
db01: 192.168.8.101(slave)
db02: 192.168.8.102(slave)
1.创建具有Replication相关权限的数据库用户(所有节点)
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO replicant@'192.168.8.%' IDENTIFIED BY 'foo.123';
FLUSH PRIVILEGES;
2.停止数据库(所有节点)
systemctl stop mysql
3.修改配置文件/etc/my.cnf

db00: 192.168.8.100(master)

[mysqld]

band_address = 192.168.8.100

log-bin = mysql-bin

server-id = 100

relay_log = db00-relay-binlog 


db01: 192.168.8.101(slave)

[mysqld]

band_address = 192.168.8.101

log-bin = mysql-bin

server-id = 101

relay-log = db01-relay-binlog

read-only


db02: 192.168.8.102(slave)

[mysqld]

band_address = 192.168.8.102

log-bin = mysql-bin

server-id = 102

relay-log = db02-relay-binlog

read-only


说明:
要实现Replication必需开启binary log功能,所以3节点都开启了log-bin
server-id,relay-log名称要全局唯一
slave节点设置为read-only
4.启动数据库(所有节点)
systemctl start mysql
5.查看master节点状态及bin-log名称(master节点)

MariaDB [(none)]> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000002     327 |                              |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

6.指定master节点(所有slave节点)

    CHANGE MASTER TO MASTER_HOST='192.168.8.100',

    MASTER_PORT=3306,   

    MASTER_USER = 'replicant',

    MASTER_PASSWORD = 'foo.123',

    MASTER_CONNECT_RETRY=10,

    MASTER_LOG_FILE = 'mysql-bin.000002',

    MASTER_LOG_POS = 0;

    START SLAVE;


7.查看slave状态

确认Slave_IO_Running,Slave_SQL_Running都为Yes,说明slave端sql和i/o线程都正常运行

MariaDB [(none)]> SHOW SLAVE STATUS\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.8.100

                  Master_User: replicant

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 327

               Relay_Log_File: db02-relay-binlog.000002

                Relay_Log_Pos: 615

        Relay_Master_Log_File: mysql-bin.000002

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          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: 327

              Relay_Log_Space: 915

              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: 100

               Master_SSL_Crl: 

           Master_SSL_Crlpath: 

                   Using_Gtid: No

                  Gtid_IO_Pos: 

      Replicate_Do_Domain_Ids: 

  Replicate_Ignore_Domain_Ids: 

                Parallel_Mode: conservative

 

1 row in set (0.00 sec)



8.测试主从同步

在master节点上创建数据库和表

CREATE DATABASE IF NOT EXISTS temp;

USE temp;

CREATE TABLE doctors (

    id int NOT NULL AUTO_INCREMENT PRIMARY KEY,

    given_names varchar(255),

    surname varchar(255),

    birthdate date);

INSERT INTO doctors VALUES 

    (1,'William','Hartnell','1908-01-08'),

    (2,'Patrick','Troughton','1920-03-25'),

    (3,'Jon','Pertwee','1919-07-07'),

    (4,'Tom','Baker','1934-01-20');


在slave端查询

MariaDB [(none)]> SELECT * FROM temp.doctors;

+----+-------------+-----------+------------+

| id | given_names | surname   | birthdate  |

+----+-------------+-----------+------------+

1 | William     | Hartnell  | 1908-01-08 |

2 | Patrick     | Troughton | 1920-03-25 |

3 | Jon         | Pertwee   | 1919-07-07 |

4 | Tom         | Baker     | 1934-01-20 |

+----+-------------+-----------+------------+

 

4 rows in set (0.00 sec)


9.更安全的Replication

[mysqld]

innodb_flush_logs_at_trx_commit = 1 

sync_binlog = 1 

尽可能快地将数据写入磁盘,减少数据损坏的可能性,但也失去了fsync的性能,mariadb开发团队已经内嵌了一个尽可能联合fsync的调优,让sync写磁盘的同时,最大提升性能。


10.开启relay log(slave节点, 可选)

默认情况下,slave只是应用master的bin-log,slave本地不存有log,

[mysqld]

log_slave_updates 

参数可以让slave保存自己单独的log,称为relay log, 可以为启用了relay log的slave再配置其它的slave


11.Global transaction IDs

https://mariadb.com/kb/en/global-transaction-id/

mariadb10引进的新功能

在所有的slave节点上操作,

STOP SLAVE; 

CHANGE MASTER TO MASTER_USE_GTID = SLAVE_POS; 

START SLAVE; 

SHOW ALL SLAVES STATUS;

MariaDB [temp]> SHOW ALL SLAVES STATUS\G

*************************** 1. row ***************************

              Connection_name: 

              Slave_SQL_State: Slave has read all relay log; waiting for the slave I/O thread to update it

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.8.100

                  Master_User: replicant

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 1004

               Relay_Log_File: db02-relay-binlog.000002

                Relay_Log_Pos: 654

        Relay_Master_Log_File: mysql-bin.000002

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          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: 1004

              Relay_Log_Space: 954

              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: 100

               Master_SSL_Crl: 

           Master_SSL_Crlpath: 

                   Using_Gtid: Slave_Pos

                  Gtid_IO_Pos: 0-100-14

      Replicate_Do_Domain_Ids: 

  Replicate_Ignore_Domain_Ids: 

                Parallel_Mode: conservative

         Retried_transactions: 0

           Max_relay_log_size: 1073741824

         Executed_log_entries: 22

    Slave_received_heartbeats: 0

       Slave_heartbeat_period: 1800.000

               Gtid_Slave_Pos: 0-100-14

 

1 row in set (0.00 sec)


注意防火墙

firewall-cmd --permanent --add-port=3306/tcp

firewall-cmd --reload


11.测试从库read-only

master节点

[root@db00 ~]# mysql -uroot -p

Enter password: 

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 6

Server version: 10.1.13-MariaDB MariaDB Server


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


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


MariaDB [(none)]> CREATE DATABASE hello;

Query OK, 1 row affected (0.00 sec)


MariaDB [(none)]> GRANT ALL ON hello.* TO 'test_read_only'@'localhost' identified by 'hello';

Query OK, 0 rows affected (0.00 sec)


MariaDB [(none)]> USE hello;

Database changed

MariaDB [hello]> CREATE TABLE doctors (

    ->     id int NOT NULL AUTO_INCREMENT PRIMARY KEY,

    ->     given_names varchar(255),

    ->     surname varchar(255),

    ->     birthdate date);

Query OK, 0 rows affected (0.02 sec)


MariaDB [hello]> INSERT INTO doctors VALUES 

    ->     (1,'William','Hartnell','1908-01-08'),

    ->     (2,'Patrick','Troughton','1920-03-25'),

    ->     (3,'Jon','Pertwee','1919-07-07'),

    ->     (4,'Tom','Baker','1934-01-20');

Query OK, 4 rows affected (0.00 sec)

 

Records: 4  Duplicates: 0  Warnings: 0


slave节点

[root@db01 ~]# mysql -utest_read_only -phello

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 8

Server version: 10.1.13-MariaDB MariaDB Server


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


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


MariaDB [(none)]> USE hello;

Database changed

MariaDB [hello]> SELECT * FROM doctors;

+----+-------------+-----------+------------+

| id | given_names | surname   | birthdate  |

+----+-------------+-----------+------------+

1 | William     | Hartnell  | 1908-01-08 |

2 | Patrick     | Troughton | 1920-03-25 |

3 | Jon         | Pertwee   | 1919-07-07 |

4 | Tom         | Baker     | 1934-01-20 |

+----+-------------+-----------+------------+

4 rows in set (0.00 sec)


MariaDB [hello]> INSERT INTO doctors (given_names,surname,birthdate) VALUES ('test','test_read_only','2016-04-10');

ERROR 1290 (HY000): The MariaDB server is running with the --read-only option so it cannot execute this statement

 

MariaDB [hello]> 



二.多主一从
https://mariadb.com/kb/en/multi-source-replication/
MariaDB <wbr>Replication
db00: 192.168.8.100(master)
db01: 192.168.8.101(master)
db02: 192.168.8.102(slave)
1.创建具有Replication相关权限的数据库用户(所有节点)
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO replicant@'192.168.8.%' IDENTIFIED BY 'foo.123';
2.停止数据库(所有节点)
systemctl stop mysql
3.修改配置文件/etc/my.cnf

db00: 192.168.8.100(master)

[mysqld]

band_address = 192.168.8.100

log-bin = mysql-bin

server-id = 100

relay_log = db00-relay-binlog 


db01: 192.168.8.101(master)

[mysqld]

band_address = 192.168.8.101

log-bin = mysql-bin

server-id = 101

relay-log = db01-relay-binlog


db02: 192.168.8.102(slave)

[mysqld]

band_address = 192.168.8.102

log-bin = mysql-bin

server-id = 102

relay-log = db02-relay-binlog

read-only

replicate_ignore_db = mysql,information_schema,performance_schema 


replicate-do-db  #要作复制的db
replicate-ignore-db  #复制时忽略的db

replicate-do-table 
 #要作复制的table
replicate-ignore-table  #复制时忽略table

4.启动数据库(所有节点)
systemctl start mysql
5.指定多个master节点(slave节点)

CHANGE MASTER 'db00' TO MASTER_HOST='192.168.8.100',

    MASTER_PORT=3306, 

    MASTER_USER = 'replicant',

    MASTER_PASSWORD = 'foo.123',

    MASTER_CONNECT_RETRY=10,

    MASTER_USE_GTID = CURRENT_POS;

CHANGE MASTER 'db01' TO MASTER_HOST='192.168.8.101',

    MASTER_PORT=3306,

    MASTER_USER = 'replicant',

    MASTER_PASSWORD = 'foo.123',

    MASTER_CONNECT_RETRY=10,

    MASTER_USE_GTID = CURRENT_POS;


START ALL SLAVES;

SHOW ALL SLAVES STATUS;

可以单独对某个SLAVE进行相关处理,如:

STOP SLAVE 'db01';

SHOW SLAVE 'db01' STATUS;

START SLAVE 'db01;


MariaDB [(none)]> SHOW ALL SLAVES STATUS\G

*************************** 1. row ***************************

              Connection_name: db00

              Slave_SQL_State: Slave has read all relay log; waiting for the slave I/O thread to update it

               Slave_IO_State: 

                  Master_Host: 192.168.8.100

                  Master_User: replicant

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: 

          Read_Master_Log_Pos: 4

               Relay_Log_File: db02-relay-binlog-db00.000001

                Relay_Log_Pos: 4

        Relay_Master_Log_File: 

             Slave_IO_Running: No

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: mysql,information_schema,performance_schema

           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: 4

              Relay_Log_Space: 249

              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: 1236

                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Error: connecting slave requested to start from GTID 0-102-63, which is not in the master's binlog'

               Last_SQL_Errno: 0

               Last_SQL_Error: 

  Replicate_Ignore_Server_Ids: 

             Master_Server_Id: 100

               Master_SSL_Crl: 

           Master_SSL_Crlpath: 

                   Using_Gtid: Current_Pos

                  Gtid_IO_Pos: 0-102-63

      Replicate_Do_Domain_Ids: 

  Replicate_Ignore_Domain_Ids: 

                Parallel_Mode: conservative

         Retried_transactions: 0

           Max_relay_log_size: 1073741824

         Executed_log_entries: 1

    Slave_received_heartbeats: 0

       Slave_heartbeat_period: 1800.000

               Gtid_Slave_Pos: 0-102-63

*************************** 2. row ***************************

              Connection_name: db01

              Slave_SQL_State: Slave has read all relay log; waiting for the slave I/O thread to update it

               Slave_IO_State: 

                  Master_Host: 192.168.8.101

                  Master_User: replicant

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: 

          Read_Master_Log_Pos: 4

               Relay_Log_File: db02-relay-binlog-db01.000001

                Relay_Log_Pos: 4

        Relay_Master_Log_File: 

             Slave_IO_Running: No

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: mysql,information_schema,performance_schema

           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: 4

              Relay_Log_Space: 249

              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: 1236

                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Error: connecting slave requested to start from GTID 0-102-63, which is not in the master's binlog. Since the master's binlog contains GTIDs with higher sequence numbers, it probably means that the slave has diverged due to executing extra erroneous transactions'

               Last_SQL_Errno: 0

               Last_SQL_Error: 

  Replicate_Ignore_Server_Ids: 

             Master_Server_Id: 101

               Master_SSL_Crl: 

           Master_SSL_Crlpath: 

                   Using_Gtid: Current_Pos

                  Gtid_IO_Pos: 0-102-63

      Replicate_Do_Domain_Ids: 

  Replicate_Ignore_Domain_Ids: 

                Parallel_Mode: conservative

         Retried_transactions: 0

           Max_relay_log_size: 1073741824

         Executed_log_entries: 1

    Slave_received_heartbeats: 0

       Slave_heartbeat_period: 1800.000

               Gtid_Slave_Pos: 0-102-63

 

2 rows in set (0.00 sec)


不巧了,同步失败,正好作为同步失败的案例处理,主要是因为slave读取了一个master不存在的Pos

解决办法,正确指定master的log_file文件,

STOP ALL SLAVES;

CHANGE MASTER 'db00' TO MASTER_HOST='192.168.8.100',

    MASTER_PORT=3306,

    MASTER_USER = 'replicant',

    MASTER_PASSWORD = 'foo.123',

    MASTER_CONNECT_RETRY=10,

    MASTER_LOG_FILE = 'mysql-bin.000003',

    MASTER_LOG_POS = 1159;


CHANGE MASTER 'db01' TO MASTER_HOST='192.168.8.101',

    MASTER_PORT=3306,

    MASTER_USER = 'replicant',

    MASTER_PASSWORD = 'foo.123',

    MASTER_CONNECT_RETRY=10,

    MASTER_LOG_FILE = 'mysql-bin.000004',

    MASTER_LOG_POS = 1777;

START ALL SLAVES;


注意: 

MASTER_LOG_FILE可以在master端SHOW MASTER STATUS;查得

MASTER_LOG_POS需要通过mysqlbinlog工具来查看MASTER_LOG_FILE获取到正确的偏移量,如:

mysqlbinlog /opt/mariadb/data/mysql-bin.000003|grep pos bin_000003.log|tail -1|awk '{print $6"\t"$7}'


MariaDB [(none)]> SHOW ALL SLAVES STATUS\G

*************************** 1. row ***************************

              Connection_name: db00

              Slave_SQL_State: Slave has read all relay log; waiting for the slave I/O thread to update it

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.8.100

                  Master_User: replicant

                  Master_Port: 3306

                Connect_Retry: 10

              Master_Log_File: mysql-bin.000003

          Read_Master_Log_Pos: 1276

               Relay_Log_File: db02-relay-binlog-db00.000002

                Relay_Log_Pos: 654

        Relay_Master_Log_File: mysql-bin.000003

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: mysql,information_schema,performance_schema

           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: 1276

              Relay_Log_Space: 959

              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: 100

               Master_SSL_Crl: 

           Master_SSL_Crlpath: 

                   Using_Gtid: No

                  Gtid_IO_Pos: 0-102-63

      Replicate_Do_Domain_Ids: 

  Replicate_Ignore_Domain_Ids: 

                Parallel_Mode: conservative

         Retried_transactions: 0

           Max_relay_log_size: 1073741824

         Executed_log_entries: 21

    Slave_received_heartbeats: 0

       Slave_heartbeat_period: 1800.000

               Gtid_Slave_Pos: 0-102-64

*************************** 2. row ***************************

              Connection_name: db01

              Slave_SQL_State: Slave has read all relay log; waiting for the slave I/O thread to update it

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.8.101

                  Master_User: replicant

                  Master_Port: 3306

                Connect_Retry: 10

              Master_Log_File: mysql-bin.000004

          Read_Master_Log_Pos: 1900

               Relay_Log_File: db02-relay-binlog-db01.000002

                Relay_Log_Pos: 660

        Relay_Master_Log_File: mysql-bin.000004

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: mysql,information_schema,performance_schema

           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: 1900

              Relay_Log_Space: 965

              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: 101

               Master_SSL_Crl: 

           Master_SSL_Crlpath: 

                   Using_Gtid: No

                  Gtid_IO_Pos: 0-102-63

      Replicate_Do_Domain_Ids: 

  Replicate_Ignore_Domain_Ids: 

                Parallel_Mode: conservative

         Retried_transactions: 0

           Max_relay_log_size: 1073741824

         Executed_log_entries: 41

    Slave_received_heartbeats: 0

       Slave_heartbeat_period: 1800.000

               Gtid_Slave_Pos: 0-102-64

 

2 rows in set (0.00 sec)


6.测试

db00,db01分别建库,建表插入数据等操作查看,slave的同步情况


同步失败解决思路补充:

在Master上
update mysql.user set super_priv = 'Y' where user = 'root'; flush privileges;
在Slave上
停止slave: stop slave;
设置跳过slave同步语句数: set global sql_slave_skip_counter = 1;
启动slave: start slave;
确认同步是否正确: show slave status \G



三.互为主从

1.修改配置文件
db01: 192.168.8.101

[mysqld]

band_address = 192.168.8.101

log-bin = mysql-bin

server-id = 101

relay-log = db01-relay-binlog

replicate_ignore_db = mysql,information_schema,performance_schema 


db02: 192.168.8.102

[mysqld]

band_address = 192.168.8.101

log-bin = mysql-bin

server-id = 101

relay-log = db01-relay-binlog

replicate_ignore_db = mysql,information_schema,performance_schema 

2.启服务

systemctl start mysql

3.创建具有Replication相关权限的数据库用户(所有节点)
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO replicant@'192.168.8.%' IDENTIFIED BY 'foo.123';

4.指定master

db01: 192.168.8.101

CHANGE MASTER TO MASTER_HOST='192.168.8.102',

    MASTER_PORT=3306,   

    MASTER_USER = 'replicant',

    MASTER_PASSWORD = 'foo.123',

    MASTER_CONNECT_RETRY=10,

    MASTER_LOG_FILE = 'mysql-bin.000004',

    MASTER_LOG_POS = 0;

START SLAVE;


db02: 192.168.8.102

CHANGE MASTER TO MASTER_HOST='192.168.8.101',

    MASTER_PORT=3306,   

    MASTER_USER = 'replicant',

    MASTER_PASSWORD = 'foo.123',

    MASTER_CONNECT_RETRY=10,

    MASTER_LOG_FILE = 'mysql-bin.000009',

    MASTER_LOG_POS = 0;

 

START SLAVE;


[root@db01 ~]# mysql -uroot -p

Enter password: 

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 14

Server version: 10.1.13-MariaDB MariaDB Server


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


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


MariaDB [(none)]> SHOW SLAVE STATUS\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.8.102

                  Master_User: replicant

                  Master_Port: 3306

                Connect_Retry: 10

              Master_Log_File: mysql-bin.000004

          Read_Master_Log_Pos: 2474

               Relay_Log_File: db01-relay-binlog.000004

                Relay_Log_Pos: 537

        Relay_Master_Log_File: mysql-bin.000004

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: mysql,information_schema,performance_schema

           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: 2474

              Relay_Log_Space: 1125

              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

               Master_SSL_Crl: 

           Master_SSL_Crlpath: 

                   Using_Gtid: No

                  Gtid_IO_Pos: 

      Replicate_Do_Domain_Ids: 

  Replicate_Ignore_Domain_Ids: 

                Parallel_Mode: conservative

 

1 row in set (0.00 sec)

[root@db02 ~]# mysql -uroot -p

Enter password: 

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 18

Server version: 10.1.13-MariaDB MariaDB Server


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


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


MariaDB [(none)]> SHOW SLAVE STATUS\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.8.101

                  Master_User: replicant

                  Master_Port: 3306

                Connect_Retry: 10

              Master_Log_File: mysql-bin.000009

          Read_Master_Log_Pos: 1145

               Relay_Log_File: db02-relay-binlog.000002

                Relay_Log_Pos: 1433

        Relay_Master_Log_File: mysql-bin.000009

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: 

          Replicate_Ignore_DB: mysql,information_schema,performance_schema

           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: 1145

              Relay_Log_Space: 1733

              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: 101

               Master_SSL_Crl: 

           Master_SSL_Crlpath: 

                   Using_Gtid: No

                  Gtid_IO_Pos: 

      Replicate_Do_Domain_Ids: 

  Replicate_Ignore_Domain_Ids: 

                Parallel_Mode: conservative

 

 

1 row in set (0.00 sec)

5.测试

db01: 192.168.8.101

CREATE DATABASE IF NOT EXISTS db01;

USE db01;

CREATE TABLE doctors (

    id int NOT NULL AUTO_INCREMENT PRIMARY KEY,

    given_names varchar(255),

    surname varchar(255),

    birthdate date);

INSERT INTO doctors VALUES 

    (1,'William','Hartnell','1908-01-08'),

    (2,'Patrick','Troughton','1920-03-25'),

    (3,'Jon','Pertwee','1919-07-07'),

    (4,'Tom','Baker','1934-01-20');


db02: 192.168.8.102

CREATE DATABASE IF NOT EXISTS db02;

USE db02;

CREATE TABLE doctors (

    id int NOT NULL AUTO_INCREMENT PRIMARY KEY,

    given_names varchar(255),

    surname varchar(255),

    birthdate date);

INSERT INTO doctors VALUES 

    (1,'William','Hartnell','1908-01-08'),

    (2,'Patrick','Troughton','1920-03-25'),

    (3,'Jon','Pertwee','1919-07-07'),

    (4,'Tom','Baker','1934-01-20');



MariaDB [db02]> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| db01               |

| db02               |

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

5 rows in set (0.00 sec)


MariaDB [db02]> SELECT * FROM db01.doctors;

+----+-------------+-----------+------------+

| id | given_names | surname   | birthdate  |

+----+-------------+-----------+------------+

1 | William     | Hartnell  | 1908-01-08 |

2 | Patrick     | Troughton | 1920-03-25 |

3 | Jon         | Pertwee   | 1919-07-07 |

4 | Tom         | Baker     | 1934-01-20 |

+----+-------------+-----------+------------+

4 rows in set (0.00 sec)


MariaDB [db02]> SELECT * FROM db02.doctors;

+----+-------------+-----------+------------+

| id | given_names | surname   | birthdate  |

+----+-------------+-----------+------------+

1 | William     | Hartnell  | 1908-01-08 |

2 | Patrick     | Troughton | 1920-03-25 |

3 | Jon         | Pertwee   | 1919-07-07 |

4 | Tom         | Baker     | 1934-01-20 |

+----+-------------+-----------+------------+

 

4 rows in set (0.00 sec)



四.增强型bin-log row注释

db01: 192.168.8.101(master)
db02: 192.168.8.102(slave)

1.修改配置文件/etc/my.cnf
db01: 192.168.8.101(master)
[mysqld]
binlog_format = row
binlog_annotate_row_events 

db02: 192.168.8.102(slave)
[mysqld]
binlog_format = row
replicate_annotate_row_events 
2.重启
systemctl restart mysql
3.在master上操作
DROP DATABASE IF EXISTS test;
CREATE DATABASE test;
USE test;
CREATE TABLE t1(a char(1));
INSERT INTO t1 VALUES ('a'),('b'),('c'),('d');
CREATE TABLE t2(a char(1));
INSERT INTO t2 VALUES ('a'),('b'),('c'),('d');
CREATE TABLE t3(a char(1));
INSERT DELAYED INTO t3 VALUES ('a'),('b'),('c'),('d');
DELETE t1, t2 FROM  t1 INNER JOIN t2 INNER JOIN t3
    WHERE t1.a=t2.a and t2.a=t3.a;
    
SHOW BINLOG EVENTS;
4.查看最新的binlog

mysqlbinlog /opt/mariadb/data/mysql-bin.000005

因为加入了注释,所以查找语句或者事件更为方便,当然,某些情况下,不希望看到这些注释时,可以加上--skip-annotate-row-events参数

[root@db01 ~]# mysqlbinlog /opt/mariadb/data/mysql-bin.000005 >annoate.sql

[root@db01 ~]# mysqlbinlog --skip-annotate-row-events /opt/mariadb/data/mysql-bin.000005 >no_annoate.sql

[root@db01 ~]# diff no_annoate.sql annoate.sql 

62a63,64

> #160410  0:42:34 server id 101  end_log_pos 806 Annotate_rows:

> #Q> INSERT INTO t1 VALUES ('a'),('b'),('c'),('d')

88a91,92

> #160410  0:42:34 server id 101  end_log_pos 1146 Annotate_rows:

> #Q> INSERT INTO t2 VALUES ('a'),('b'),('c'),('d')


五.bin log event校验

db01: 192.168.8.101(master)
db02: 192.168.8.102(slave)

1.master节点
SET GLOBAL binlog_checksum = 1;
SET GLOBAL master_verify_checksum = 1; 
[mysqld]
binlog_checksum = 1
master_verify_checksum = 1

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

+---------------------------+-------+

| Variable_name             | Value |

+---------------------------+-------+

| aria_page_checksum        | ON    |

| binlog_checksum           | CRC32 |

| master_verify_checksum    | ON    |

| slave_sql_verify_checksum | ON    |

+---------------------------+-------+

4 rows in set (0.00 sec)


2.slave 节点(默认开启)
SET GLOBAL slave_sql_verify_checksum = 1; 
[mysqld]
slave_sql_verify_checksum = 1

mysqlbinlog可以通过--verify-binlog-checksum参数来校验 

六.选择性地跳过binlog event复制
db01: 192.168.8.101(master)
db02: 192.168.8.102(slave)
适用场景,master中的某些临时库或者表,不希望同步到slave时,可以选择性的跳过复制

1.master节点,开启skip_replication
SET @@skip_replication=1;

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

+------------------+-------+

| Variable_name    | Value |

+------------------+-------+

| skip_replication | ON    |

+------------------+-------+

1 row in set (0.00 sec)

CREATE DATABASE w;
2.slave节点
STOP SLAVE 'db01';
SET GLOBAL replicate_events_marked_for_skip  = FILTER_ON_MASTER;
START SLAVE 'db01';
3.master节点
CREATE DATABASE wx;
4.slave节点
STOP SLAVE 'db01';
SET GLOBAL replicate_events_marked_for_skip  = FILTER_ON_SLAVE;
START SLAVE 'db01';
5.master节点
CREATE DATABASE wxy;
6.slave节点
STOP SLAVE 'db01';
SET GLOBAL replicate_events_marked_for_skip  = REPLICATE;
START SLAVE 'db01';
7.master节点
CREATE DATABASE wxyz;
8.slave节点
SHOW DATABASES;

MariaDB [(none)]> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| hello              |

| information_schema |

| mysql              |

| pe                 |

| performance_schema |

| temp               |

| test               |

| w                  |

| wxyz               |

+--------------------+

9 rows in set (0.00 sec)

说明:

replicate_events_marked_for_skip 的默认值为REPLICATE,任何event都会执行复制动作

值为FILTER_ON_MASTER,FILTER_ON_SLAVE时结果都是不执行复制动作,唯一的区别是在SLAVE过滤时经过了网络传输

9.master节点,关闭skip_replication
SET @@skip_replication=0;

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

+------------------+-------+

| Variable_name    | Value |

+------------------+-------+

| skip_replication | OFF   |

+------------------+-------+

1 row in set (0.00 sec)

posted @ 2016-04-09 21:59  李庆喜  阅读(583)  评论(0编辑  收藏  举报