1. 程式人生 > >MySQL配置主主複製和高可用

MySQL配置主主複製和高可用

MySQL主從複製設定步驟:

作業系統:   CentOS release 6.6 (Final) 64位
主機:     192.168.226.51
備機:     192.168.226.52
資料庫版本:  5.1.73
  • 1、主備機各自安裝MySQL資料庫
yum install mysql-server
  • 2、主機上啟用二進位制日誌:
vi /etc/my.cnf
>log-bin=mysql-bin
  • 3、重啟MySQL資料庫,檢查日誌名稱和位置資訊,並建立MySQL使用者,用於從備機訪問日誌
    service mysqld restart
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000005 |      350 |              |                          |
+------------------+----------+--------------+--------------------------+
1 row in set (0.01 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysql52'@'192.168.226.52' IDENTIFIED BY 'mysql52'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
  • 4、告知備機日誌名稱和位置資訊
CHANGE MASTER TO
MASTER_HOST='192.168.226.51',
MASTER_USER='mysql52'
, MASTER_PASSWORD='mysql52', MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=350;

5、啟動slave並檢視狀態

mysql> slave start;
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.226.51
                  Master_User: mysql52
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 514
               Relay_Log_File: mysqld-relay-bin.000009
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 514
              Relay_Log_Space: 960
              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)

mysql> 

說明:

  • Slave_IO_Running和Slave_SQL_Running狀態必須為Yes,如果為No,Last_IO_Error會顯示錯誤資訊,一般都是防火牆問題,關閉iptables和selinux;
  • 啟動時如果提示server id重複了,請修改my.cnf檔案,主備機的id不能重複
  • 6、測試主從複製
    在主機上test資料庫下建立表並插入資料,備機上可以檢視到新建的表和內容

配置主主複製

修改主機my.cnf

auto_increment_increment=2   #步進值auto_imcrement。一般有n臺主MySQL就填n
auto_increment_offset=1   #起始值。一般填第n臺主MySQL。此時為第一臺主MySQL
binlog-ignore=mysql   #忽略mysql庫
binlog-ignore=information_schema   #忽略information_schema庫
replicate-do-db=test   #要同步的資料庫,預設所有庫

修改備機my.cnf

auto_increment_increment=2
auto_increment_offset=2
replicate-do-db=test

配置好後重啟MySQL
在備機上為主機建立複製所用的賬號

mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysql51'@'192.168.226.51' IDENTIFIED BY 'mysql51';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

檢視備機二進位制日誌檔案和讀取位置:
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000005 |      106 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

mysql> 

在主機上備機二進位制檔案日誌:

CHANGE MASTER TO
MASTER_HOST='192.168.226.52',
MASTER_USER='mysql51',
MASTER_PASSWORD='mysql51',
MASTER_LOG_FILE='mysql-bin.000005',
MASTER_LOG_POS=106;

啟動slave並檢視狀態:

mysql> slave start;
Query OK, 0 rows affected (0.00 sec)

mysql>  show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000005 |      514 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.226.52
                  Master_User: mysql52
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 106
               Relay_Log_File: mysqld-relay-bin.000008
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          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: 106
              Relay_Log_Space: 552
              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.01 sec)

mysql> 

配置keepalived實現高可用

1、主備機安裝keepalived

yum install keepalived –y

2、配置keepalived
主機配置vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id MYSQL_HA           #標識,主備相同
}

vrrp_instance VI_1 {
    state BACKUP                #兩臺機都設定為BACKUP
    interface eth1  
    virtual_router_id 51        #主備相同
    priority 100                #節點優先順序(1-254之間),備機設定為90
    advert_int 1
    nopreempt                   #禁止搶佔服務。
                                #預設情況,當MASTER服務宕掉之後,BACKUP自動升級為MASTER並接替它的任務;當MASTER服務恢復後,升級為MASTER的BACKUP服務又自動降為BACKUP,把工作權交給原MASTER。
                                #當配置了nopreempt,MASTER從掛掉到恢復,不再將服務搶佔過來。 
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.226.53
    }
}

virtual_server 192.168.226.53 3306 {
    delay_loop 2
    #lb_algo wrr                #LVS演算法
    #lb_kind DR                 #LVS模式
    persistence_timeout 50  #同一IP的連線60秒內被分配到同一臺真實伺服器
    protocol TCP
    real_server 192.168.226.51 3306 {           #檢測本地mysql,備機填寫備機的IP
    weight 3
    notify_down /usr/local/keepalived/mysql.sh  #當mysq服down時,執行此指令碼,殺死keepalived實現切換
    TCP_CHECK {
        connect_timeout 3       #連線超時
        nb_get_retry 3          #重試次數
        delay_before_retry 3    #重試間隔時間
    }
}

備機配置vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id MYSQL_HA
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90                 #優先順序設定為90
    advert_int 1
    #nopreempt                  #備機不設定
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.226.53
    }
}

virtual_server 192.168.226.53 3306 {
    delay_loop 2
    #lb_algo wrr
    #lb_kind DR
    persistence_timeout 50 
    protocol TCP
    real_server 192.168.226.52 3306 {   #填寫本機真實IP
    weight 3
    notify_down /usr/local/keepalived/mysql.sh   
    TCP_CHECK {
    connect_timeout 3    
    nb_get_retry 3       
    delay_before_retry 3 
  }
}

在主機上建立切換指令碼

mkdir /etc/keepalived/
vi /usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
chmod +x /usr/local/keepalived/mysql.sh

重啟keepalived服務

service keepalived start

在主備機上建立使用者用於遠端訪問測試:
grant all on . to’root’@’%’ identified by ‘root’;
flush privileges;

mysql> grant all on *.* to'root'@'%' identified by 'root';
flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

遠端連線到虛擬IP地址:

jdbc:mysql://192.168.226.53:3306/test?useUnicode=true&characterEncoding=utf8

停止主機上的mysqld服務,發現連線依然可用