mysql主從複製與資料同步Slave_IO_Running錯誤

來源:互聯網
上載者:User

mysql主從複製


怎麼安裝mysql資料庫,這裡不說了,只說它的主從複製,步驟如下:

1、主從伺服器分別作以下操作:
1.1、版本一致
1.2、初始化表,並在後台啟動mysql
1.3、修改root的密碼

2、修改主伺服器master:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin   //[必須]啟用二進位日誌
server-id=108       //[必須]伺服器唯一ID,預設是1,一般取IP最後一段

3、修改從伺服器slave:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin   //[必須]啟用二進位日誌
server-id=3       //[必須]伺服器唯一ID,預設是1,一般取IP最後一段

4、重啟兩台伺服器的mysql
/etc/init.d/mysql restart

5、在主伺服器上建立帳戶並授權slave:
#/usr/local/mysql/bin/mysql -uroot -pmttang
mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456'; //一般不用root帳號,“%”表示所有用戶端都可能連,只要帳號,密碼正確,此處可用具體用戶端IP代替,如192.168.1.3,加強安 全。

6、登入主伺服器的mysql,查詢master的狀態
mysql>show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      251 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
註:執行完此步驟後不要再操作主伺服器MYSQL,防止主伺服器狀態值變化

7、配置從伺服器Slave:
mysql>change master to aster_host='192.168.145.222',master_user='mysync',master_password='q123456',
master_log_file='mysql-bin.000001',master_log_pos=251;   //注意不要斷開,“251”無單引號。

Mysql>start slave;    //啟動從伺服器複製功能

8、檢查從伺服器複製功能狀態:

mysql> show slave statusG

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

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.2.222  //主伺服器地址

Master_User: mysync         //授權帳戶名稱,盡量避免使用root

Master_Port: 3306           //資料庫連接埠,部分版本沒有此行

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 600        //#同步讀取二進位日誌的位置,大於等於>=Exec_Master_Log_Pos

Relay_Log_File: ddte-relay-bin.000003

Relay_Log_Pos: 251

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Yes       //此狀態必須YES

Slave_SQL_Running: Yes       //此狀態必須YES
......

註:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,否則都是錯誤的狀態(如:其中一個NO均屬錯誤(:錯誤解答))。

以上操作過程,主從伺服器配置完成。

9、主從伺服器測試:

主伺服器Mysql,建立資料庫,並在這個庫中建表插入一條資料:

mysql> create database hi_db;
Query OK, 1 row affected (0.00 sec)

mysql> use hi_db;
Database changed

mysql>  create table hi_tb(id int(3),name char(10));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into hi_tb values(001,'wf');
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hi_db              |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)

從伺服器Mysql查詢:

mysql> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| hi_db              |          //I'M here,大家看到了吧
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> use hi_db
Database changed
mysql> select * from hi_tb;    //可以看到在主伺服器上新增的具體資料
+------+------+
| id   | name |
+------+------+
|    1 | wf |
+------+------+
1 row in set (0.00 sec)
10、完成:
編寫一shell指令碼,用nagios監控slave的兩個“yes”,如發現只有一個或零個“yes”,就表明主從有問題了,發簡訊警報吧。


mysql 資料同步 出現Slave_IO_Running:No問題的解決方案小結


mysql replication 中slave機器上有兩個關鍵的進程,死一個都不行,一個是slave_sql_running,一個是Slave_IO_Running,一個負責與主機的io通訊,一個負責自己的slave mysql進程。
下面寫一下,這兩個要是有no了,怎麼恢複。。

如果是slave_io_running no了,那麼就我個人看有三種情況,一個是網路有問題,串連不上,像有一次我用虛擬機器搭建replication,使用了nat的網路結構,就是死都連不 上,第二個是有可能my.cnf有問題,設定檔怎麼寫就不說了,網上太多了,最後一個是授權的問題,replication slave和file許可權是必須的。如果不怕死就all咯。。

一旦io為no了先看err日誌,看看爆什麼錯,很可能是網路,也有可能是包太大收不了,這個時候主備上改max_allowed_packet這個參數。

如 果是slave_sql_running no了,那麼也有兩種可能,一種是slave機器上這個表中出現了其他的寫操作,就是程式寫了,這個是會有問題的,今天我想重現,但是有時候會有問題,有 時候就沒有問題,現在還不是太明了,後面再更新,還有一種占絕大多數可能的是slave進程重啟,交易回復造成的,這也是mysql的一種自我保護的措 施,像關鍵時候唯讀一樣。

這個時候想恢複的話,只要停掉slave,set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;再開一下slave就可以了,這個全域變數賦值為N的意思是:

This statement skips the next N events from the master. This is useful for recovering from replication stops caused by a statement.

This statement is valid only when the slave thread is not running. Otherwise, it produces an error.

呵呵,講的比我清楚。

MYSQL鏡像伺服器因錯誤停止的恢複

下午主伺服器,由於一些原因,導致死機,重啟後,發現從伺服器的資料沒有跟上。
配好MYSQL主從也才前幾天的事,沒多少經驗,第一次碰上這問題,有點焦急。不過,自己試了下,還算解決了:)

從伺服器上
Master_Log_File: mysqlhxmaster.000007
Read_Master_Log_Pos: 84285377

看一下主伺服器:mysqlhxmaster.000007 | 84450528 |
已經過後很多了,確實沒跟上。

show slave statusG
Slave_IO_Running: Yes
Slave_SQL_Running: No

有問題了,Slave_SQL_Running應該是Yes才對。

再往下看,有錯誤的提示:

Last_Errno: 1053
Last_Error: Query partially completed on the master (error on master: 1053) and was aborted. There is a chance that your master is inconsistent at this point. If you are sure that your master is ok, run this query manually on the slave and then restart the slave with SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE; . Query: 'INSERT INTO hx_stat_record ......(一句SQL語句)'

這裡有說明要怎麼操作了:)

先stop slave,然後執行了一下提示的語句,再SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE;

show slave statusG

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

OK了,從伺服器也在幾分鐘內把堆積的log處理完了,兩邊又同步了:)

從MYSQL伺服器Slave_IO_Running: No的解決2

早晨機房意外斷電,導致了發現mysql從伺服器同步異常。使用以前碰到的Slave_SQL_Running為No的解決辦法無效,仍然無法同步。

查看一下狀態show slave status
Master_Log_File: mysqlmaster.000079
Read_Master_Log_Pos: 183913228
Relay_Log_File: hx-relay-bin.002934
Relay_Log_Pos: 183913371
Relay_Master_Log_File: mysqlmaster.000079
Slave_IO_Running: No
Slave_SQL_Running: Yes

主伺服器show master statusG
File: mysqlmaster.000080
Position: 13818288
Binlog_Do_DB:
Binlog_Ignore_DB: mysql,test

mysql錯誤記錄檔:
100512 9:13:17 [Note] Slave SQL thread initialized, starting replication in log 'mysqlmaster.000079' at position 183913228, relay log './hx-relay-bin.002934' position: 183913371
100512 9:13:17 [Note] Slave I/O thread: connected to master 'replicuser@192.168.1.21:3306', replication started in log 'mysqlmaster.000079' at position 183913228
100512 9:13:17 [ERROR] Error reading packet from server: Client requested master to start replication from impossible position ( server_errno=1236)
100512 9:13:17 [ERROR] Got fatal error 1236: 'Client requested master to start replication from impossible position' from master when reading data from binary log
100512 9:13:17 [Note] Slave I/O thread exiting, read up to log 'mysqlmaster.000079', position 183913228

這次是Slave_IO_Running為No,從日誌上來看,伺服器讀mysqlmaster.000079這個Log的183913228這個位置時發生錯誤,這個位置不存在,於是無法同步。

查看一下這個Log的最後幾行:
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
# at 4
#100511 9:35:15 server id 1 end_log_pos 98 Start: binlog v 4, server v 5.0.27-standard-log created 100511 9:35:15
# Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.

嘗試從損壞之前的位置開始
SLAVE STOP;
CHANGE MASTER TO MASTER_LOG_FILE='mysqlcncnmaster.000079', MASTER_LOG_POS=183913220;
SLAVE START;
無效!
只好從新的日誌開始
SLAVE STOP;
CHANGE MASTER TO MASTER_LOG_FILE='mysqlcncnmaster.000080', MASTER_LOG_POS=0;
SLAVE START;
此時Slave_IO_Running恢複為Yes,同步進行了!觀察了會兒,沒有任何出錯跡象,問題解決。

另外,出現Slave_IO_Running:NO還有一個原因是slave上沒有許可權讀master上的資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.