天天看點

mysql 添加第N個從庫

某項目mysql伺服器 1主 1從 ,現在要添加一個mysql從伺服器,要求主庫不能停止服務,以前由于不是線上的伺服器,可以在主伺服器上 執行 flush tables with read lock 語句(鎖表,隻讀),所有的表隻能讀不能寫,然後再拷貝主庫資料到新的從庫伺服器上,并保持資料一緻性,現在隻能換一種方法了,思路 新從庫2拷貝老的從庫1的資料!

老從庫1 相關操作:

#1 停止 mysql從庫,鎖表,

     記住 Read_Master_Log_Pos: 與 Master_Log_File: (紅色字)

mysql> stop slave; 

mysql> flush tables with read lock; 

mysql> show slave status\G; 

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

               Slave_IO_State: 

                  Master_Host: 192.168.6.53 

                  Master_User: dongnan 

                  Master_Port: 3306 

                Connect_Retry: 60 

              Master_Log_File: mysql-bin.000097 

          Read_Master_Log_Pos: 19157395 

               Relay_Log_File: zabbix-slave-relay-bin.000185 

                Relay_Log_Pos: 11573578 

        Relay_Master_Log_File: mysql-bin.000097 

             Slave_IO_Running: No 

            Slave_SQL_Running: No 

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

              Relay_Log_Space: 19142103 

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

                Last_IO_Error: 

               Last_SQL_Errno: 0 

               Last_SQL_Error: 

1 row in set (0.00 sec) 

ERROR: 

No query specified 

#2 打包資料并發送到新從庫2伺服器上

cd /usr/local/mysql/var/ 

tar czvf zabbix_20110805.tar.gz zabbix/ 

scp zabbix_20110805.tar.gz [email protected]:/root 

新從庫2相關操作:

#1 更改 server-id 值不能為1,因為master 的 server-id=1 

vim /etc/my.cnf  

server-id = 3

#2 停止mysql資料庫并解壓縮到/usr/local/mysql/var/  

tar zxvf zabbix_20110805.tar.gz -C /usr/local/mysql/var/

#3 啟動mysql資料庫并change master

Read_Master_Log_Pos 值  19157395 

Master_Log_File 值 mysql-bin.000097

mysql> change master to master_host='192.168.6.53',master_user='dongnan',master_password='password',master_log_file='mysql-bin.000097',master_log_pos=19157395; 

mysql> slave start;  #啟動slave

mysql> show slave status\G;  #顯示slave 狀态

               Slave_IO_State: Waiting for master to send event 

          Read_Master_Log_Pos: 21194359 

               Relay_Log_File: db1-relay-bin.000002 

                Relay_Log_Pos: 2037215 

             Slave_IO_Running: Yes 

            Slave_SQL_Running: Yes 

          Exec_Master_Log_Pos: 21194359 

              Relay_Log_Space: 2037368 

        Seconds_Behind_Master: 0 

驗證重庫是否同步:

老從庫1

mysql> slave start; 

新從庫2

結束

既然主庫不能動,那就去操作從庫吧,新從庫2拷貝老的從庫1的資料!

本文轉自 dongnan 51CTO部落格,原文連結:http://blog.51cto.com/dngood/742875

繼續閱讀