Keepalived+MariaDB10配置双主高可用数据库
目前创新互联已为近千家的企业提供了网站建设、域名、虚拟空间、网站托管、服务器租用、企业网站设计、宝应网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
OS | RS | 子网掩码 | 路由网关 |
Centos6.6 MariaDB10 Keepalived | Eth0:192.168.26.210 | 255.255.252.0 | 192.168.25.3 |
VIP:192.168.27.210 | 255.255.255.252 | ||
Centos6.6 MariaDB10 Keepalived | Eth0:192.168.26.211 | 255.255.252.0 | 192.168.25.3 |
VIP:192.168.27.210 | 255.255.255.255 | ||
Win7(client) | IP:192.168.26.70 | 255.255.252.0 | 192.168.25.3 |
在两台服务器上分别安装MYSQL:
yum -y install MariaDB (这里略)
安装完后配置双主模型:
编辑配置文件26.210:vim /etc/my.cnf.d/server.cnf
在配置文件中加入以前几个参数设置:
log-bin=mysql-bin
server-id = 210
relay-log =relay-bin
编辑配置文件26.211:vim /etc/my.cnf.d/server.cnf
log-bin =mysql-bin
server-id = 211
relay-log = relay-bin
分别启动两台服务器的MYSQL服务:service mysql start
服务器设置双主:
26.210:
mysql
grant replication slave ,replication client on *.* to 'jerry'@'192.168.%.%' identified by 'jerrypass';
change master to master_host='192.168.26.211',master_user='jerrymy',master_password='jerrypass',master_log_file='mysql-bin.000006',master_log_pos=558;
start slave;
show slave status\G; #查看从状态IO和SQL 运行状态均为YES表示配置成功
26.211:
mysql
grant replication slave ,replication client on *.* to 'jerrymy'@'192.168.%.%' identified by 'jerrypass';
change master to master_host='192.168.26.210',master_user='jerry',master_password='jerrypass',master_log_file='mysql-bin.000007',master_log_pos=540;
start slave;
show slave status\G; #查看从状态IO和SQL 运行状态均为YES表示配置成功
显示状态为成功,我们再创建数据库来测试,看看双主是否运行正常。
双主设置成功。
26.210:安装Keepalived
安装报错未安装gcc
yum -y install gcc
yum -y install openssl-devel
安装组件后编译成功:
编辑配置文件:vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {
router_id mysql-ha
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 20
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.27.210
}
}
virtual_server 192.168.27.210 3306 {
delay_loop 2
lb_algo rr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.26.210 3306 {
weight 1
notify_down /etc/keepalived/mysql.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 2
delay_before_retry 1
}
}
}
保存退出启动Keepalived服务。
service keepalived start
26.211:安装Keepalived。
编辑配置文件26.211: vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs {
router_id mysql-ha
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 20
priority 99
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.27.210
}
}
virtual_server 192.168.27.210 3306 {
delay_loop 2
lb_algo rr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.26.211 3306 {
weight 1
notify_down /etc/keepalived/mysql.sh
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 2
delay_before_retry 1
}
}
}
保存退出启动26.211上的Keepalived
service keepalived start
测试VIP是否可用:ping 192.168.27.210 -t
回到26.210或211都可以 上,创建一个数据库访问测试用户账号密码。
grant all on *.* to 'jerrytest'@'192.168.%.%' identified by 'jerrypass';
下面通过 VIP访问数据库观察访问:
VIP在26.210这台服务器上,因此我们访问的26.210上的数据库。
下面停掉26.210上的MYSQL观察:
首先观察VIP网络出现波动后恢复。
数据库访问成功:
VIP已经转移动26.211服务器上了。
我们再恢复26.210上的MYSQL数据库服务观察。
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。