Keepalived +Mysql 主主同步

一、配置Mysql主主同步
1,修改/etc/my.cnf,创建同步用户
Master104
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server-id = 1                    #backup这台设置2
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema       #忽略写入binlog日志的库
auto-increment-increment = 2             #字段变化增量值
auto-increment-offset = 1              #初始字段ID为1
slave-skip-errors = all                       #忽略所有复制产生的错误
[[email protected] ~]# service mysqld restart
#查看log bin日志和pos值位置
[[email protected] ~]# mysql -uroot -p
mysql> grant replication slave on *.* to ‘replication‘@‘192.168.9.%‘ identified by ‘replication‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

Backup106
#106配置Mysql主主同步
[[email protected] ~]# vim /etc/my.cnf 
[mysqld]
server-id = 2                    
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema       
auto-increment-increment = 2             
auto-increment-offset = 1              
slave-skip-errors = all    
[[email protected] ~]# service mysqld restart
#查看log bin日志和pos值位置
[[email protected] ~]# mysql -uroot -p
mysql> grant replication slave on *.* to ‘replication‘@‘192.168.9.%‘ identified by ‘replication‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

2、配置Mysql主主同步
Master 104
[[email protected] ~]# mysql -uroot -p
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000002 | 625 | | msyql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

mysql> change master to
 -> master_host=‘192.168.9.106‘,
 -> master_user=‘replication‘,
 -> master_password=‘replication‘,
 -> master_log_file=‘mysql-bin.000002‘,
 -> master_log_pos=356;
Query OK, 0 rows affected (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
 Slave_IO_State: Waiting for master to send event
 Master_Host: 192.168.9.106
 Master_User: replication
 Master_Port: 3306
 Connect_Retry: 60
 Master_Log_File: mysql-bin.000002
 Read_Master_Log_Pos: 356
 Relay_Log_File: 104-relay-bin.000002
 Relay_Log_Pos: 253
 Relay_Master_Log_File: mysql-bin.000002
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes

Backup 106
[[email protected] ~]# mysql -uroot -p
mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000002 | 356 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

mysql> change master to
-> master_host=‘192.168.9.104‘,
-> master_user=‘replication‘,
-> master_password=‘replication‘,
-> master_log_file=‘mysql-bin.000002‘,
-> master_log_pos=625;
Query OK, 0 rows affected (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
 Slave_IO_State: Waiting for master to send event
 Master_Host: 192.168.9.104
 Master_User: replication
 Master_Port: 3306
 Connect_Retry: 60
 Master_Log_File: mysql-bin.000002
 Read_Master_Log_Pos: 625
 Relay_Log_File: 106-relay-bin.000002
 Relay_Log_Pos: 253
 Relay_Master_Log_File: mysql-bin.000002
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes

#如果Slave_IO_Running和Slave_SQL_Running为Yes,则配置主主正常,可以创建测试下

二、配置keepalived实现双主热备
1,#下载安装keepalived
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
[[email protected] src]# yum -y install gcc gcc-c++ gcc-g77 ncurses-devel bison libaio-devel cmake libnl* libpopt* popt-static openssl-devel
[[email protected] src]# tar xf keepalived-1.2.13.tar.gz 
[[email protected] src]# cd keepalived-1.2.13
[[email protected] keepalived-1.2.13]# ./configure --prefix=/usr/local/keepalived  --disable-fwmark 
[[email protected] keepalived-1.2.13]# make
[[email protected] keepalived-1.2.13]# make install
2,#配置keepalived配置成系统服务
[[email protected] keepalived-1.2.13]# cd /usr/local/keepalived/
[[email protected] keepalived]# cp etc/rc.d/init.d/keepalived /etc/init.d/
[[email protected] keepalived]# cp etc/sysconfig/keepalived /etc/sysconfig/
[[email protected] keepalived]# mkdir /etc/keepalived
[[email protected] keepalived]# cp etc/keepalived/keepalived.conf /etc/keepalived/
[[email protected] keepalived]# cp sbin/keepalived /usr/sbin/
[[email protected] keepalived]# chkconfig keepalived on
[[email protected] keepalived]# chkconfig --list keepalived 
keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  3,#配置mysql热备
Mater 104
[[email protected] keepalived]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
        router_id Mysql_HA    #标识,双主相同
}

vrrp_instance zabbix_mysql {
        state Backup               #俩台都设置Backup
        interface eth0
        virtual_router_id 53     #主备相同
        priority 100                 #优先级,backup设置90
        advert_int 1
        nopreempt                  #不主动抢占资源,只在master这台优先级高的设置,backup不设置
        authentication {
                auth_type PASS
                auth_pass sina 
        }
        virtual_ipaddress {
                192.168.9.123
         }
   }
   virtual_server 192.168.9.123 3306 {
         delay_loop 6
         nat_mask 255.255.255.0
         persistence_timeout 50
         protocol TCP
         real_server 192.168.9.104 3306 {
                  weight 3
                  notify_down "/etc/keepalived/mysql.sh"
                  TCP_CHECK {
                          connect_timeout 5
                          nb_get_retry 2
                          delay_before_retry 3
                  }
         }
 }
[[email protected] keepalived]# vim /etc/keepalived/mysql.sh
#!/bin/bash
/etc/init.d/keepalived stop

#启动keepalived
[[email protected]104 keepalived]# /etc/init.d/keepalived start
[[email protected] keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:ff:a5:92 brd ff:ff:ff:ff:ff:ff
 inet 192.168.9.104/24 brd 192.168.9.255 scope global eth0
 inet 192.168.9.123/32 scope global eth0
 inet6 fe80::20c:29ff:feff:a592/64 scope link 
 valid_lft forever preferred_lft forever

Backup106
[[email protected] keepalived]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
        router_id Mysql_HA    
}

vrrp_instance zabbix_mysql {
        state Backup               
        interface eth0
        virtual_router_id 53     
        priority 90                 
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass sina 
        }
        virtual_ipaddress {
                192.168.9.123
         }
   }
   virtual_server 192.168.9.123 3306 {
         delay_loop 6
         nat_mask 255.255.255.0
         persistence_timeout 50
         protocol TCP
         real_server 192.168.9.106 3306 {
                  weight 3
                  notify_down "/etc/keepalived/mysql.sh"
                  TCP_CHECK {
                          connect_timeout 5
                          nb_get_retry 2
                          delay_before_retry 3
                  }
         }
 }
[[email protected] keepalived]# vim /etc/keepalived/mysql.sh
#!/bin/bash
/etc/init.d/keepalived stop
[[email protected] keepalived]# chmod +x /etc/keepalived/mysql.sh
#启动keepalived
[[email protected] keepalived]# /etc/init.d/keepalived start
[[email protected] keepalived]# ip addr

4,验证
104停止mysqld
[[email protected] keepalived]# service mysqld stop
Shutting down MySQL.... SUCCESS! 
[[email protected] keepalived]# ip addr 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:ff:a5:92 brd ff:ff:ff:ff:ff:ff
 inet 192.168.9.104/24 brd 192.168.9.255 scope global eth0
 inet6 fe80::20c:29ff:feff:a592/64 scope link 
 valid_lft forever preferred_lft forever
106
[[email protected] keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 00:0c:29:c1:76:6b brd ff:ff:ff:ff:ff:ff
 inet 192.168.9.106/24 brd 192.168.9.255 scope global eth0
 inet 192.168.9.123/32 scope global eth0
 inet6 fe80::20c:29ff:fec1:766b/64 scope link 
 valid_lft forever preferred_lft forever
时间: 2024-12-25 04:46:58

Keepalived +Mysql 主主同步的相关文章

keepalived+mysql双主

       mysql高可用keepalived+mysql双主 MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,PXC,MMM,Heartbeat+DRBD等,比较常用的是keepalived+双主,MHA和PXC. 本节主要介绍了利用 keepalived 实现 MySQL 数据库的高可用. Keepalived+mysql双主来实现MySQL-HA,我们必须保证两台MySQL数据库的数据完全一样,基本思路是两台MySQL互为主从关系,通过Keepalived配

MySQL 高可用性之keepalived+mysql双主

生产环境中一台mysql主机存在单点故障,所以我们要确保mysql的高可用性,即两台MySQL服务器如果其中有一台MySQL服务器挂掉后,另外一台能立马接替其进行工作. MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,PXC,MMM,Heartbeat+DRBD等,比较常用的是keepalived+双主,MHA和PXC. 这里将主要介绍了利用 keepalived 实现 MySQL数据库的高可用. Keepalived+mysql双主来实现MySQL-HA,我们必须保

MySQL 高可用性——keepalived+mysql双主(有详细步骤和全部配置项解释)

博主QQ:819594300 博客地址:http://zpf666.blog.51cto.com/ 有什么疑问的朋友可以联系博主,博主会帮你们解答,谢谢支持! 前言:生产环境中一台mysql主机存在单点故障,所以我们要确保mysql的高可用性,即两台MySQL服务器如果其中有一台MySQL服务器挂掉后,另外一台能立马接替其进行工作. MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,PXC,MMM,Heartbeat+DRBD等,比较常用的是keepalived+双主,

MySQL 高可用之 keepalived+Mysql 双主双活。

环境描述:[[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) IP 规划:MySQL-M 192.168.10.10 MySQL-S 192.168.10.20 配置两服务服务器双主: MySQL 安装: [[ema

keepalived+mysql双主复制高可用方案

MySQL双主复制,即互为Master-Slave(只有一个Master提供写操作),可以实现数据库服务器的热备,但是一个Master宕机后不能实现动态切换.而Keepalived通过虚拟IP,实现了双主对外的统一接口以及自动检查.失败切换机制.联合使用,可以实现MySQL数据库的高可用方案. 实验环境:OS:centos 6.x x86_64系统MySQL版本: :mysql 5.6.22   64 位A: master :192.168.79.3 3306B: slave :192.168.

keepalived + mysql 双主多从

keepalived 的热备可以看文档:keepalived的配置使用 mysql 的主从 的文档讲解:mysql 主从复制 keepalived的双主,就是主从的一种, 大家看一眼就懂了,为了省力, 下面的双主配置我直接网上找了一篇,在下面,大家随便看下 keepalived的双子多从的整体结构 如下图: 1.配置两台Mysql主主同步[[email protected] ~]# yum install mysql-server mysql -y [[email protected] ~]#

keepalived+mysql双主高可用配置

具体架构图如下 两个节点一主一从(从库上面也可以再挂一个从库),或者是双主,再利用keepalived在出现容灾时进行高可用切换. Keepalived原理说明: 其实这个原理可以很简单的用一个故事说明. 以前有一个帮派,像其他所有的帮派一样,有老大.这个老大活的也很憋屈,不仅自己做所有的活,还要隔一段时间就要告诉所有小弟我是老大,你们老实点.所有的小弟也是很乖的,只要老大不死,他们肯定不会抢夺老大的地位,但是一旦他们都收不到老大的通信的话,他们就确信老大死了,他们就开始抢夺老大的地位了.不过他

Keepalived+Mysql(2主2从架构)

利用keepalived构建高可用MySQL-HA,保证两台MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换. 使用MySQL双master+keepalived是一种非常好的解决方案,在MySQL-HA环境中,MySQL互为主从关系,这样就保证了两台MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换. 下面,我把即将上线的

mysql主主复制及keepalived高可用群集

mysql主主复制及keepalived高可用 Keepalived+mysql双主来实现MySQL-HA,我们必须保证两台MySQL数据库的数据完全一样,基本思路是两台MySQL互为主从关系(双主),通过Keepalived配置虚拟IP,实现当其中的一台MySQL数据库宕机后,应用能够自动切换到另外一台MySQL数据库,保证系统的高可用. 实验基本拓扑: 主主同步就是两台机器互为主从的关系,在任何一台机器上写入都会同步. 在master1 mysql主配置文件中添加下面参数(/etc/my.c

MySQL双主+keepalived实现高可用

mysql+keepalived实现高可用+主主复制模式 为了解决mysql的单点故障问题,衍生出很多mysql的高可用方案: keepalived+双主.MHA.PXC.MMM.Hearbeat+DRBD等,比较常用的一般是keepalived+双主,MHA和PXC 在此搭建实验环境,实现keepalived+mysql双主模式. 实验思路: 两台MySQL互为主从关系(双主),通过keepalived配置虚拟vip,实现当其中的一台MySQL数据库宕机后,应用能自动切换到另外一台MySQL数