MySQL主主复制+Keepalived2.0实现mysql高可用

1 篇文章 0 订阅
1 篇文章 0 订阅

服务器1:192.168.1.100(server1)
服务器2:192.168.1.101(server2)
服务器操作系统:Firefly-Ubuntu16.04

MySQL5.7主主复制

安装mysql-server

#更新软件包列表
apt-get update
#安装mysql-server
apt-get install mysql-server

配置Server1

sudo vi /etc/mysql/my.cnf
bind-address=192.168.1.100
server-id=1
log_bin=/var/local/mysql/mysql-bin.log
expire_logs_days					=	10
max_binlog_size 					=	100M
binlog_ignore_db					=	mysql
auto_increment_increment			=	2
auto_increment_offset				=	1
sudo systemctl start mysql
mysql -uroot -p
mysql>grant replication slave,repilcation client,super,reload on *.* to 'replicate'@'192.168.1.101' identified by 'password';
mysql>exit;

配置Server2

sudo vi /etc/mysql/my.cnf
bind-address=192.168.1.101
server-id=3
log_bin=/var/local/mysql/mysql-bin.log
expire_logs_days					=	10
max_binlog_size 					=	100M
binlog_ignore_db					=	mysql
auto_increment_increment			=	2
auto_increment_offset				=	2
sudo systemctl start mysql
mysql -uroot -p
mysql>grant replication slave,repilcation client,super,reload on *.* to 'replicate'@'192.168.1.100' identified by 'password';
mysql>exit;

继续配置Server1

mysql -uroot -p
mysql>change master to 
->master_host='192.168.1.101',
->master_port=3306,
->master_user='replicate',
->master_password='replicate',
->master_log_file='mysql-bin.000001',#根据另一台服务器的show master status
->master_log_pos=154;#根据另一台服务器的show master status
mysql>start slave;
mysql>exit;

继续配置Server2

mysql -uroot -p
mysql>change master to 
->master_host='192.168.1.100',
->master_port=3306,
->master_user='replicate',
->master_password='replicate',
->master_log_file='mysql-bin.000001',#根据另一台服务器的show master status
->master_log_pos=164;#根据另一台服务器的show master status
mysql>start slave;
mysql>exit;

Keepalived-2.0安装

去keepalived官网下载tar.gz包,解压到/tmp目录下:

tar -zxvf keepalived-2.0.18.tar.gz /tmp/
cd /tmp/keepalived-2.0.18
apt-get install openssl libssl-dev
sudo ./configure
sudo make
sudo make install

设置keepalived开机自启动

sudo systemctl enable keepalived

配置文件Server1

#/etc/keepalived/keepalived.conf
! Configuraion file  for Keepalived
global_defs {
router_id 1.100
notification_email {
xxx@163.com
}
notification_email_from xxx@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
}
vrrp_instance V_mysql_1 {
state BACKUP
interface eth0
virtual_router_id 233
priority 100
advert_int 1
nopreemept
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.1.150/24
}
}

配置文件Server2

#/etc/keepalived/keepalived.conf
! Configuraion file  for Keepalived
global_defs {
router_id 1.101 #更改路由ID
notification_email {
xxx@163.com
}
notification_email_from xxx@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
}
vrrp_instance V_mysql_1 {
state BACKUP
interface eth0
virtual_router_id 233
priority 90 #更改优先级
advert_int 1
nopreemept
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
192.168.1.150/24
}
}

把两台服务器的keepalived服务启动起来就可以做测试啦!

systemctl start keepalived

DONE.

P.S:关于mysql服务监控,可以编写脚本放进keepalived配置文件里进行监控,也可以写一个脚本判断同步mysql和keepalived的启动停止状态并用crontab添加一个定时任务来监控。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值