天天看點

mysql主主同步

實驗環境:兩台centos6.5

mysql1 ip:10.1.1.20

mysql2  ip:10.1.1.21

mysql vip:10.1.1.25

一、安裝mysql,并打造主主同步

主主同步就是兩台機器互為主的關系,在任何一台機器上寫入都會同步。

安裝mysql的過程不解釋,yum就好啦

配置主主同步

1.配置 /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

log-bin=binlog  #開啟binlog功能

log-bin-index=binlog.index

sync_binlog=0

server_id = 1    #兩台機器不能重複,一個1 一個2 就好

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

2.分别在兩台機器上配置同步賬号

10.1.1.20機器上:

[root@localhost ~]# mysql

Welcome to the MySQL monitor. Commands end with; or \g.

Your MySQL connection id is 2

Server version: 5.0.77-log Sourcedistribution

Type 'help;' or '\h' for help. Type '\c' toclear the buffer.

mysql> GRANT replication slave ON *.* TO'ab'@'%' identified by '123';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

10.1.1.21機器上:

3.設定同步

10.1.1.20機器上:

mysql> flush tables with read lock;

mysql> show master status;

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

| File | Position | Binlog_Do_DB |Binlog_Ignore_DB |

| binlog.000003 | 365 | | |

1 row in set (0.03 sec)

mysql> unlock tables;

Query OK, 0 rows affected (0.03 sec)

mysql> change master to master_host='10.1.1.20', master_port=3306, master_user='ab',master_password='123', master_log_file='binlog.000003',master_log_pos=365;

Query OK, 0 rows affected (0.06 sec)

mysql> start slave;

mysql> show slave status \G  #執行這指令後 注意觀察下面這兩個參數,必須要都是yes才行

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

同樣的 反過來做相同操作

10.1.1.21機器上:

| binlog.000004 | 207 | | |

mysql> change master tomaster_host='10.1.1.21', master_port=3306, master_user='ab',master_password='123', master_log_file='binlog.000004',master_log_pos=207;

介此,主主同步打造完成,可以簡單測試一下,分别在兩個機器上寫資料 看看會不會同步到另一台機器上

PS:如果報錯  Slave_IO_Running: NO  可以檢查同步的賬号是否建立正常!

二、安裝keepalived 并設定監控

keepalived是安裝在兩台MySQL伺服器上的

首先安裝keepalived 過程不解釋就正常解壓安裝就好

安裝後配置 vim /etc/keepalived/keepalived.conf 内容如下

10.1.1.20的配置檔案

! Configuration File for keepalived

global_defs {

  notification_email {

    [email protected]

    [email protected]

    [email protected]

  }

  notification_email_from [email protected]

  smtp_server 127.0.0.1

  smtp_connect_timeout 30

  router_id LVS_DEVEL

}

vrrp_instance VI_1 {

  state backup      #兩台配置此處均是BACKUP

  interface eth0

  virtual_router_id 51

  priority 100      #優先級,另一台改為90 

  advert_int 1

  nopreempt          #不搶占,隻在優先級高的機器上設定即可,優先級低的機器不設定

  authentication {

      auth_type PASS

      auth_pass 1111

  virtual_ipaddress {

      10.1.1.25

virtual_server 10.1.1.25 3306 {

  delay_loop 6

  lb_algo wrr

  lb_kind DR

  persistence_timeout 50        #會話保持時間 

  protocol TCP

real_server 10.1.1.20 3306 {

      weight 3

      notify_down /tmp/nimei.sh    #檢測到mysql服務挂了就執行這個腳本(腳本要自己寫哈)

      TCP_CHECK {

      connect_timeout 10        #連接配接逾時時間

      nb_get_retry 3            #重連次數 

      delay_before_retry 3      #重連間隔時間

      connect_port 3306        #健康檢查端口 

        }

      }

10.1.1.21 的配置檔案

  state backup

  priority 90

  persistence_timeout 50

real_server 10.1.1.21 3306 {

      notify_down /tmp/nimei.sh

      connect_timeout 10

      nb_get_retry 3

      delay_before_retry 3

      connect_port 3306

          }

編寫監控mysql服務是否挂了的腳本,按照上面配置檔案的位置編寫腳本。

vim /tmp/nimei.sh

#!/bin/sh 

pkill keepalived

腳本很簡單啊 就一句,目的是當keepalived檢測到mysql服務挂了之後觸發這個腳本,殺死keepalived程序,讓另一台機器接管

好 修改後啟動keeplived服務

介此整個叢集搭建完成

三、測試

找一台機器用虛拟ip連接配接mysql

[root@localhost html]# mysql -uab  -h 10.1.1.25 -p123

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 736

Server version: 5.1.66-log Source distribution

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

這樣成功連上了,然後你可以任意關閉某台機器,或者某台機器的mysql服務,看看還能不能連上!!

本文轉自 linuxpp 51CTO部落格,原文連結:http://blog.51cto.com/1439337369/1758790,如需轉載請自行聯系原作者

繼續閱讀