mysql 主主模式+keeplived
2022-07-14 本文已影响0人
JepWei
1、主主模式
主数据库(128.113.2.156)
vim /etc/my.cnf #在my.cnf文件下添加以下配置
log-slave-updates=on
log-bin=master-bin
log-bin-index=master-bin.index
server-id=156
expire-logs-days=7
sync_binlog=1
sync_relay_log=1
sync_relay_log_info=1
slave-skip-errors=1032
从数据库(128.113.2.158)
vim /etc/my.cnf #在my.cnf文件下添加以下配置
log-slave-updates=on
log-bin=slave-bin
log-bin-index=slave-bin.index
server-id=158
expire-logs-days=7
sync_binlog=1
sync_relay_log=1
sync_relay_log_info=1
slave-skip-errors=1032
主数据库(128.113.2.156)(mysql命令)
create user 'repl_master'@'%' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl_master'@'%';
FLUSH PRIVILEGES;
flush tables with read lock;
从数据库(128.113.2.158)(mysql命令)
create user 'repl_slave'@'%' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl_slave'@'%';
FLUSH PRIVILEGES;
flush tables with read lock;
完成以上配置后,主从数据库均执行重启数据库命令
systemctl restart mysqld
主数据库(128.113.2.156)(mysql命令)
show master status; #观察pos值与binlog文件名
从数据库(128.113.2.158)(mysql命令)
change master to master_host='128.113.2.156',master_port=3306,master_user='repl_master',master_password='123456',master_log_file='master-bin.000001',master_log_pos=154;
start slave; #开启
show slave status\G #观察状态如有两个yes说明成功
show master status; #观察pos值与binlog文件名
unlock tables;
主数据库(128.113.2.156)(mysql命令)
change master to master_host='128.113.2.158',master_port=3306,master_user='repl_slave',master_password='123456',master_log_file='slave-bin.000001',master_log_pos=154;
start slave; #开启slave
show slave status\G #观察slave状态如有两个yes说明成功
show master status; #观察pos值与binlog文件名
unlock tables;
2、keeplived
主从数据库均部署Keeplived服务
yum install keepalived #yum安装,无网络也可用rpm离线安装
vim /etc/keepalived/keepalived.conf #编辑keeplived配置文件
# 全局配置 不用动 只需注释掉vrrp_strict
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_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
# 检查mysql服务是否存活的脚本
vrrp_script chk_mysql {
script "/usr/bin/killall -0 mysqld"
}
# vrrp配置虚IP
vrrp_instance VI_1 {
# 状态:MASTER 另外一台机器为BACKUP
state MASTER
# 绑定的网卡
interface eth0
# 虚拟路由id 两台机器需保持一致
virtual_router_id 51
# 优先级 MASTER的值要大于BACKUP
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟IP地址 两台keepalived需要一致
virtual_ipaddress {
192.168.73.150
}
# 检查脚本 vrrp_script的名字
track_script {
chk_mysql
}
}
###后边的virtual_server全部注释掉 它是和LVS做负载均衡用的 这里用不到
两台数据库的keeplived配置文件均配置好后均执行以下命令即可
service keepalived start
主从关系报错信息处理:
临时解决:
stop slave;
set global sql_slave_skip_counter=1;
start slave;