Replication 目的是備份DB的資料 當主DB死掉 資料不致失去太多 也可以立刻以SLAVE DB 代替

先準備兩台DB

先開放兩邊的port 3306

 /sbin/iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT

 

開始設定

1.停止mysql服務

Master#>services mysqld stop 

 

2.修改/etc下的my.cnf

加入這兩行

server-id = 1

log-bin    = mysql-bin

將server-id設為1

打開log-bin

其中server-id必須各自不同, 用於Replication關係中識別各Server. 

 

3.重新啟動mysql

Master#>services mysqld start

進入Mysql

Master#>mysql -u root  -p

設定Replication 的帳號 repl

mysql>GRANT REPLICATION SLAVE ON *.* TO repl@Slave的IP IDENTIFIED BY '密碼'; 

mysql>FLUSH PRIVILEGES;

將DB TABLE 資料先停住 不讓其寫入

mysql>FLUSH TABLES WITH READ LOCK;

show 出Position  設定Slave要用的參數 

mysql> SHOW MASTER STATUS;

  1. +-----------------------+-----------+-------------------+-----------------------+
  2. | File                        | Position | Binlog_Do_DB  | Binlog_Ignore_DB |
  3. +-----------------------+-----------+-------------------+-----------------------+
  4. | mysql-bin.000001 |      759   |                         |                              |
  5. +-----------------------+-----------+-------------------+-----------------------+
 
4.設定Slave

修改/etc下的my.cnf

加入這兩行

server-id = 2

log-bin    = mysql-bin

5.進入Mysql設定

Slave#>services mysqld start

Slaver#>mysql -u root -p

mysql>CHANGE MASTER TO   MASTER_HOST=Master-IP,  MASTER_USER='repl',  MASTER_PASSWORD='repl_password', MASTER_LOG_FILE='mysql-bin.000001',  MASTER_LOG_POS='759;

MASTER_HOST=Master的IP

MASTER_USER=設定的replication的帳號

MASTER_PASSWORD=replication的密碼

啟動Slave

mysql> Start Slave; 

mysql> SHOW SLAVE STATUS\G 

    1. mysql> SHOW SLAVE STATUS\G
    2. *************************** 1. row ***************************
    3.              Slave_IO_State: Waiting for master to send event
    4.                 Master_Host: xxx.xxx.xxx.xxx
    5.                 Master_User: repl
    6.                 Master_Port: 3306
    7.               Connect_Retry: 60
    8.             Master_Log_File: mysql-bin.000001
    9.         Read_Master_Log_Pos: 759
    10.              Relay_Log_File: mysqld-relay-bin.000042
    11.               Relay_Log_Pos: 235
    12.       Relay_Master_Log_File: mysql-bin.000001
    13.            Slave_IO_Running: Yes
    14.           Slave_SQL_Running: Yes
    15.             Replicate_Do_DB:
    16.         Replicate_Ignore_DB:
    17.          Replicate_Do_Table:
    18.      Replicate_Ignore_Table:
    19.     Replicate_Wild_Do_Table:
    20. Replicate_Wild_Ignore_Table:
    21.                  Last_Errno: 0
    22.                  Last_Error:
    23.                Skip_Counter: 0
    24.         Exec_Master_Log_Pos: 759
    25.             Relay_Log_Space: 235
    26.             Until_Condition: None
    27.              Until_Log_File:
    28.               Until_Log_Pos: 0
    29.          Master_SSL_Allowed: No
    30.          Master_SSL_CA_File:
    31.          Master_SSL_CA_Path:
    32.             Master_SSL_Cert:
    33.           Master_SSL_Cipher:
    34.              Master_SSL_Key:
    35.       Seconds_Behind_Master: 0
    36. 1 row in set (0.00 sec)

      Slave_IO_Running: Yes 
      Slave_SQL_Running: Yes 

6.回到Master上

重新打開剛剛鎖住的DB

mysql>UNLOCK TABLES;

Slave_IO_Running = No 或Connecting

代表replication未成功

原因有檢查 1.網路連線沒開通

                 2.pos設定沒同步

                 3.repl帳號密碼錯誤

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 foanwang 的頭像
    foanwang

    Foan的程式&城市札記

    foanwang 發表在 痞客邦 留言(0) 人氣()