mysql> show variables like '%server_id%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 25152 |
+---------------+-------+
1 row in set (0.00 sec)
从库的server-id是
mysql> show variables like '%server_id%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 25250 |
+---------------+-------+
1 row in set (0.00 sec)
主库建库,建表,插入数据:
mysql> create database yayun;
Query OK, 1 row affected (0.00 sec)
mysql> create table yayun.tb1 ( id int, age int, name char(20), primary key(id) );
Query OK, 0 rows affected (0.07 sec)
mysql> use yayun
Database changed
mysql> insert into tb1 (id,age,name)values(1,18,'aa');
Query OK, 1 row affected (0.00 sec)
mysql> insert into tb1 (id,age,name)values(2,18,'bb');
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb1;
+----+------+------+
| id | age | name |
+----+------+------+
| 1 | 18 | aa |
| 2 | 18 | bb |
+----+------+------+
2 rows in set (0.00 sec)
mysql>
从库查询:
mysql> select * from tb1;
+----+------+------+
| id | age | name |
+----+------+------+
| 1 | 18 | aa |
| 2 | 18 | bb |
+----+------+------+
2 rows in set (0.00 sec)
mysql>
此时数据是一致的。
接下来在从库备份数据,并且记录pos点。(这里模拟的是从库每天进行的备份)
+----+------+-------+
| id | age | name |
+----+------+-------+
| 1 | 18 | yayun |
| 2 | 18 | bb |
| 3 | 19 | cc |
+----+------+-------+
我们实际查询一下:
mysql> select * from tb1;
+----+------+------+
| id | age | name |
+----+------+------+
| 1 | 18 | aa |
| 2 | 18 | bb |
+----+------+------+
2 rows in set (0.00 sec)
mysql>
卧槽,发生了什么,怎么数据少了,而且id等于1的name字段结果也不一样?
下面我们看看原来老的从库的binlog
#160908 13:45:57 server id 25152 end_log_pos 4239 Query thread_id=16 exec_time=0 error_code=0
SET TIMESTAMP=1473313557/*!*/;
insert into tb1 (id,age,name)values(3,19,'cc')
/*!*/;
# at 4239
#160908 13:45:57 server id 25152 end_log_pos 4266 Xid = 160
COMMIT/*!*/;
# at 4266
#160908 13:46:20 server id 25152 end_log_pos 4325 Query thread_id=16 exec_time=0 error_code=0
SET TIMESTAMP=1473313580/*!*/;
BEGIN
/*!*/;
# at 4325
#160908 13:46:20 server id 25152 end_log_pos 4427 Query thread_id=16 exec_time=0 error_code=0
SET TIMESTAMP=1473313580/*!*/;
update tb1 set name='yayun' where id=1
/*!*/;
# at 4427
#160908 13:46:20 server id 25152 end_log_pos 4454 Xid = 162
COMMIT/*!*/;
DELIMITER ;
可以看见有insert,update,但是server id都是25152,也就是主库的。这也就是为什么少了数据的原因。开头也提到过了。
如果我们在新的主库上面进行update,如果这条记录在从库没有存在,而且主从的binlog是row模式,那么就会触发1032错误,复制将中断,由于我的是mixed模式,同步一直没有报错,没有早发现问题。我update语句加limit就会触发row模式,下面我们试试。
主库: