mysql主從同步,數據庫雙機熱備份

mysql主從同步:

主服務器:

master:192.168.1.200

從服務器:

slave:192.168.1.225



一、master端配置:

vi /etc/my.cnf

server-id       = 1    master端ID號

log-bin=/data/logbin/mysql-bin    日誌路徑及文件名

#binlog-do-db = cacti            同步cacti,此處關閉的話,就是除不允許的,其它的庫均同步。

binlog-ignore-db = mysql        不同步mysql庫,以下同上

binlog-ignore-db = test

binlog-ignore-db = information_schema



[root@localhost ~]# mysql -u root -p

mysql> grant replication slave on *.* to backdata@'192.168.1.225' identified by "backpass";

Query OK, 0 rows affected (0.03 sec)

(添加同步用的賬號backdata 密碼backpass,從服務器要用到)

mysql> flush privileges;

Query OK, 0 rows affected (0.02 sec)


mysql> flush tables with read lock;

Query OK, 0 rows affected (0.01 sec)


mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000009 |   120366 |              |                  |

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

1 row in set (0.00 sec)

(這裏要記錄下File和Position的值,在從服務器中要用到)

mysql> unlock tables;


(下面這一步是要在備份服務器上建立這個數據庫,如果備份服務器上沒有會出錯,至於錯誤下面會有介紹)

#cd /var/lib/mysql

#tar -zcvf cacti.tgz

[root@localhost mysql]# scp cacti.tgz  [email protected]:/var/lib/mysql/cacti.tgz



二、slave端配置:

[root@localhost ~]#vi /etc/my.cnf

server-id=2  #添加這一句


[root@localhost ~]# mysql -u root -p



mysql> slave stop;

Query OK, 0 rows affected, 1 warning (0.00 sec)


mysql> change master to

   -> master_host='192.168.1.200',

   -> master_user='backdata',

   -> master_password='backpass',

   -> master_log_file='mysql-bin.000009',

   -> master_log_pos=120366;

Query OK, 0 rows affected (0.25 sec)

(這裏面填的值就是在主服務器上剛纔設置 記錄的值)

mysql> start slave;

ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO

[root@localhost ~]#service mysqld restart

mysql> slave start;

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id:    4

Current database: *** NONE ***


Query OK, 0 rows affected (0.00 sec)

重啓數據庫後沒有了上面的錯誤提示


mysql> show slave status \G

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

              Slave_IO_State: Waiting for master to send event

                 Master_Host: 192.168.1.200

                 Master_User: backdata

                 Master_Port: 3306

               Connect_Retry: 60

             Master_Log_File: mysql-bin.000009

         Read_Master_Log_Pos: 155726

              Relay_Log_File: mysqld-relay-bin.000003

               Relay_Log_Pos: 251

       Relay_Master_Log_File: mysql-bin.000009

            Slave_IO_Running: Yes

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

                  Last_Error: Error 'Table 'cacti.settings' doesn't exist' on query. Default database: 'cacti'. Query: 'REPLACE


INTO settings (name,value) VALUES ('poller_lastrun',1375515901)'

                Skip_Counter: 0

         Exec_Master_Log_Pos: 120366

             Relay_Log_Space: 35767

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

              Last_SQL_Error: Error 'Table 'cacti.settings' doesn't exist' on query. Default database: 'cacti'. Query: 'REPLACE


INTO settings (name,value) VALUES ('poller_lastrun',1375515901)'

1 row in set (0.00 sec)


查看上面的Slave_IO_Running: Yes 和Slave_SQL_Running: Yes  2個都爲yes則證明主從同步正常,如果有任一個顯示NO,則證明同步有問題。

看錯誤提示是沒有cacti這個數據庫,在master複製過來數據庫,上面已有介紹

[root@localhost mysql]# scp cacti.tgz  [email protected]:/var/lib/mysql/cacti.tgz

[root@localhost mysql]#tar -zxvf cacti.tgz

[root@localhost mysql]#service mysqld restart


mysql> show slave status \G

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id:    4

Current database: *** NONE ***


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

              Slave_IO_State: Waiting for master to send event

                 Master_Host: 192.168.1.200

                 Master_User: backdata

                 Master_Port: 3306

               Connect_Retry: 60

             Master_Log_File: mysql-bin.000009

         Read_Master_Log_Pos: 169963

              Relay_Log_File: mysqld-relay-bin.000005

               Relay_Log_Pos: 251

       Relay_Master_Log_File: mysql-bin.000009

            Slave_IO_Running: Yes

           Slave_SQL_Running: Yes

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

             Relay_Log_Space: 407

             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

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)

正常了


以上是安裝過程,同步的情況將繼續測試。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章