程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> Mysql互為主從問題--日志同步數據不同步

Mysql互為主從問題--日志同步數據不同步

編輯:MySQL綜合教程

我搭建的是mysql 互為主從 復制

兩台機器的mysql環境完全相同

 

第一部分測試:

B為master  A為slave的同步測試


在B上創建表lian,並插入數據
mysql> create table lian (a int,b char(10));

mysql> insert into lian (a,b)values(22,'hahah');


mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| lian           |
+----------------+
mysql> select * from lian;
+------+-------+
| a    | b     |
+------+-------+
|   22 | hahah |
+------+-------+

查看一下master-B的binlog日志,查看以上操作是否記錄了日志:
cat mysql-bin.000002                                                                                                                                                                                  
.?Nh?@stdtestcreate table lian (a int,b char(10))??Nl>@stdtestinsert into lian (a,b)values(22,'hahah')

現在查看slave-A的relay日志,發現日志已經同步了
[root@XKWB5510 var]# cat XKWB5510-relay-bin.000003                                                                                                                                                     
.?Nh?@stdtestcreate table lian (a int,b char(10))??Nl>@stdtestinsert into lian (a,b)values(22,'hahah')

再在slave-A上看一下數據庫是不是存在lian這個表:
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| aniya          |
| lian           |
+----------------+
2 rows in set (0.00 sec)

 

現在說明數據B A 主 從 同步成功
---------------------------------------------------------------------------
第二部分測試:

A為master  B為slave的同步測試

 

在A上創建表From246,並插入數據
mysql> use test;
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| A246           |
| aniya          |
| lian           |
+----------------+
3 rows in set (0.00 sec)

mysql> create table From246(Name varchar(255),Sex varchar(255),Age int(10));
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| A246           |
| From246        |
| aniya          |
| lian           |
+----------------+
4 rows in set (0.00 sec)

mysql> insert into From246 (Name,Sex,Age)values('Zhaoyj','Girl',24);
mysql> select * from From246;
+--------+------+------+
| Name   | Sex  | Age  |
+--------+------+------+
| Zhaoyj | Girl |   24 |
+--------+------+------+
1 row in set (0.00 sec)


查看master-A的binlog日志,證明上述操作成功
[root@XKWB5510 var]# tail -1  mysql-bin.000002
testcreate table From246(Name varchar(255),Sex varchar(255),Age int(10))?N?R@stdtestinsert into From246 (Name,Sex,Age)values('Zhaoyj','Girl',24)

查看master-A的日志狀態
[root@XKWB5510 var]# /usr/local/mysql/bin/mysqlbinlog mysql-bin.000003 |tail -15
/*!*/;
# at 702
#110926 14:01:51 server id 1  end_log_pos 838  Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1317016911/*!*/;
create table From246(Name varchar(255),Sex varchar(255),Age int(10))
/*!*/;
# at 838
#110926 14:02:05 server id 1  end_log_pos 966  Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1317016925/*!*/;
insert into From246 (Name,Sex,Age)values('Zhaoyj','Girl',24)
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;


查看relay-B的日志,同步日志成功
testcreate table From246(Name varchar(255),Sex varchar(255),Age int(10))?N?R@stdtestinsert into From246 (Name,Sex,Age)values('Zhaoyj','Girl',24)[root@XKWB5705 var]

查看relay-B日志狀態,可以看到日志已經同步
[root@XKWB5705 var]# /usr/local/mysql/bin/mysqlbinlog XKWB5705-relay-bin.000005|tail -13
/usr/local/mysql/bin/mysqlbinlog: Character set '#28' is not a compiled character set and is not specified in the '/usr/local/mysql/share/mysql/charsets/Index.xml' file
#110926 14:01:51 server id 1  end_log_pos 838  Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1317016911/*!*/;
create table From246(Name varchar(255),Sex varchar(255),Age int(10))
/*!*/;
# at 853
#110926 14:02:05 server id 1  end_log_pos 966  Query thread_id=5 exec_time=0 error_code=0
SET TIMESTAMP=1317016925/*!*/;
insert into From246 (Name,Sex,Age)values('Zhaoyj','Girl',24)
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;


但是數據卻沒有插入到relay-B的數據庫
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| lian           |
+----------------+
1 row in set (0.00 sec)

當我刪除master-A上的表時,relay-B日志也同步了
[root@XKWB5705 var]# tail -4 XKWB5705-relay-bin.000005                                                                                                                                                  
??NS?@stdtestdrop table A246??NT@stdtestdrop table aniya??NSd@stdtestdrop table lian??NV?@stdtestdrop table From246

 

這是什麼奇怪問題 ??
------------------------------------------------------------------------------
問題排查:

首先在Master-A上用

show processlist; 查看下進程是否Sleep太多。發現很正常

show master status; 也正常

再跑到Slave上查看 show slave status; 也正常


當我手動從A導入B數據時,發現一個問題:
mysql> load table From246 from master;
ERROR 1115 (42000): Unknown character set: 'gbk'

懷疑:難道是因為字符串的問題導致AB主從復制失敗 ?


通過show character set 命令查看到
master-A有gbk字符集而slave-B沒有
mysql> show character set;
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| dec8     | DEC West European           | dec8_swedish_ci     |      1 |
| cp850    | DOS West European           | cp850_general_ci    |      1 |
| hp8      | HP West European            | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                    | ascii_general_ci    |      1 |
| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 |
| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 |
| greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 |
| cp1250   | Windows Central European    | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |
| cp866    | DOS Russian                 | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  |      1 |
| macce    | Mac Central European        | macce_general_ci    |      1 |
| macroman | Mac West European           | macroman_general_ci |      1 |
| cp852    | DOS Central European        | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic          | latin7_general_ci   |      1 |
| cp1251   | Windows Cyrillic            | cp1251_general_ci   |      1 |
| cp1256   | Windows Arabic              | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic              | cp1257_general_ci   |      1 |
| binary   | Binary pseudo charset       | binary              |      1 |
| geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |
+----------+-----------------------------+---------------------+--------+
27 rows in set (0.00 sec)


那現在應該是在啟動mysql的時候統一他們的字符集
master-A : [root@XKWB5510 var]# /usr/local/mysql/bin/mysqld_safe --default-character-set=latin1 &
slave-B   : [root@XKWB5705 var]# /usr/local/mysql/bin/mysqld_safe --default-character-set=latin1 &

再次在B上從A導入數據:
mysql> show tables;
Empty set (0.00 sec)

mysql> load table From246 from master;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| From246        |
+----------------+
1 row in set (0.00 sec)

現在字符集的問題解決了

---------------------------------------------------------------


現在手動啟動一下“將日志應用於數據庫”的線程:SLAVE start SQL_THREAD
和“把master段的日志寫到本地”的線程:SLAVE start IO_THREAD

發現同步數據還是失敗,那說明不是線程的問題

如果發現 Seconds_Behind_Master 為 (null)
解決:
stop slave;
set global sql_slave_skip_counter =1 ;
start slave;
之後Slave會和Master去同步 主要看Seconds_Behind_Master是否為0,直到為0時就已經同步了。。

 


-----------------------------------
slave B機器上master.info信息,與master A上的信息是否是同步的

mater A:
mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000004
        Position: 808
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)

slave B:
[root@XKWB5705 var]# cat master.info
15
mysql-bin.000004
808
211.100.97.246
repl2
123456
3306
60
0

從以上可以看到是同步的

 

到目前為止我都沒發現到底是什麼問題導致“日志同步數據不同步” 很是納悶

向高手們請教!!!!

摘自:ANLJF的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved