閱讀845 返回首頁    go 技術社區[雲棲]


Mysql replication 配置

MySQL 設定寫入 Master 後, 自動 Replication 到 Slave 去, 運作基本原理是:

  1. INSERT/UPDATE/DELETE 語法, 自動寫入 Master 的 binlog file.
  2. 由 GRANT REPLICATION 授權的賬號, 自動將 SQL 語法 repl 到 Slave 的 DB 執行.
  3. 因而完成 Replication 的動作.
========================================================================

操作係統:Linux

MasterIP192.168.2.122

SlaveIP192.168.2.123

注意:在做配置的過程中,最好不要向 Master 數據庫做寫入操作。

1        主機master設置

1.1       測試用數據庫

1.       建立名為test1的數據庫

create database test1;

2.       建立表名為test1的表

CREATE TABLE `test1` ( `id` bigint(20) unsignedNOT NULL AUTO_INCREMENT,   `name`  varchar(100) ,   PRIMARY KEY(`id`)  );

3.       在表test1中插入3條測試數據

INSERT INTO `test1` VALUES (1,'name1');

INSERT INTO `test1` VALUES (2,'name2');

INSERT INTO `test1` VALUES (3,'name3');

1.2       設置Mysql Replication

1.       增加一用於數據庫同步的用戶root

mysql>GRANT REPLICATIONSLAVE,REPLICATION CLIENT,RELOAD,SUPER ON *.* TO'root'@'192.168.2.123' IDENTIFIED BY 'password';

(授與從192.168.2.123主機上登錄用戶root數據複製權限)

2.       使權限生效,這步可不做,因為過會要重啟mysql

mysql>flush privileges;

3.       退出mysql命令行界麵

mysql> exit

4.       停止mysql服務

service mysqld stop

5.       更改Mysql配置文件/etc/my.cnf

1)       打開mysql配置文件

vi /etc/my.cnf

2)       在配置文件中添加以下內容

   #bind-address           = 127.0.0.1

   server-id               = 1    (注意不能與其他服務器的配置一樣)
log_bin                 = /var/log/mysql/mysql-bin.log

# 若是 innodb, 且有用 transaction 的, 需再加入下麵
innodb_flush_log_at_trx_commit=1
sync_binlog=1

#如果指定具體的數據庫,需加入下麵一行

binlog-do-db= test1

3)      記錄 master status

mysql>show master status; #這邊資料都要記好,等一下設定slave要用

+----------------------+------------+------------------+----------------------+
| File                     | Position   | Binlog_Do_DB | Binlog_Ignore_DB  |
+----------------------+------------+------------------+----------------------+
| mysql-bin.000014  |      232   |                      |                          |
+----------------------+------------+------------------+----------------------+


6.       將要進行熱備的數據庫test1打包

tar czvf /var/lib/mysql/test1.tar.gztest1

7.       啟動Mysql

service mysqld start

2        備機slave設置

1.       更改Mysql配置文件/etc/my.cnf

3)       打開Mysql的配置文件

vi /etc/my.cnf

4)       在配置文件中添加以下內容

master-host = 192.168.2.122

master-user = root

master-password = password

master-port = 3306

master-connect-retry = 10

master_log_file='mysql-bin.000014' # 這邊就要用到之前 Master 抄下來的值.

master_log_pos=232   # 這邊就要用到之前 Master 抄下來的值.

replicate-do-db = test1  #當需要具體到某個數據庫的時候,才配置該行

2.       將先前(1.2中第6步)從master打包的數據庫文件test1.ter.gz發送到備機slave的相應目錄下,本次在/var/lib/mysql目錄,解壓文件並修改其可執行權限

cd /var/lib/mysql

tar zxvf  test1.tar.gz

chmod 700 test1

chmod 660 test1

3.       重新啟動Mysql服務

service mysqld  restart

4.       啟動slave

mysql> START SLAVE;

查看slave狀態:

mysql> show slavestatus\G;

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

            Slave_IO_State:Waiting for master to send event

               Master_Host: 192.168.2.122

               Master_User: root

               Master_Port: 3306

             Connect_Retry: 10

           Master_Log_File: mysql-bin.000002

       Read_Master_Log_Pos: 582

            Relay_Log_File: mysqld-relay-bin.000005

             Relay_Log_Pos: 515

     Relay_Master_Log_File: mysql-bin.000002

          Slave_IO_Running: Yes

         Slave_SQL_Running: Yes

           Replicate_Do_DB: test1,test1

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

           Relay_Log_Space: 515

           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)

******************************************************

可以看到

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

如果都是yes,代表已經在同步

至此server服務器上的Mysql設置完畢

3        測試

1.       主機和備機都正常運行情況,對主機進行增加一條記錄INSERT INTO `test1` VALUES(6,'name6'); 檢測備機是否也增加了此條記錄,經檢查,數據相同

2.       主機和備機都正常運行情況,在主機沒有對數據庫操作的情況下測試主備數據是否一致,經查看主機和備機的的test1表,記錄相同,說明備機成功複製數據。

3.       主機和備機都正常運行情況,停掉備機mysql,對主機進行添加記錄的操作,然後再重啟備機mysql,經檢查,兩邊數據完全一致。

4.       主機和備機都正常運行情況,在主機執行mysql> delete fromtest1 where id=3; 檢測兩邊數據是否一致。經檢查,兩邊數據完全一致。


最後更新:2017-04-04 07:03:27

  上一篇:go discuz-ucenter-api-for-java 中文亂碼問題
  下一篇:go 業界大佬們對於2013年大數據的預測