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

服务器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 {
[email protected]
}
notification_email_from [email protected]
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 {
[email protected]
}
notification_email_from [email protected]
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添加一个定时任务来监控。

你可能感兴趣的:(Firefly,ARM,MySQL,KeepAlived)