目录

1、环境设置
2、配置my.cnf配置文件
3、授权允许同步
4、开启slave同步
5、验证互为主从
6、安装keepalived
7、配置keepalived
8、验证

1、环境设置

10.0.0.132 master1
10.0.0.134 master2
setenforce 0
systemctl stop firewalld

2、配置my.cnf配置文件

master1上

vim /etc/my.cnf[client]socket=/usr/local/mysql/mysql.sock[mysqld]basedir=/usr/local/mysqldatadir=/usr/local/mysql/datauser=mysqlpid-file=/usr/local/mysql/data/mysqld.pidlog-error=/usr/local/mysql/data/mysql.errsocket=/usr/local/mysql/mysql.sockport=3306server_id=1log-bin=mysql-bingtid-mode=onenforce-gtid-consistency=1log_slave_updates = 1binlog-format=mixedauto-increment-increment=2          auto-increment-offset=1
systemctl restart mysqld

master2上

vim /etc/my.cnf
[client]socket=/usr/local/mysql/mysql.sock[mysqld]basedir=/usr/local/mysqldatadir=/usr/local/mysql/datauser=mysqlpid-file=/usr/local/mysql/data/mysqld.pidlog-error=/usr/local/mysql/data/mysql.errsocket=/usr/local/mysql/mysql.sockport=3306server_id=2log-bin=mysql-bingtid-mode=onenforce-gtid-consistency=1log_slave_updates = 1binlog-format=mixedauto-increment-increment=2  auto-increment-offset=2
systemctl restart mysqld

3、授权允许同步

master1上

mysql -uroot -p
mysql> grant replication slave on *.* to rep@'10.0.0.%' identified by '123';

master2上

mysql -uroot -p
mysql> grant replication slave on *.* to rep@'10.0.0.%' identified by '123';

4、开启slave同步

master1上

mysql> change master to master_host='10.0.0.134',master_user='rep',master_password='123',master_port=3306,master_autoter_auto_position=1;
mysql> start slave;

master2上

mysql> change master to master_host='10.0.0.132',master_user='rep',master_password='123',master_port=3306,master_autoter_auto_position=1;
mysql> start slave;

5、验证互为主从

master1上

mysql> show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 10.0.0.134Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 154Relay_Log_File: master1-relay-bin.000002Relay_Log_Pos: 367Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 576Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 2Master_UUID: e59d0925-be6a-11e8-9cab-000c29b63badMaster_Info_File: /usr/local/mysql/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 1Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)

master2上

mysql> show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 10.0.0.132Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 154Relay_Log_File: master2-relay-bin.000002Relay_Log_Pos: 367Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 576Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: ceb0ca3d-8366-11e8-ad2b-000c298b7c9aMaster_Info_File: /usr/local/mysql/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 1Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)

6、安装keepalived

master1上

yum -y install keepalived

master2上

yum -y install keepalived

7、配置keepalived

master1上

vim /etc/keepalived/keepalived.conf

global_defs {router_id master1
}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 100advert_int 1nopreemptauthentication {auth_type PASSauth_pass 1111}   virtual_ipaddress {10.0.0.100 }
}   virtual_server 192.168.1.100 3306 {delay_loop 6lb_algo rr lb_kind DR persistence_timeout 50protocol TCPreal_server 10.0.0.132 3306 {weight 1notify_down /etc/keepalived/bin/mysql.sh                  TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3connect_port 3306}}
}
systemctl enable keepalived
systemctl start keepalived
mkdir /etc/keepalived/bin

vim /etc/keepalived/bin/mysql.sh

#!/bin/bash
pkill keepalived
/sbin/ifdown ens33 && /sbin/ifup ens33
chmod +x /etc/keepalived/bin/mysql.sh

master2上

vim /etc/keepalived/keepalived.conf

global_defs {router_id master2
}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 51priority 50advert_int 1nopreemptauthentication {auth_type PASSauth_pass 1111}   virtual_ipaddress {10.0.0.100 }
}   virtual_server 192.168.1.100 3306 {delay_loop 6lb_algo rr lb_kind DR persistence_timeout 50protocol TCPreal_server 10.0.0.134 3306 {weight 1notify_down /etc/keepalived/bin/mysql.sh                  TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3connect_port 3306}}
}
systemctl enable keepalived
systemctl start keepalived
mkdir /etc/keepalived/bin

vim /etc/keepalived/bin/mysql.sh

#!/bin/bash
pkill keepalived
/sbin/ifdown ens33 && /sbin/ifup ens33
chmod +x /etc/keepalived/bin/mysql.sh

8、最终验证(keepalived双主)

验证一

在master1上

ip addr show ens33可以看到我们设置vip

在master2上

ip addr show ens33没有vip出现

验证二

在两台数据库上授权允许remote用户允许远程登录

master1上

mysql -uroot -p
mysql> grant all on *.* to remote@'%' identified by '123';

master2上

mysql -uroot -p
mysql> grant all on *.* to remote@'%' identified by '123';

另寻找一台MySQL数据库使用vip远程访问数据库集群

查看server_id,图示为1,所以说明我们当前登录到了master1上;也证明了master1如今是active状态的,而master2是备份状态

我们创建创建一个数据库试试

回到master1上

再到master2上查看

这就说明我们的主从复制也是没有问题的

验证三:验证keepalived双主集群的可用性

首先停掉master1上mysqld服务

systemctl stop mysqld

再次查看master1的ens33网卡,vip已经消失

查看master2的ens33网卡,出现vip

再次通过vip远程登录,依然可以登录,而且可以看到server_id变成了2。

转载于:https://blog.51cto.com/13434336/2178982

MySQL数据库Keepalived双主相关推荐

  1. linux的mysql主主_Linux下指定mysql数据库数据配置主主同步的实例

    一. 概念:① 数据库同步  (主从同步 --- 主数据库写的同时 往从服务器写数据) ② 数据库同步  (主主同步 --- 两台数据库服务器互相写数据) 二. 举例 主主数据库同步服务器配置 数据库 ...

  2. mysql基础14(关于mysql数据库在没有主键情况下去除重复数据办法)

    关于mysql数据库在没有主键情况下去除重复数据办法 约定 表名:mat 根据 cat 字段去重 新增加主键为 id 步骤 1.为mat新增一列自增主键 alter table mat add col ...

  3. 安装 mysql 数据库, 并做 主 从(二)

    2019独角兽企业重金招聘Python工程师标准>>> 按照第一篇安装 mysql 数据库, 并做 主 从(一)  安装好两台mysql;做好这些后,我们来做主从吧! ip serv ...

  4. mysql keepalived双主双活_mysql高可用架构方案之中的一个(keepalived+主主双活)

    Mysql双主双活+keepalived实现高可用 文件夹 1.前言... 4 2.方案... 4 2.1.环境及软件... 4 2.2.IP规划... 4 2.3.架构图... 4 3.安装设置MY ...

  5. mysql+keepalived 双主热备高可用

    理论介绍: 我们通常说的双机热备是指两台机器都在运行,但并不是两台机器都同时在提供服务.当提供服务的一台出现故障的时候,另外一台会马上自动接管并且提供服务,而且切换的时间非常短.MySQL双主复制,即 ...

  6. mysql数据库集群 主主复制 原理_MySql搭建集群 之 主主复制(双主代从)MYSQL数据库...

    作者:VEPHP   时间 2017-09-27 <MySql搭建集群 之 主主复制(双主代从)MYSQL数据库>要点: 本文介绍了MySql搭建集群 之 主主复制(双主代从)MYSQL数 ...

  7. mysql keepalived双主双活_Keepalived单网络双活双主配置模式

    前言: 一般这种模式不需要相对复杂的配置,相对单网络单主模式,多了一个主可用模式.主要实现单网络双主故障漂移模式. 1.架构示意图: 多播IP是:224.0.0.111. NAT网络可根据自己的实际情 ...

  8. MyCat学习:使用MySQL搭建主从复制(双主双从模式)

    首先,上一篇我们建立了一主一丛模式的MySQL读写分离 点击这里 那么接下来,我们搭建双主双从模式的读写分离,并且使用MyCat登录访问 原理:master1,master2,slave1,slave ...

  9. Mysql 集群双主双从安装使用详细讲解

    文章目录 下载Mysql 安装单机Mysql 配置Mysql集群 双Master配置 master1配置 master2配置 配置说明 双Slave配置 Slave1配置 Slave2配置 双 Mas ...

最新文章

  1. 【Scala-spark.mlib】本地矩阵乘法计算效率比较(稠密稀疏哪家强?)
  2. CSS学习05之结构伪类选择器
  3. 服务器错误重启mysql错误信息,mysql开启和使用事件、与服务器重启mysql错误
  4. redis实现令牌桶算法思路
  5. MATLAB插值问题
  6. 电脑网页打不开但qq能上解决方法
  7. android iOS 都精通,Android IOS开发要理解何为面向对象的思维
  8. 编译安装-httpd-2.2.15.tar.gz
  9. samsung-smart app 开发
  10. 京东-黑客马拉松大赛复盘
  11. ROS中机械手臂的运动规划
  12. vue中img本地图片地址的具体使用
  13. 长隆大马戏机器人_腾讯长隆打造智能主题酒店 小Q机器人全面部署智慧旅游
  14. 荣耀开发者关怀月邂逅1024程序员节,请开发者喝咖啡
  15. 反相器的Cadence仿真
  16. Linux命令——系统管理
  17. 手机话费充值接口,实现话费充值功能接入
  18. 艾兰岛编辑器-设置旋转效果
  19. php编码规范(未完待续)
  20. 第06课:生活中的中介模式——找房子问中介

热门文章

  1. MyBatis-09MyBatis注解方式之@Update/@Delete
  2. JavaScript-语法、关键保留字及变量
  3. 修改centos等linux的hostname-永久生效
  4. 紧致差分的matlab程序,对流占优扩散方程的一种新C—N 紧致差分格式
  5. python3安装mysql模块_Python3.6 下 安装MySql
  6. 在云服务器上持续运行springboot项目
  7. 【TypeScript】parseInt 函数将字符串转换为整数
  8. 2021-03-29 自动控制-滑模控制 Simulink仿真
  9. Python 深度学习目标检测评价指标 :mAP、Precision、Recall、AP、IOU等
  10. 老旧计算机升级云桌面,哇哦!PC机房轻松升级云课堂 ,PC利旧方案看我的!