乐趣区

MySQL-主从同步架构中你不知道的坑上

以下操作针对指定不同步库

binlog-format=ROW 模式

查看主从的 binlog 模式

mysql> show slave status\G

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

Slave_IO_State: Waiting for master to send event

  Master_Host: 192.168.22.171

  Master_User: rep

  Master_Port: 3306

Connect_Retry: 60

Master_Log_File: master-bin.000004

Read_Master_Log_Pos: 463

Relay_Log_File: s1.000011

Relay_Log_Pos: 630

Relay_Master_Log_File: master-bin.000004

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB: test

查看从库状态也显示此项配置

1:在忽略不同步的库不存在的情况下

在配置文件指定主从库都不存在的库,然后在主库中创建这个库,测试数据是否同步过去

主库创建数据库

mysql> create database test;

Query OK, 1 row affected (0.06 sec) 

mysql> show databases;

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

| Database     |

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

| information_schema |

| mysql       |

| performance_schema |

| sys        |

| test        |

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

5 rows in set (0.01 sec)

从库查看

mysql> show databases;

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

| Database      |

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

| information_schema |

| mysql       |

| performance_schema |

| sys        |

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

5 rows in set (0.01 sec)

结论:发现建库的动作没有被从库同步过来

2:测试主从服务器库存在数据同步情况

目前主从库都存在库 test001

[root@mysql-m ~]# mysql -uroot -p -e "show databases;"|grep test001

Enter password:

test001

[root@mysql-s ~]# mysql -uroot -p -e "show databases;"|grep test001

Enter password:

test001

在主库插入数据测试从库同步情况

mysql> use test001;

Database changed

mysql> create table test (-> id varchar(10) not null,

    -> name varchar(10) not null

    -> );

Query OK, 0 rows affected (0.06 sec)

mysql> insert into test values('zhang','man');

Query OK, 1 row affected (0.03 sec)

mysql> select * from test;

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

| id  | name |

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

| zhang | man |

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

1 row in set (0.00 sec)

mysql> select * from test001.test;

ERROR 1146 (42S02): Table 'test001.test' doesn't exist

mysql> use test001;

Database changed

mysql> show tables;

Empty set (0.00 sec)

结论:从库没有表,同样也有数据同步过来

3:征对数据库默认的库同步(如 mysql)

replicate-ignore-db = mysql  

#忽略指定不同步的库

测试前的数据

[root@mysql-m ~]# mysql -uroot -p -e "select user,host from mysql.user;"

Enter password:

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

| user    | host     |

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

| mysql.sys | localhost   |

| root   | localhost  |

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

[root@mysql-s ~]# mysql -uroot -p -e "select user,host from mysql.user;"

Enter password:

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

| user   | host     |

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

| mysql.sys | localhost  |

| root   | localhost  |

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

[root@mysql-m ~]# mysql -uroot -p -e "grant all privileges on *.* to test@localhost identified by'123456';"

Enter password:

[root@mysql-m ~]# mysql -uroot -p -e "select user,host from mysql.user;"

Enter password:

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

| user   | host    |

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

| mysql.sys | localhost  |

| root   | localhost  |

| test   | localhost  |

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

[root@mysql-s ~]# mysql -uroot -p -e "select user,host from mysql.user;"

Enter password:

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

| user   | host     |

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

| mysql.sys | localhost  |

| root   | localhost  |

| test   | localhost  |

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

结论:此种模式下此配置对默认的库不生效,因此调整配置

replicate-ignore-db = mysql     

#忽略指定不同步的库

replicate-wild-ignore-table=mysql.%

#忽略指定不同步的库的所有表

[root@mysql-m ~]# mysql -uroot -p -e "grant all privileges on *.* to testuser@localhost identified by'123456';"

Enter password:

[root@mysql-m ~]# mysql -uroot -p -e "select user,host from mysql.user;"

Enter password:

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

| user   | host    |

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

| mysql.sys | localhost  |

| root   | localhost  |

| test   | localhost  |

| testuser | localhost  |

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

[root@mysql-s ~]# mysql -uroot -p -e "select user,host from mysql.user;"

Enter password:

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

| user   | host     |

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

| mysql.sys | localhost   |

| root   | localhost   |

| test   | localhost   |

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

结论:此种模式下此配置对默认的库进行不同步数据库,需要增加相关表的参数,最后测试得出不加 replicate-ignore-db = mysql 参数,同样对默认的库生效,

binlog-format=STATEMENT 模式

修改主从库的 binlog 模式


一起测试不存在的库、存在的库与默认库

replicate-ignore-db = test 

#忽略指定不同步的库(主从不存在)replicate-ignore-db = test001 

#忽略指定不同步的库

replicate-wild-ignore-table=mysql.% 

#忽略指定不同步的库的所有表 

1:测试默认库数据同步

mysql> grant all privileges on *.* to user@'localhost' identified by '123456';

Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> select user,host from mysql.user;

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

| user   | host     |

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

| rep   | %       |

| mysql.sys | localhost  |

| root   | localhost  |

| test   | localhost  |

| user   | localhost  |

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

5 rows in set (0.01 sec)

[root@mysql-s ~]# mysql -uroot -p -e "select user,host from mysql.user;"

Enter password:

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

| user   | host     |

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

| rep    | %       |

| mysql.sys | localhost  |

| root   | localhost  |

| test   | localhost  |

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

结论:在此种模式下,对于默认的库,配置 replicate-ignore-db = mysql,对默认的库与库中的表都生效

2:测试主从服务器库已经同步的情况下数据同步(test001)

mysql> use test001;

Database changed

mysql> create table test001 (-> id varchar(10) not null,

    -> name varchar(10) not null

    -> );

Query OK, 0 rows affected (0.06 sec)

mysql> insert into test001 values('zhang','man');

Query OK, 1 row affected (0.11 sec) 

[root@mysql-m ~]# mysql -uroot -p -e "select * from test001.test001;"

Enter password:

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

| id  | name |

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

| zhang | man  |

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

[root@mysql-s ~]# mysql -uroot -p -e "select * from test001.test001;"

Enter password:

ERROR 1146 (42S02) at line 1: Table 'test001.test001' doesn't exist

点击 –> 民工哥技术之路 关注微信公众号,对话框回复关键字:1024 可以获取一份最新整理的 2048G 技术干货:包括系统运维、数据库、redis、MogoDB、电子书、Java 基础课程、Java 实战项目、架构师综合教程、架构师实战项目、大数据、Docker 容器、ELK Stack、机器学习、BAT 面试精讲视频等。

退出移动版