linux上mysql MM(雙主)及keepalived搭建
一、主備機IP及VIP規劃:
master1 10.1.1.14 VIP 10.1.1.16
master2 10.1.1.15 VIP 10.1.1.16
二、mysql MM配置
1.修改master1的my.cnf
# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql/
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/mysqld.log
port = 3306
socket=/usr/local/mysql/mysql.sock
pid-file=/usr/local/mysql/mysql.pid
expire-logs-days=10
#binlog-do-db=db1
#binlog-ignore-db=db2
server-id = 1
log-bin = binlog
relay_log = relay-bin
log_slave_updates =1
auto_increment_increment=2
auto_increment_offset=1
2.修改master2的my.cnf
# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql/
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/mysqld.log
port = 3306
socket=/usr/local/mysql/mysql.sock
pid-file=/usr/local/mysql/mysql.pid
expire-logs-days=10
#binlog-do-db=db1
#binlog-ignore-db=db2
server-id=2
relay_log=relay-bin
log_bin =binlog
log_slave_updates =1
auto_increment_increment=2
auto_increment_offset=2
3.建立master1複製賬號
grant replication slave,replication client on *.* to 'repl'@'10.1.1.15' identified by 'repl';
4.建立master2複製賬號
grant replication slave,replication client on *.* to 'repl'@'10.1.1.14' identified by 'repl';
5.為master1配置master
show master status;
change master to
master_host='10.1.1.15',master_user='repl',master_password='repl',master_log_file='binlog.000005',master_log_pos=154;
6.為master2配置master
show master status;
change master to
master_host='10.1.1.14',master_user='repl',master_password='repl',master_log_file='binlog.000001',master_log_pos=154;
7.啟動slave
master1:
start slave;
master2:
start slave;
三、keepalived配置
1.編輯master1的keepalived配置檔案
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
#配置告警通知郵箱,可以配置多個
notification_email {
root@localhost
}
#配置郵件傳送目標地址
notification_email_from mysql@xiaomi.com
#配置smtp伺服器地址,其必須存在
smtp_server 10.1.1.11
#配置連線smtp伺服器的超時時間
smtp_connect_timeout 30
#設定執行Keepalived例項的標識,其將顯示於郵件標題中
router_id mysql_ha
}
#監控指令碼
vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 2
weight 2
}
#配置VRRP例項,例項命名任意
vrrp_instance mysql-ha {
#配置Keepalived角色,MASTER為主機 BACKUP為備機,此處兩個都設定為BACKUP
state BACKUP
#配置keepalived監測的網路介面
interface eth0
#虛擬路由標識,其為一個(1-255)的數字,一個VRRP例項中主機的該ID必須相同
virtual_router_id 66
#伺服器優先順序,數字越大優先順序越高,一個例項中主伺服器優先順序要高於備伺服器
priority 50
#配置主備伺服器間同步檢查的時間間隔(秒)
advert_int 1
#配置伺服器搶佔模式,這裡配置為非搶佔模式(只需對master1配置即可)
nopreempt
#配置驗證型別和密碼
authentication {
#兩種驗證型別{PASS|HA}
auth_type PASS
#指定驗證密碼,一個例項中的主備伺服器密碼要一樣
auth_pass centos
}
track_script {
#指定執行監控的服務
chk_mysql
}
#配置虛擬IP,可指定有多個,每個佔一行
virtual_ipaddress {
10.1.1.16
}
}
2.編輯master1心跳檢測指令碼:
#vi /etc/keepalived/check_mysql.sh
#!/bin/bash
#This scripts is check for Mysql Slave status
counter=$(netstat -na|grep "LISTEN"|grep "3311"|wc -l)
if [ "${counter}" -eq 0 ]; then
service keepalived stop
killall keepalived
fi
ping 10.1.1.14 -w1 -c1 &>/dev/null
if [ $? -ne 0 ]
then
systemctl stop keepalived
killall keepalived
fi
3.編輯master2的keepalived配置檔案
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
#配置告警通知郵箱,可以配置多個
notification_email {
root@localhost
}
#配置郵件傳送目標地址
notification_email_from mysql@xiaomi.com
#配置smtp伺服器地址,其必須存在
smtp_server 10.1.1.11
#配置連線smtp伺服器的超時時間
smtp_connect_timeout 30
#設定執行Keepalived例項的標識,其將顯示於郵件標題中
router_id mysql_ha
}
# 監控監本
vrrp_script chk_mysql {
script "/etc/keepalived/check_mysql.sh"
interval 2
weight 2
}
#配置VRRP例項,例項命名任意
vrrp_instance mysql-ha {
#配置Keepalived角色,MASTER為主機 BACKUP為備機,此處兩個都設定為BACKUP
state BACKUP
#配置keepalived監測的網路介面
interface eth0
#虛擬路由標識,其為一個(1-255)的數字,一個VRRP例項中主機的該ID必須相同
virtual_router_id 66
#伺服器優先順序,數字越大優先順序越高,一個例項中主伺服器優先順序要高於備伺服器
priority 49
#配置主備伺服器間同步檢查的時間間隔(秒)
advert_int 1
#配置伺服器搶佔模式,這裡配置為非搶佔模式(只需對master1配置即可)
#nopreempt
#配置驗證型別和密碼
authentication {
#兩種驗證型別{PASS|HA}
auth_type PASS
#指定驗證密碼,一個例項中的主備伺服器密碼要一樣
auth_pass centos
}
track_script {
#指定執行監控的服務
chk_mysql
}
#配置虛擬IP,可指定有多個,每個佔一行
virtual_ipaddress {
10.1.1.16
}
}
4.編輯master2檢測指令碼
# vim /etc/keepalived/check_mysql.sh
#!/bin/bash
#This scripts is check for Mysql Slave status
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
killall keepalived
fi
ping 10.1.1.15 -w1 -c1 &>/dev/null
if [ $? -ne 0 ]
then
service keepalived stop
killall keepalived
fi
5.vip漂移檢測
1)master1和master2上同時開啟keepalived和mysql
#service keepalived start
#service mysqld start
2)檢視master1上ip地址
ip addr
3)登入10.1.1.16上的mysql
mysql -uusername -ppassword -h10.1.1.16 -P3311
4)停掉master1上的mysql服務
service mysqld stop
5)觀察master1和master2上的ip地址
ip addr
6)繼續在3)中的session中執行mysql命令,看看發生了什麼
mysql> use information_schema;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8484829/viewspace-2674315/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL叢集搭建(6)-雙主+keepalived高可用MySql
- MySQL主從配置及haproxy和keepalived搭建MySql
- MySQL 高可用性—keepalived+mysql雙主MySql
- mysql雙主雙從 搭建配置MySql
- mysql5.6主主複製及keepalived 高可用MySql
- MYSQL8+CENTOS7.6 主從+keepalived搭建總結MySqlCentOS
- MySql雙主一從服務搭建MySql
- MYSQL5.7.22 原始碼安裝 主從搭建 + KEEPALIVED高可用MySql原始碼
- mysql高可用衡搭建(Keepalived)MySql
- MySQL主主模式+Keepalived高可用MySql模式
- MySQL進階:主主複製+Keepalived高可用MySql
- Mysql主從複製原理及搭建MySql
- 基於MySQL雙主複製架構下部署LVS+KeepAlived負載均衡MySql架構負載
- 基於Linux的docker mysql主從搭建LinuxDockerMySql
- 使用Keepalived實現Nginx的自動重啟及雙主熱備高可用Nginx
- mysql 5.7 主從複製搭建及原理MySql
- MySQL雙主雙從配置MySql
- RocketMQ雙主雙從叢集搭建MQ
- Redis高可用方案:使用Keepalived實現主備雙活Redis
- 使用Keepalived實現Nginx的雙機主備高可用Nginx
- Mysql雙主雙從高可用叢集的搭建且與MyCat進行整合MySql
- mysql主從搭建MySql
- rocketMq之雙主雙從同步模式搭建MQ模式
- Keepalived - Keepalived 實現 tomcat雙機熱備Tomcat
- keepalived 主備使用
- RocketMQ4.7.1雙主雙從叢集搭建MQ
- centos7上配置mysql8的雙主互寫CentOSMySql
- MYSQL主從搭建5.6.38MySql
- MYSQL5.6.40原始碼安裝 主從搭建 主主搭建MySql原始碼
- mysql 5.7+keepalived主從切換步驟簡述MySql
- linux svn server搭建、多專案管理及主備方案LinuxServer專案管理
- linux 伺服器上 jmeter 的搭建及使用Linux伺服器JMeter
- MySQL叢集搭建(1)-主備搭建MySql
- MySQL高可用架構之Keepalived+主從架構部署MySql架構
- Docker 方式 MySQL 主從搭建DockerMySql
- Mysql主從搭建(docker compose)MySqlDocker
- mysql主從複製搭建MySql
- 基於 Docker 的 MySQL 主從複製搭建及原理(真正弄懂)DockerMySql