CentOS 6.3下MySQL主主互備架構配置

來源:互聯網
上載者:User

MySQL主主互備結構是基於mysql增量日誌基礎上的,區別於主從複製結構.

在主主複製結構中,兩台伺服器的任何一台上面的資料庫存發生了改變都會同步到另一台伺服器上,這個改變是基於sql語句的改變,如果刪除系統資料庫源檔案或刪除後新建立同名MYSQL表實現同步則無效。這樣兩台伺服器互為主從,並且都能向外提供服務,這就比使用主從複製具有更好的效能.

接下來我將使用兩個相同的環境來實現這個效果:

系統內容:CentOS6.3

資料庫: mysql-5.6.10

安裝配置mysql(略)

1.主1 mysql配置:(192.168.7.201)

刪除之前遺留bin日誌

# rm -rf /usr/local/mysql/log/bin*

修改mysql設定檔:

# vi /etc/my.cnf

添加:

-----------------

# Replication Master Server 1

# bin日誌路徑

log-bin = /usr/local/mysql/log/bin.log

# 伺服器ID號

server-id = 1

# 忽略mysql資料庫複寫

binlog-ignore-db=mysql

# 每次增長2

auto-increment-increment = 2

# 設定自動成長的欄位的位移量,即初始值為2

auto-increment-offset = 1

------------

# mysql -u root -p123456

在主1上為主2添加同步帳號

> grant replication slave on *.* to 'slave'@'192.168.7.249' identified by '123456';

將主1的資料庫表全部備份,並發送到主2伺服器上。

# /usr/local/mysql/bin/mysqldump -u root -p123456 --opt --skip-lock-tables  --flush-logs  --all-database > /root/allbak.sql

# cd ~

# scp allbak.sql root@192.168.7.249:/root

重啟服務

# service mysqld restart

# mysql -u root -p123456;

配置串連到主2伺服器(注意這步一定要在主2添加賬戶後執行)

> stop slave;

> change master to master_host='192.168.7.249',master_user='slave',master_password='123456';

> start slave;

2.主2 mysql配置(192.168.7.249)

刪除之前遺留bin日誌

# rm -rf /usr/local/mysql/log/bin*

修改mysql設定檔:

# vi /etc/my.cnf

添加:

--------------------

# Replication Master Server 2

# bin日誌路徑

log-bin = /usr/local/mysql/log/bin.log

server-id=2

# 忽略mysql資料庫複寫

replicate-ignore-db=mysql

# 每次增長2

auto-increment-increment = 2

# 設定自動成長的欄位的位移量,即初始值為2

auto-increment-offset = 2

---------------------

# mysql -u root -p123456

在主2上為主1添加同步帳號

> grant replication slave on *.* to 'slave'@'192.168.7.201' identified by '123456';

恢複主1的資料庫到主2

#/usr/local/mysql/bin/mysql -u root -p123456 < /root/allbak.sql

重啟服務

# service mysqld restart

# mysql -u root -p123456;

配置串連到主1伺服器

> stop slave;

> change master to master_host='192.168.7.201',master_user='slave',master_password='123456';

> start slave;

最後分別登陸兩台伺服器mysql後台查看主主備份串連狀態

# mysql -u root -p123456;

> show slave status\G;

搜尋這三行,如下則主主互備配置成功

Slave_IO_State: Waiting for master to send event

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

當主1的資料庫增加,刪除,改變資料時,主2也會同步更新。

當主2的資料庫增加,刪除,改變資料時,主1也會同步更新。

這樣就可以配合keepalived實現資料庫雙機熱備+資料同步互備,大大提高了MYSQL的可靠性和安全性。

Keepalived+MySQL互備

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.