博客分享的第一篇技術(shù)文章;
項(xiàng)目主要搭建:主主數(shù)據(jù)庫(kù)高可用集群搭建。
數(shù)據(jù)庫(kù)互為主備,應(yīng)用技術(shù):MM+keepalived
使用的是虛擬機(jī)搭建的實(shí)驗(yàn)向大家展示:
數(shù)據(jù)庫(kù)1:192.168.4.7
數(shù)據(jù)庫(kù)2:192.168.4.77
VIP:192.168.4.68
web1:192.168.4.69
web2:192.168.4.70
一、安裝MySQL,部署主主同步結(jié)構(gòu)。
直接yum安裝
配置主主同步:
由于主數(shù)據(jù)庫(kù)192.168.4.7里面存放著數(shù)據(jù),所以需要先導(dǎo)出數(shù)據(jù),方法很多,我們采取mysqldump:
#mysqldump -uroot -p123456 --all-databases > /opt/all.sql
然后需要將數(shù)據(jù)拷貝到數(shù)據(jù)庫(kù)192.168.4.77。
數(shù)據(jù)庫(kù)2:
1、修改配置文件:
數(shù)據(jù)庫(kù)192.168.4.7
[mysqld]
server_id=7 #兩臺(tái)主機(jī)不能重復(fù)
log-bin=master7 #開(kāi)啟binlog功能
log-bin-index=master7.index
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog_format="mixed"
數(shù)據(jù)庫(kù)192.168.4.77
[mysqld]
#vim
/etc/my.cnf
[mysqld]
log-bin=master77
server_id=77
binlog_format="mixed"
log_slave_updates
datadir=/var/lib/
mysqlsocket=/var/lib/mysql/mysql.sock
:wq
依次啟動(dòng)兩臺(tái)數(shù)據(jù)庫(kù):systemctl
start mariadb
2、導(dǎo)入數(shù)據(jù)庫(kù):
將數(shù)據(jù)庫(kù)192.168.4.7備份出來(lái)的數(shù)據(jù)庫(kù)導(dǎo)入到數(shù)據(jù)庫(kù)192.168.4.77,以此保持兩端數(shù)據(jù)一致
mysql -uroot -p123456 < all.sql
3、配置用戶授權(quán)并查看master信息:
數(shù)據(jù)庫(kù)192.168.4.7
給用戶授權(quán):允許192.168.4.77的數(shù)據(jù)庫(kù)用戶slave擁有replication slave權(quán)限:
mysql>grant replication slave on *.* to slave@"192.168.4.77" identified by "123456";
mysql>show master status; //查看狀態(tài);
數(shù)據(jù)庫(kù)192.168.4.77
給用戶授權(quán):允許192.168.4.7的數(shù)據(jù)庫(kù)用戶slave擁有replication slave權(quán)限:
mysql>grant replication slave on *.* to slave@"192.168.4.7" identified by "123456";
mysql>show master status; //查看狀態(tài);
4、配置相互主從,并開(kāi)啟slave模式:
數(shù)據(jù)庫(kù)192.168.4.7
#mysql -uroot -p123456
mysql>change master to master_host="192.168.4.77",master_user="slave",master_password="12
3456",master_log_file="master77.000001",master_log_pos=357;
mysql>start slave; //啟動(dòng)slave
mysql>show slave status\G; //查看slave狀態(tài)
數(shù)據(jù)庫(kù)192.168.4.77
mysql>change master to master_host="192.168.4.7",master_user="slave",master_password="123
456",master_log_file="master7.000001",master_log_pos=357;
mysql>start slave;
mysql>show slave status\G; //查看slave狀態(tài)
狀態(tài)都為yes是正常,否則主主同步異常,需要進(jìn)一步排查
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
主主同步配置完成后可以進(jìn)行一些建庫(kù),建表的測(cè)試測(cè)試,看看兩邊的數(shù)據(jù)是否一致
二、安裝keepalived并設(shè)置監(jiān)控
keepalived是安裝在兩臺(tái)MySQL服務(wù)器上的
首先安裝keepalived過(guò)程不解釋就正常解壓安裝就好
1、修改配置文件/etc/keepalived/keepalived.conf
!
Configuration File for keepalived
global_defs
{
notification_email
{
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from
Alexandre.Cassen@firewall.loc
smtp_server
192.168.200.1
smtp_connect_timeout
30
router_id
LVS_DEVEL
}
vrrp_script
check_mysql {
script "/opt/mysql.sh" //監(jiān)控腳本存放位置(最后有參考腳本,也可以自己寫)
interval 2 //(檢測(cè)腳本執(zhí)行的間隔)
weight 2
}
vrrp_instance
VI_1 {
state MASTER //備份服務(wù)器上將MASTER改為 BACKUP
interface eth0
virtual_router_id 51 //主、備機(jī)的virtual_router_id必須相同
priority 100 //主、備機(jī)取不同的優(yōu)先級(jí),主機(jī)值較大,備份機(jī)值較小,備份服
務(wù)器設(shè)置為99
advert_int 1
authentication
{
auth_type
PASS
auth_pass
1111 //主、備機(jī)一樣
}
track_script
{
check_mysql //檢測(cè)腳本
}
virtual_ipaddress
{
192.168.4.68 //VIP(虛擬IP,對(duì)外提供服務(wù)的IP)
}
}
數(shù)據(jù)庫(kù)192.168.4.77配置文件:
vim
/etc/keepalived/keepalived.conf
!
Configuration File for keepalived
global_defs
{
notification_email
{
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from
Alexandre.Cassen@firewall.loc
smtp_server
192.168.200.1
smtp_connect_timeout
30
router_id
LVS_DEVEL
}
vrrp_script
check_mysql {
script
"/opt/mysql.sh"
interval 2
weight 2
}
vrrp_instance
VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication
{
auth_type
PASS
auth_pass
1111
}
track_script
{
check_mysql
}
virtual_ipaddress
{
192.168.4.68
}
}
******************************************************************************
監(jiān)控腳本的作用是:實(shí)現(xiàn)keepalived主備的切換
附:監(jiān)控腳本
vim
/opt/ mysql.sh
#!/bin/bash
a=`ps
-C mysqld --no-header | wc -l`
if
[ $a -eq 0 ];then
systemctl
start mariadb
sleep
3
b=`ps
-C mysqld --no-header | wc -l`
if
[ $b -eq 0 ];then
killall
keepalived
fi
fi
新聞名稱:MM(主主數(shù)據(jù)庫(kù))+keepalived主備高可用集群
本文地址:
http://weahome.cn/article/gehojj.html