两台机器分别安装mysql,这里用的版本是mysql 5.6.17
mysql 安装包和安装手册地址:
链接:https://pan.baidu.com/s/1MutHmknehN2_kWsZ5zF1Gw
提取码:kykn
2.双机热备
登录服务器A:136 进入mysql
在服务器上创建用户并且赋权:
create user 'actia'@'192.168.71.136' identified by 'test@123456';
GRANT REPLICATION SLAVE ON *.* TO 'actia'@'192.168.71.138';
Flush privileges;
登录服务器B:138 进入 mysql
在服务器上创建用户并且赋权:
create user 'actia'@'192.168.71.138' identified by 'test@123456';
GRANT REPLICATION SLAVE ON *.* TO 'actia'@'192.168.71.136';
Flush privileges;
创建完成后,通过终端主机去访问对方主机测试
服务器A: mysql -h192.168.71.138 -uactia -ptest@123456;
服务器B: mysql -h192.168.71.136 -uactia -ptest@123456;
如果登录成功,则可以对两台服务器进行双机热备操作。
2.1 修改mysql 配置文件
修改配置文件
服务器A:136
# 唯一id
server-id=1
# 默认为1
innodb_flush_log_at_trx_commit=2
#binlog日志文件名(可以任意命名)
log-bin=mysql-bin-1
# 记录日志的数据库(如果没有此项,表示同步所有的库)
#binlog-do-db=test
# 不记录日志的数据库
binlog-ignore-db=mysql,information_schema,performance_schema,sys
#开启binlog日志同步功能
sync_binlog=1
# 跳过1146错误
slave_skip_errors=1146
服务器B: 138
# 唯一id
server-id=2
# 默认为1
innodb_flush_log_at_trx_commit=2
#binlog日志文件名(可以任意命名)
log-bin=mysql-bin-2
# 记录日志的数据库
#binlog-do-db=test
# 不记录日志的数据库
binlog-ignore-db=mysql,information_schema,performance_schema,sys
#开启binlog日志同步功能
sync_binlog=1
# 跳过1146错误
slave_skip_errors=1146
2.2 重启并且同步
重启 服务器A 和服务器B
查看两服务器得主机状态
服务器A:136
服务器B:138
同步配置
服务器A:136
stop slave;
change master to
master_host='192.168.71.138',master_user='actia',master_password='test@123456',
master_log_file='mysql-bin-2.000001',master_log_pos=320;
start slave;
服务器B:138
stop slave;
change master to
master_host='192.168.71.136',master_user='actia',master_password='test@123456',
master_log_file='mysql-bin-1.000001',master_log_pos=120;
start slave;
2.3查看服务器状态
服务器A:136
服务器B:138
如果如下两项状态为yes则说明配置成功:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
2.4 验证
任意一台Mysql 删除新增操作,数据都会同步。
3 配置keepalived实现高可用
官网地址:http://www.keepalived.org(以下以2.0.6版本为例)
安装位置:/usr/local/
解压到 /usr/local
安装依赖包
yum -y install curl gcc openssl-devel libnl3-devel net-snmp-devel
yum -y install libnfnetlink-devel
yum -y install ipvsadm
安装keepalived
cd keepalived
./configure
make && make install
生成启动脚本
cat /usr/lib/systemd/system/keepalived.service
将keepalved加进系统服务并且在/etc/sysconfig/写入配置文件以及将keepalived命令/usr/bin 和 /usr/sbin
具体操作
cp /usr/local/keepalived/keepalived/etc/init.d/keepalived /etc/init.d/keepalived #最好查看下keeplaived是否有执行权限
mkdir /etc/keepalived/
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/sbin/keepalived /usr/bin/ #方便以后查看keepalived的版本,用keepalived -v可以查看
cp /usr/local/sbin/keepalived /usr/sbin/
修改配置文件
vi /etc/keepalived/keepalived.conf
配置服务器A:136
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
#nopreempt #不抢占模式,只有优先级高的机器上设置即可,优先级低的机器可不设置
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.71.200
}
}
virtual_server 192.168.71.200 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60 #会话保持时间
protocol TCP
real_server 192.168.71.136 3306 {
weight 3
notify_down /root/shutdown.sh #检测到服务down后执行的脚本
TCP_CHECK {
nb_get_retry 3 #重连次数
connect_timeout 10 #连接超时时间
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
}
服务器B:138
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL # 标识本节点的字条串,通常为hostname
vrrp_skip_check_adv_addr
#vrrp_strict #这个最好注释掉,否则可能造成物理机的浏览器无法访问应用
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
#state MASTER
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { # Block limited to 20 IP addresses @IP
192.168.71.200
}
}
#虚拟服务器定义块
virtual_server 192.168.71.200 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.71.138 {
weight 3
notify_down /root/shutdown.sh
TCP_CHECK {
nb_get_retry 3 #重连次数
connect_timeout 10 #连接超时时间
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
}
编写检测服务down后所要执行的脚本shutdown.sh
shutdown.sh 内容
#!/bin/bash
killall keepalive
启动keepalived并查看日志
chkconfig keepalived on
service keepalived start #启动服务
service keepalived stop #停止服务
service keepalived restart #重启服务
systemctl enable keepalived.service #设置开机启动
启动后查看日志
tail -f /var/log/messages
测试
远程客户端通过vip登录测试