本文先配置了一個雙master環境,互為主從,然后通過Keepalive配置了一個虛擬IP,客戶端通過虛擬IP連接master1,當master1宕機,自動切換到master2。一次只能連接其中一個master進行讀寫,所以是active-passive模式。
一 Mysql主主復制搭建
1.1 實驗環境
兩台機器事先都已經裝好了mysql單實例。

二者的端口號需要保持一致,否則在最后用vip連接的時候,不能使用相同端口號連接。
1.2 實驗步驟
1.2.1 修改配置文件
修改master1:
在[mysqld]下面添加:
server-id = 1
relay-log=/data/server/mysql_3307/binlog/ZabbixServer-relay-bin
relay-log-index=/data/server/mysql_3307/binlog/ZabbixServer-relay-bin.index
auto-increment-offset = 1
auto-increment-increment = 2
log-slave-updates=true
修改master2:
在[mysqld]下面添加:
server-id = 3
relay-log =/data/server/mysql/binlog/single-relay-bin
relay-log-index=/data/server/mysql/binlog/single-relay-bin.index
auto-increment-offset = 2
auto-increment-increment = 2
log-slave-updates=true
添加auto-increment-offset那兩項,是為了避免在MySQL INSERT時主鍵沖突。
修改完后記得重啟mysql
1.2.2 建復制用戶
分別在兩台mysql上執行
GRANT REPLICATION SLAVE ON *.* TO 'RepUser'@'%'identified by 'beijing';
1.2.3 指向master
兩台服務器均為新建立,且無其它寫入操作,各服務器只需記錄當前自己二進制日志文件及事件位置,以之作為另外的服務器復制起始位置即可。否則,需要先備份主庫,在備庫進行恢復,從而保持數據一致,然后再指向master。
Master1:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 302| | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
Master2:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#Master1指向Master2
CHANGE MASTER TO MASTER_USER='RepUser',MASTER_HOST='192.168.1.21',MASTER_PASSWORD='beijing',MASTER_PORT=3307,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=120;
#Master2指向Master1
CHANGE MASTER TO MASTER_USER='RepUser',MASTER_HOST='192.168.1.22',MASTER_PASSWORD='beijing', MASTER_PORT=3307,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=302;
1.2.4 分別啟動slave
start slave ;
確保show slave status
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
測試兩邊是否同步,略。
二:配置keepalived
1 keepalive安裝
分別在master1,master2上安裝keepalive
yum install -y popt-devel
cd /usr/local/src
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/
make
make install
#假如在執行./configure --prefix=/時報錯: OpenSSL is not properly installed on your system !!!CannotincludeOpenSSLheaders files,則yum install openssl-devel -y
2 分別在master1,master2上新建檢查mysql腳本
vi /root/check_mysql.sh
內容如下
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=system@123
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1
#$mysqlclient --host=$host --port=$port --user=$user --password=$password -e "show databases;" > /dev/null 2>&1
if [ $? == 0 ]
then
echo " $host mysql login successfully "
exit 0
else
#echo " $host mysql login faild"
/etc/init.d/keepalived stop
exit 2
fi
chmod +x /root/check_mysql.sh
3 修改配置文件
vi /etc/keepalived/keepalived.conf
master1和master2配置文件內容相同。
內容:
#ConfigurationFile for keepalived
global_defs {
notification_email { ######定義接受郵件的郵箱
wangjj@hrloo.com
}
notification_email_from jiankong@staff.tuge.com ######定義發送郵件的郵箱
smtp_server mail.tuge.com
smtp_connect_timeout 10
}
vrrp_script check_mysql { ######定義監控mysql的腳本
script "/root/check_mysql.sh"
interval 2 ######監控時間間隔
weight 2 ######負載參數
}
vrrp_instance vrrptest { ######定義vrrptest實例
state BACKUP ######服務器狀態
interface eth0 ######使用的接口
virtual_router_id 51 ######虛擬路由的標志,一組lvs的虛擬路由標識必須相同,這樣才能切換
priority 150 ######服務啟動優先級,值越大,優先級越高,BACKUP 不能大於MASTER
advert_int 1 ######服務器之間的存活檢查時間
authentication {
auth_type PASS ######認證類型
auth_pass ufsoft ######認證密碼,一組lvs 服務器的認證密碼必須一致
}
track_script { ######執行監控mysql進程的腳本
check_mysql
}
virtual_ipaddress { ######虛擬IP地址
192.168.1.60
}
}
這里state不配置MASTER,且優先級一樣,是期望在MASTER1宕機后再恢復時,不主動將MASTER狀態搶過來,避免MySQL服務的波動。
由於不存在使用lvs進行負載均衡,不需要配置虛擬服務器virtual server,下同。
4 vi /etc/sysconfig/iptables
#注意,在兩台機器上都要修改。
添加:
-A INPUT -d 192.168.1.60/32 -j ACCEPT
-A INPUT -d 224.0.0.18 -j ACCEPT #添加VRRP通訊支持
注意:第一行中的192.168.1.60需要改成你自己的vip。
service iptables restart
5 啟動keepalived
在master1、master2上分別啟動:service keepalived start
分別執行ip addr命令,可以在其中一台機器上看到虛擬IP.如:
[root@slave1 keepalived]# ip addr
1: lo: mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_faststate UP qlen 1000
link/ether 08:00:27:04:05:16 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.22/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.60/32 scope global eth0
inet6 fe80::a00:27ff:fe04:516/64 scope link tentativedadfailed
valid_lft forever preferred_lft forever
說明虛擬vip連在了master1這台機器上。
如果自動只連接到了master2,關閉master2的keepalived,再啟動,自動就連接到master1了。
現在都可以ping通虛擬ip了。
6 測試
停止master1服務器keepalived,檢查VIP是否切換到master2服務器(用ip addr命令驗證即可);
三 測試高可用環境是否配置成功
3.1 建允許遠程訪問的用戶
在master1,master2創建允許遠程訪問的用戶:
grant select,update,delete,insert on *.* to 'dandan' identified by 'dandan';
3.2 訪問虛擬IP
用一台同網段的機器訪問通過vip訪問數據庫:
mysql -u dandan-pdandan -h 192.168.1.60 --port 3307
停止master1服務器的mysql,VIP切換到了master2服務器。
在master2上查看:
mysql> showprocesslist;
+----+-------------+--------------------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+--------------------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| 3 | root | localhost | dba | Query | 0 | init | show processlist |
| 14 | systemuser | | NULL |Connect | 247 | Reconnecting after afailed master event read | NULL |
| 15 | systemuser | | NULL |Connect | 207 | Slave has read all relaylog; waiting for the slave I/O thread to update it | NULL |
| 90 |dandan | 192.168.1.60:39995 |dba | Sleep | 8| | NULL |
+----+-------------+--------------------+------+---------+------+-----------------------------------------------------------------------------+------------------+
看到了dandan的連接信息。
本文介绍了如何配置MySQL双主复制,确保数据同步,并通过Keepalive设置虚拟IP,实现在主节点故障时自动切换到备用节点。详细步骤包括主从配置、用户授权、状态检查和Keepalived的安装与配置,最终实现高可用环境。
327

被折叠的 条评论
为什么被折叠?



