源碼包mysqlAB複製(資料同步)

來源:互聯網
上載者:User


環境介紹:  主伺服器: IP 192.168.1.70

           從伺服器: IP 192.168.1.71


主伺服器配置:


 修改許可權:

chown -R root /usr/local/mysql51/*

chown -R mysql /usr/local/mysql51/var

chown -R mysql /usr/local/mysql51/*


修改my.cnf

 vim my.cnf

server-id = 1



查看bin-log是否開啟,修改之後必須重啟Mysql服務

log-bin=mysql-bin

/mysql/mysql.server restart --user=root



建立一個從資料庫的賬戶,(用於資料同步)使用REPLICATION SLAVE賦予許可權,如:

mysql> grant replication slave,reload,super on *.* to "slave"@"192.168.1.71" identified by "slave";


授權登陸使用者:

grant all on to *.* "xcy"@"192.168.1.70" identified by "xcy" with grant option;

grant all on to  *.* "xcy"@"192.168.1.71" identified by "xcy" with grant option;



建立好了可以檢查授權使用者:

mysql> select Host,User,password from mysql.user;

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

| Host         | User      | password                                  |

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

| 192.168.1.71 | slave     | *51125B3597BEE0FC43E0BCBFEE002EF8641B44CF |

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



查看master的狀態

mysql> show master status;                                                            

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000007 |      468 |              |                  |

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

1 row in set (0.00 sec)


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

從伺服器配置:


修改許可權:

chown -R root /usr/local/mysql51/*

chown -R mysql /usr/local/mysql51/var

chgrp -R mysql /usr/local/mysql51/*


修改my.cnf


vim my.cnf

server-id = 2

查看bin-log是否開啟

log-bin=mysql-bin

授權登陸使用者:>授權登陸使用者:

grant all on  *.* "xcy"@"192.168.1.70" identified by "xcy" with grant option;

grant all on  *.* "xcy"@"192.168.1.71" identified by "xcy" with grant option;


重啟mysql

/mysql/mysql.server restart --user=root


建立從伺服器串連主伺服器的串連


CHANGE MASTER TO

      MASTER_HOST='192.168.1.70',

      MASTER_USER='slave',

      MASTER_PASSWORD='slave',

      MASTER_LOG_FILE='mysql-bin.000007',

      MASTER_LOG_POS=468;



啟動從資料庫的slave

  mysql> slave start;


查看 是否同步;

mysql> show slave status\G

            Slave_IO_State: Waiting for master to send event

                 Master_Host: 192.168.1.70

                 Master_User: slave

                 Master_Port: 3306

               Connect_Retry: 60

             Master_Log_File: mysql-bin.000007

         Read_Master_Log_Pos: 468

              Relay_Log_File: linux-relay-bin.000002

               Relay_Log_Pos: 613

       Relay_Master_Log_File: mysql-bin.000007

            Slave_IO_Running: Yes

           Slave_SQL_Running: Yes

             Replicate_Do_DB:

         Replicate_Ignore_DB:



需要注意的是 重啟mysql 以後 master的 狀態 會變動,建立串連的時候需要注意

如果改變了 可以修改串連檔案;    

change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98;


如果沒同步成功的話


=================================================================================================================

總結:

##############################  錯誤處理 ① ##########################

對於       Slave_IO_Running: No

              Slave_SQL_Running: Yes

錯誤解答

Master slave 複製錯誤

Description:

Slave_IO_Running:NO

Slave_SQL_Running:Yes

Seconds_Behind_Master: NULL

本人遇到的Slave_IO_Running:NO的情況有下面兩種:

1. 在配置slave同步時因為slave訪問master沒有許可權導致;

2. master上的mysql-bin.xxxxxx檔案全被我誤刪除了;

對於第一種情況,仔細檢查資料庫存取權限即可解決;

對於第二種情況:

mysql> show slave status\G

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

            Slave_IO_State:

               Master_Host: 192.168.0.123

               Master_User: slave

               Master_Port: 3306

             Connect_Retry: 60

           Master_Log_File: mysql-bin.000016

       Read_Master_Log_Pos: 173

            Relay_Log_File: mysqld-relay-bin.000008

             Relay_Log_Pos: 98

     Relay_Master_Log_File: mysql-bin.000016

          Slave_IO_Running: No

         Slave_SQL_Running: Yes

           Replicate_Do_DB:

       Replicate_Ignore_DB:

        Replicate_Do_Table:

    Replicate_Ignore_Table: br>          

                 Last_Error: 0

              Skip_Counter: 0

       Exec_Master_Log_Pos: 173

           Relay_Log_Space: 98

           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

1 row in set (0.00 sec)



解決步驟:

重啟master庫:service mysqld restart

mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000001 |       98 |              |                                         |

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

mysql> slave stop;

mysql> change master to Master_Log_File='mysql-bin.000001',Master_Log_Pos=98;

mysql> slave start;

mysql> show slave status\G

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

            Slave_IO_State: Waiting for master to send event

               Master_Host: 192.168.0.123

               Master_User: slave

               Master_Port: 3306

             Connect_Retry: 60

           Master_Log_File: mysql-bin.000001

       Read_Master_Log_Pos: 98

            Relay_Log_File: mysqld-relay-bin.000002

             Relay_Log_Pos: 235

     Relay_Master_Log_File: mysql-bin.000001

          Slave_IO_Running: Yes

         Slave_SQL_Running: Yes

           Replicate_Do_DB:

   Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

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

           Relay_Log_Space: 235

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

1 row in set (0.00 sec)

##############################  錯誤處理 ② ##########################

問題:如果你的mysql資料庫已經有資料存放區使用,將導致master與slave日誌錯誤!

mysql> start slave;

ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log

mysql> show slave status;

Empty set (0.00 sec)

處理:刪除mysql-bin.000001 記錄檔,並重啟mysql服務

1、master

#rm -rf /var/lib/mysql/*.*        

2、slave

#rm -rf /usr/local/mysql/var/*.*    

3、service mysqld restart




本文出自 “history_xcy” 部落格,請務必保留此出處http://historys.blog.51cto.com/7903899/1295239

相關文章

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.