MySQL 高可用之 keepalived+Mysql 雙主雙活。

環境描述:
[root@MySQL-M ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

[root@MySQL-S ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

IP 規劃:
MySQL-M 192.168.10.10

MySQL-S 192.168.10.20

配置兩服務服務器雙主:

MySQL 安裝:

[root@MySQL-M ~]# yum install mysql-server mysql -y
[root@MySQL-M ~]# systemctl mysqld start

編輯 /etc/my.cnf 配置文件,增加以下內容。設置 server-id。

[mysqld]
server-id = 1 #backup這臺設置2。
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema #忽略寫入binlog日誌的庫。
auto-increment-increment = 2 #字段變化增量值。
auto-increment-offset = 1 #初始字段ID爲1。
slave-skip-errors = all #忽略所有複製產生的錯誤 。

查看 主服務器 log bin 日誌和pos 值位置。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 6383 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

MySQL-M 配置如下:
主服務器授權:
mysql> grant replication client,replication slave on . to repl@'172.20.2.%' identified by 'repl';
Query OK, 0 rows affected, 1 warning (0.01 sec)

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

第二臺主服務器授權:
主服務器授權:
mysql> grant replication client,replication slave on . to repl@'172.20.2.%' identified by 'repl';
Query OK, 0 rows affected, 1 warning (0.01 sec)

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

第二臺主服務器操作:
mysql> change master to
-> master_host='192.168.10.10',
-> master_user='repl',
-> master_password='repl',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=1559;
Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> show slave status;

啓動同步功能:
mysql> start slave;

##############################################################

mysql> show slave status;
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+--------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+--------------

mysql> show slave status \G
1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 172.20.2.46
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1559
Relay_Log_File: mysql-relay.000003
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

SQL_Remaining_Delay: NULL
  Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
       Master_Retry_Count: 86400
              Master_Bind: 
  Last_IO_Error_Timestamp: 
 Last_SQL_Error_Timestamp: 

主服務器操作:
mysql> change master to
-> master_host='192.168.10.20',
-> master_user='repl',
-> master_password='repl',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=1559;
Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> show slave status;

###########################################################

啓動同步功能:
mysql> start slave;

########################################################################
測試:
主服務器 10.10:
mysql> create database test;

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
| wangbo |
+--------------------+
6 rows in set (0.00 sec)

mysql> use test;

mysql> create table t1 ( id int, age int );

mysql> insert into t1 values (1,10),(2,20),(3,30);

mysql> select * from t1;
+------+------+
| id | age |
+------+------+
| 1 | 20 |
| 2 | 30 |
| 3 | 30 |
+------+------+
6 rows in set (0.00 sec)

###########################################################
從庫插入:

mysql> use test;

mysql> insert into t1 values (4,40),(5,50),(6,60);

可以看到已經成功同步過去,同樣在backup插入到user表數據,一樣同步過去,雙主就做成功了。
###########################################################

安裝 keepalived :
[root@MySQL-M ~]#yum -y install keepalived

[root@master ~]# vi /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs {
notification_email {br/>[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MYSQL_HA #標識,雙主相同
}
vrrp_instance VI_1 {
state BACKUP #兩臺都設置BACKUP
interface eth0
virtual_router_id 51 #主備相同
priority 100 #優先級,backup設置90
advert_int 1
nopreempt #不主動搶佔資源,只在master這臺優先級高的設置,backup不設置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100
}
}
virtual_server 192.168.10.100 3306 {
delay_loop 2
#lb_algo rr #LVS算法,用不到,我們就關閉了
#lb_kind DR #LVS模式,如果不關閉,備用服務器不能通過VIP連接主MySQL
persistence_timeout 50 #同一IP的連接60秒內被分配到同一臺真實服務器
protocol TCP
real_server 192.168.10.10 3306 {本地mysql,backup也要寫檢測本地mysql
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 #重試間隔時間
}
}

[root@master ~]# vi /usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
[root@master ~]# chmod +x /usr/local/keepalived/mysql.sh
[root@master ~]# /etc/init.d/keepalived start

注:
#backup服務器只修改priority爲90、nopreempt不設置、real_server設置本地IP。

#授權兩臺Mysql服務器允許root遠程登錄,用於在其他服務器登陸測試!

mysql> grant all on . to' root'@'192.168.10.%' identified by 'oldboy123';

mysql> flush privileges;

3、測試高可用性

1、通過Mysql客戶端通過VIP連接,看是否連接成功。

2、停止master這臺mysql服務,是否能正常切換過去,可通過ip addr命令來查看VIP在哪臺服務器上。

3、可通過查看/var/log/messges日誌,看出主備切換過程

4、master服務器故障恢復後,是否主動搶佔資源,成爲活動服務器。

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