mysql互為主從複製MM+keepalived高可用
mysql雙主MM+keepalived高可用方案
一.安裝單機mysql伺服器
1.資源分配
伺服器名 |
IP |
VIP |
MYSQL版本 |
|
Master |
10.15.107.177 |
10.15.107.188 |
5.7 |
|
Slave |
10.15.107.171 |
10.15.107.188 |
5.7 |
二.mysql互為主從複製配置
1.Master上的配置
1.1修改my.cnf
1)修改/etc/my.cnf配置檔案,主從複製必須開啟binlog和不同的server-id.
[mysqld]
server-id=1 #不同id
log-bin = mysql-bin #binlog檔案
binlog_format=mixed #binlog日誌格式
2) service mysqld restart #重啟mysql
1.2授權複製專用使用者
mysql>grant replication slave,replication client on *.* to [email protected]'10.15.%' identified by 'Mysql123!';
mysql>flush privileges;
1.3鎖主庫表
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.11 sec)
1.4記錄主庫資訊
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 1551 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.02 sec)
要記住file和position.
1.5邏輯快照
(注:如2臺DB為新安裝,沒資料同步,可略過)
按需要同步全庫或幾個DB(重複dump單個DB).
mysqldump --master-data -uroot -p -R --all-databases >master_repl.sql #全庫
mysqldump --master-data -uroot -p -R testdb >master_repl.sql #單個數據庫.
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
2.Slave上的配置
2.1 slave上的my.cnf
[mysqld]
server-id=2 #不同id
log-bin=mysql-bin #binlog檔案
binlog_format=mixed #binlog日誌格式
2.2 授權複製專用使用者
mysql>grant replication slave,replication client on *.* to [email protected]'10.15.%' identified by 'Mysql123!';
mysql>flush privileges;
2.3匯入主庫資料
mysql -uroot -p -A <master_repl.sql #針對主庫全庫匯出
mysql -uroot -p -A test_for_repl <master_repl.sql #相應資料庫名需提前建立
2.4change master 同步
mysql> change master to master_host='10.15.107.177',master_user='repl',master_password='Mysql123!',MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=1551;
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
……
1 row in set (0.00 sec)
如果2臺mysql是克隆出來,出現如下錯誤:
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
改檔案或刪除 /data/mysql5/auto.cnf,再重啟mysql :
vi /data/mysql5/auto.cnf
[auto]
server-uuid=6ab71063-60f9-11e5-85e1-000c292a1fc5.
2.5為主庫準備
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 1762 | | | |
1 row in set (0.00 sec)
mysql> unlock table;
Query OK, 0 rows affected (0.00 sec)
3.master的互為主從
mysql> change master to master_host='10.15.107.171',master_user='repl',master_password='Mysql123!',MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=1762;
Query OK, 0 rows affected, 2 warnings (0.07 sec)
mysql> start slave;
Query OK, 0 rows affected (0.08 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4. MySQL同步測試
如上述均正確配置,現在任何一臺MySQL上更新資料都會同步到另一臺MySQL
三.keepalived安裝配置
3.1下載
keepalived-1.2.19.tar.gz
3.2安裝
yum install gcc gcc-c++ make openssl-devel kernel-devel ncurses-devel
tar zxf keepalived-1.2.19.tar.gz
./configure --disable-fwmark --prefix=/opt/keepalived
make && make install
mkdir /etc/keepalived
cp /opt/keepalived/sbin/keepalived /usr/sbin/
cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /opt/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ --add by lhf
chmod a+x /etc/init.d/keepalived #新增執行許可權
chkconfig keepalived on #設定開機啟動
service keepalived start #啟動
service keepalived stop #關閉
service keepalived restart #重啟
grant all on *.* to'root'@'10.15.%.%' identified by 'Mysql123!';
flush privileges;
3.3master的keepalived.conf
Interface的 配置檢視路徑/etc/sysconfig/network-scripts
/etc/keepalived/目錄下keepalived.conf的配置,主從配置檔案的區別priority,real_server不同。主機要配置nopreempt
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id db_master
}
vrrp_script chk_http_port {
script " /etc/keepalived/mysql_keepalived.sh " #在這裡新增指令碼連結
interval 3 #指令碼執行間隔
weight -20 #指令碼結果導致的優先順序變更
}
vrrp_instance VI_NODE {
state BACKUP #都是BACKUP
interface eth0 #注意介面
virtual_router_id 100 #vrrp組名,2節點相同
priority 110 #優先順序控制MS角色
advert_int 2
nopreempt #master必須不搶佔
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_http_port #新增指令碼執行
}
virtual_ipaddress {
10.15.107.188 #VIP
}
}
virtual_server 10.15.107.188 3306 { #VIP
delay_loop 2
lb_algo rr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 10.15.107.177 3306 { #RS的ip地址
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
3.4slave的keepalived.conf
--此處為/etc/keepalived/目錄下keepalived.conf的配置
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id db_master
}
vrrp_script chk_http_port {
script "/etc/keepalived/mysql_keepalived.sh" #在這裡新增指令碼連結
interval 3 #指令碼執行間隔
weight -20 #指令碼結果導致的優先順序變更
}
vrrp_instance VI_NODE {
state BACKUP
interface eth0 #注意介面
virtual_router_id 100 #vrrp組名,2節點相同
priority 100 #優先順序低
advert_int 2
authentication {
auth_type PASS
auth_pass 123456
}
track_script {
chk_http_port #新增指令碼執行
}
virtual_ipaddress {
10.15.107.188 #VIP地址
}
}
virtual_server 10.15.107.188 3306 { #VIP
delay_loop 2
lb_algo rr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 10.15.107.171 3306 { #對應RS的IP
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
3.5健康檢測指令碼
cat /etc/keepalived/mysql_keepalived.sh
#!/bin/bash
if [ $(ps -C mysqld --no-header | wc -l) -eq 0 ]; then
service mysqld restart
fi
sleep 2
if [ $(ps -C mysqld --no-header | wc -l) -eq 0 ]; then
/etc/init.d/keepalived stop
rm -f /var/lock/subsys/keepalived
fi
chmod a+x /opt/mysql5/mysql_keepalived.sh #必須授權才能執行成功.
3.6keepalived測試
1.監控keepalived日誌
tailf /var/log/messages
2.停掉master上的mysql或keepalived服務,都會自動切換VIP到另一臺主機上.
停掉mysql時:
3.在區域網內ping vip是否能ping通
4.用mysql客戶端連線ip為vip是否能連線上