Keepalived架構高可用Mysql(一)

 1試驗網絡拓撲圖

環境描述

MASTER

eth0:10.0.0.18   Mysql

VIP

10.0.0.20

BACKUP

eth0:10.0.0.19   Mysql

 

2Mysql安裝

   架設高可用Mysql數據庫,目前解決方案很多,主要架構設計在Mysql上。可能通過共享存儲,主從備份,主主備份等手段來實現數據的一致性。爲了簡單化方便,這裏使用主主同步備份的方式來實現高可用性Mysql集羣。

   MySQL數據還是放在本地較爲安全,存儲設備畢竟存在單點隱患。使用MySQLmaster+keepalived是一種非常好的解決方案,在MySQL-HA環境中,MySQL互爲主從關係,這樣就保證了兩臺MySQL數據的一致性,然後用keepalived實現虛擬IP,通過keepalived自帶的服務監控功能來實現MySQL故障時自動切換。

注:爲了區分兩臺服務器主機名分別設爲MasterBackup(不是必要的)

[root@Master ~]# yum install mysql-server mysql

[root@Backup ~]# yum install mysql-server mysql

 

3、配置主主同步備份Mysql

MySQL master-master配置方法

A:修改MySQL配置文件

兩臺MySQL均如要開啓binlog日誌功能,開啓方法:在MySQL配置文件[MySQLd]段中加上log-bin=MySQL-bin選項

兩臺MySQLserver-ID不能一樣,默認情況下兩臺MySQLserverID都是1,需將其中一臺修改爲2即可

B:互將本機設爲另一臺服務的主服務器

 

1)  授權用戶

創建同步用戶,在主服務器上爲從服務器建立一個連接帳戶,該帳戶必須授予REPLICAITON SLAVE權限。

這裏服務器Master主機和服務器Backup互爲主從,所以都要分別建立一個同步用戶。

[root@Master ~]# service mysqld start

[root@Master ~]# mysql

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

Your MySQL connection id is 2

Server version: 5.0.77 Source distribution

 

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

 

mysql> grant replication slave,file on *.* to 'repl1'@'10.0.0.19' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

 

[root@Backup ~]# service mysqld start

[root@Backup ~]# mysql

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

Your MySQL connection id is 2

Server version: 5.0.77 Source distribution

 

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

 

mysql> grant replication slave,file on *.* to 'repl2'@'10.0.0.18' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

 

[root@Master ~]# service mysqld stop

Stopping MySQL:                                            [  OK  ]

[root@Backup ~]# service mysqld stop

Stopping MySQL:                                            [  OK  ]

 

2)  配置Mysql主主同步配置文件

[root@Master ~]# vi /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

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

user=mysql

##################加入以下內容

log-bin=mysql-bin                 啓動二進制日誌系統

server-id=1                           本機數據庫ID 標示爲主,另一配置爲2

binlog-do-db=test                 二進制需要同步的數據庫名

binlog-ignore-db=mysql        避免同步mysql用戶配置,以免不必要的麻煩

replicate-do-db=test              同步數據庫名稱

replicate-ignore-db=mysql     屏蔽對mysql庫的同步

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1       另一配置爲2

3)  分別重啓兩服務器的Mysql服務

[root@Master ~]# service mysqld start

[root@Backup ~]# service mysqld start

Starting MySQL:                                            [  OK  ]

 

4)  分別在服務器上查看做爲主服務器狀態

注:這裏鎖表的目的是爲了生產環境中不讓進新的數據,好讓從服務器定位同步位置。初次同步完成後,記得解鎖。

[root@Master ~]# mysql

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

Your MySQL connection id is 2

Server version: 5.0.77-log Source distribution

 

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

 

mysql> flush tables with read lock\G

Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G

*************************** 1. row ***************************

            File: mysql-bin.000001

        Position: 98

    Binlog_Do_DB: test

Binlog_Ignore_DB: mysql

1 row in set (0.01 sec)

 

[root@Backup ~]# mysql

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

Your MySQL connection id is 2

Server version: 5.0.77-log Source distribution

 

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

 

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G

*************************** 1. row ***************************

            File: mysql-bin.000001

        Position: 98

    Binlog_Do_DB: test

Binlog_Ignore_DB: mysql

1 row in set (0.03 sec)

5  分別在兩服務器上用change master語句指定同步位置

AMaster服務器上執行

mysql> change master to

    -> master_host='10.0.0.19',

    -> master_user='repl2',

    -> master_password='123456',

    -> master_log_file='mysql-bin.000001',

    -> master_log_pos=98;

Query OK, 0 rows affected (0.03 sec)

一行寫法

mysql> change master to master_host='10.0.0.19', master_user='repl2', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=98;

B:啓動從服務器線程

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

 

CBackup服務器上執行

mysql> change master to master_host='10.0.0.18', master_user='repl1', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=98;

D:啓動從服務器線程

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

6)  查看各自主機看IO進程和slave進程

mysql> show processlist\G

*************************** 1. row ***************************

     Id: 3

   User: root

   Host: localhost

     db: test

Command: Query

   Time: 0

  State: NULL

   Info: show processlist

*************************** 2. row ***************************

     Id: 4

   User: system user

   Host:

     db: NULL

Command: Connect

   Time: 62

  State: Waiting for master to send event

   Info: NULL

*************************** 3. row ***************************

     Id: 5

   User: system user

   Host:

     db: NULL

Command: Connect

   Time: 53

  State: Has read all relay log; waiting for the slave I/O thread to update it

   Info: NULL

*************************** 4. row ***************************

     Id: 6

   User: repl1

   Host: Backup:54282

     db: NULL

Command: Binlog Dump

   Time: 53

  State: Has sent all binlog to slave; waiting for binlog to be updated

   Info: NULL

4 rows in set (0.00 sec)

釋放掉各自的鎖

mysql> unlock tables;

 

7)  分別在兩服務器上查看從服務器狀態

mysql>show slave status\G;

 

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

查看以上兩項的值,均爲Yes則表示狀態正常

8)  測試主主同步

A:測試服務器Master,在服務器Master中新建數據

mysql> use test

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)

9)兩臺MySQL服務器都要授權允許從遠程登錄

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

 

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章