Keepalived 架構高可用 Mysql

czxin788發表於2015-01-21

Keepalived 架構高可用 Mysql

 

以下是我實驗的步驟,僅供參考。

 

1、實驗網路拓撲圖

Keepalived 架構高可用 Mysql

2keepalived的安裝

10.72.16.22610.72.16.227分別安裝keepalived軟體。

[root@localhost ~]# tar xf keepalived-1.2.1.tar.gz

[root@localhost ~]# cd keepalived-1.2.1

[root@localhost keepalived-1.2.1]# ./configure --prefix=/ --mandir=/usr/local/share/man/         --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5xen-i686/

--選擇和自己一樣的kerneltab鍵補齊。

[root@localhost keepalived-1.2.1]# make

[root@localhost keepalived-1.2.1]# make install

主備機器都如上安裝。

3keepalived的配置

3.110.72.16.226master)上面配置

[root@localhost keepalived-1.2.1]# cd /etc/keepalived/

[root@localhost keepalived]# cp keepalived.conf keepalived.conf.default

[root@localhost keepalived]# vim keepalived.conf

主備mysql中只是黃色字型不一樣,其他都一樣

global_defs {

router_id Hadb_1

}

vrrp_sync_group VGM {

group {

VI_HA

}

}

vrrp_script chk_mysql {

script "killall -0 mysqld"

#script "/tcp/127.0.0.1/3306"

interval 1

}

# vvrp_instance define #

vrrp_instance VI_HA {

state MASTER

interface eth0

virtual_router_id 54

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass hamysql

}

virtual_ipaddress {

10.72.16.230

}

track_script {

chk_mysql

}

}

 

[root@localhost keepalived]#scp keepalived.conf 10.72.16.227:/etc/keepalived/

3.210.72.16.227(backup)上面的配置

[root@localhost keepalived-1.2.1]# cd /etc/keepalived/

[root@localhost keepalived]# cp keepalived.conf keepalived.conf.default

[root@localhost keepalived]# vim keepalived.conf

主備的keepalived.conf中只是黃色字型不一樣,其他都一樣。

所以只需將state改成BACKUPpriority改成50即可

 

global_defs {

router_id Hadb_1

}

vrrp_sync_group VGM {

group {

VI_HA

}

}

vrrp_script chk_mysql {

script "killall -0 mysqld"

#script "/tcp/127.0.0.1/3306"

interval 1

}

# vvrp_instance define #

vrrp_instance VI_HA {

state BACKUP

interface eth0

virtual_router_id 54

priority 50

advert_int 1

authentication {

auth_type PASS

auth_pass hamysql

}

virtual_ipaddress {

10.72.16.230

}

track_script {

chk_mysql

}

}

 

3.3啟動 Keepalived 服務

分別啟動兩臺伺服器(10.72.16.22610.72.16227)的 Keepalived 服務並觀察其日誌,校驗高可用是否成功。

[localhost@localhost ~]# service keepalived start

[localhost @localhost ~]# tail /var/log/messages

3.4 測試keepalived

1)在一臺裝有 mysql 客戶端的 windows linux 機器登陸 VIP 10.72.16.230 測試。

# mysql -uroot –p123 -h10.72.16.230

2keepalived 故障轉移測試

 

A:在 windows linux 客戶端一直去 ping VIP 10.72.16.230,然後關閉 10.72.16.226 上的 keepalived,正常情況下 VIP 10.72.16.230就會切換到 10.72.16.227 上面去。

注:透過命令ip a來檢視vip 10.72.16.230ifconfig看不到。

 

B:開啟 10.72.16.226 上的 keepalived,看vip是否能自動切換回來。

3)MySQL 故障轉移測試

A:在 10.72.16.226 上關閉 MySQL 服務,看 VIP 是否會切換到 10.72.16.227

[localhost@localhost ~]# service mysqld stop

[localhost @localhost ~]# tail /var/log/messages -f

[root@localhost keepalived]# ip a

2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000

    link/ether 00:0c:29:76:fe:3b brd ff:ff:ff:ff:ff:ff

    inet 10.72.16.226/24 brd 10.72.16.255 scope global eth0

    inet 10.72.16.230/32 scope global eth0

    inet6 fe80::20c:29ff:fe76:fe3b/64 scope link

       valid_lft forever preferred_lft forever

4 MySQL主主複製

4.1 授權使用者

10.72.16.226

grant replication slave,file on *.* to 'repl1'@'10.72.16.227' identified by '123';

 

10.72.16.227

grant replication slave,file on *.* to 'repl2'@'10.72.16.226' identified by '123';

 

4.2 修改配置檔案

4.2.1 10.72.16.226

vi /etc/my.cnf

新增如下標黃色部分,其中只需要修改紅色字型部分。

[mysqld]

datadir=/var/lib/mysql

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

user=mysql

log-bin=mysql-bin

server-id=1

binlog-do-db=db1 #需要進行同步的資料庫

binlog-ignore-db=mysql

replicate-do-db=db1 #需要進行同步的資料庫

replicate-ignore-db=mysql

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

master-host=10.72.16.227

master-user=repl2

master-password=123

# Default to using old password format for compatibility with mysql 3.x

# clients (those using the mysqlclient10 compatibility package).

old_passwords=1

 

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

# to do so, uncomment this line:

# symbolic-links=0

 

[mysqld_safe]

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

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

 

4.2.2 10.72.16.227

vi /etc/my.cnf

新增如下標黃色部分,其中只需要修改紅色字型部分。

 

[mysqld]

datadir=/var/lib/mysql

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

user=mysql

log-bin=mysql-bin

server-id=2

binlog-do-db=db1 #需要同步的資料庫

binlog-ignore-db=mysql

replicate-do-db=db1 #需要同步的資料庫

replicate-ignore-db=mysql

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1

master-host=10.72.16.226

master-user=repl1

master-password=123

# Default to using old password format for compatibility with mysql 3.x

# clients (those using the mysqlclient10 compatibility package).

old_passwords=1

 

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

# to do so, uncomment this line:

# symbolic-links=0

 

[mysqld_safe]

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

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

~

4.3分別重啟兩伺服器的 Mysql 服務

service mysqld restart

4.4 分別在兩伺服器上檢視從伺服器狀態

mysql>show slave status\G;

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

檢視以上兩項的值,均為 Yes 則表示狀態正常

 

4.5  測試主主同步

A:測試伺服器 Master,在伺服器 Master 中新建資料

mysql> use db1

Database changed

mysql> show tables;

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

| Tables_in_test |

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

1 row in set (0.00 sec)

mysql> create table t2 (id int,name varchar(10));

Query OK, 0 rows affected (0.00 sec)

B:在 Backup 伺服器進行檢視

mysql> show tables;

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

| Tables_in_test |

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

| t2              |

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

2 rows in set (0.00 sec)

C:在 Backup 伺服器中插入一條記錄

mysql> insert into t2 values (001,"ganxing");

Query OK, 1 row affected (0.01 sec)

D:在 Master 伺服器中檢視

mysql> select * from t2;

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

| id    | name     |

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

|     1 | ganxing |

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

1 row in set (0.00 sec)

 

 

以上,我們搭建完成了MYSQL的主主複製和keepalived提供vip漂移的高可用模式。

 

完!





附件列表

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28916011/viewspace-1409122/,如需轉載,請註明出處,否則將追究法律責任。

相關文章