linux 数据库 双活,MySQL 高可用之 keepalived+Mysql 双主双活。

本文详细介绍了如何在两台CentOS 7.5服务器上配置MySQL双主同步,并使用Keepalived实现高可用性。首先,安装并启动MySQL,设置server-id、binlog选项,然后在两台服务器上进行权限配置和主从切换。接着,通过`show slave status`验证同步状态。最后,安装并配置Keepalived以实现VIP漂移。在故障发生时,系统能够自动切换并保持服务连续性。
摘要由CSDN通过智能技术生成

环境描述:

[root@MySQL-M ~]# cat /etc/redhat-release

CentOS Linux release 7.5.1804 (Core)

[root@MySQL-S ~]# 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 安装:

[root@MySQL-M ~]# yum install mysql-server mysql -y

[root@MySQL-M ~]# systemctl mysqld start

编辑 /etc/my.cnf 配置文件,增加以下内容。设置 server-id。

[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 #忽略所有复制产生的错误 。

查看 主服务器 log bin 日志和pos 值位置。

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 | 6383 | | | |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

MySQL-M 配置如下:

主服务器授权:

mysql> grant replication client,replication slave on . to repl@'172.20.2.%' identified by 'repl';

Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

第二台主服务器授权:

主服务器授权:

mysql> grant replication client,replication slave on . to repl@'172.20.2.%' identified by 'repl';

Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

第二台主服务器操作:

mysql> change master to

-> master_host='192.168.10.10',

-> master_user='repl',

-> master_password='repl',

-> master_log_file='mysql-bin.000001',

-> master_log_pos=1559;

Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> show slave status;

启动同步功能:

mysql> start slave;

##############################################################

mysql> show slave status;

+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+--------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+--------------

mysql> show slave status \G

1. row

Slave_IO_State: Waiting for master to send event

Master_Host: 172.20.2.46

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 1559

Relay_Log_File: mysql-relay.000003

Relay_Log_Pos: 320

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

主服务器操作:

mysql> change master to

-> master_host='192.168.10.20',

-> master_user='repl',

-> master_password='repl',

-> master_log_file='mysql-bin.000001',

-> master_log_pos=1559;

Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> show slave status;

###########################################################

启动同步功能:

mysql> start slave;

########################################################################

测试:

主服务器 10.10:

mysql> create database test;

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

| test |

| wangbo |

+--------------------+

6 rows in set (0.00 sec)

mysql> use test;

mysql> create table t1 ( id int, age int );

mysql> insert into t1 values (1,10),(2,20),(3,30);

mysql> select * from t1;

+------+------+

| id | age |

+------+------+

| 1 | 20 |

| 2 | 30 |

| 3 | 30 |

+------+------+

6 rows in set (0.00 sec)

###########################################################

从库插入:

mysql> use test;

mysql> insert into t1 values (4,40),(5,50),(6,60);

可以看到已经成功同步过去,同样在backup插入到user表数据,一样同步过去,双主就做成功了。

###########################################################

安装 keepalived :

[root@MySQL-M ~]#yum -y install keepalived

[root@master ~]# vi /usr/local/keepalived/mysql.sh

#!/bin/bash

pkill keepalived

[root@master ~]# chmod +x /usr/local/keepalived/mysql.sh

[root@master ~]# /etc/init.d/keepalived start

注:

#backup服务器只修改priority为90、nopreempt不设置、real_server设置本地IP。

#授权两台Mysql服务器允许root远程登录,用于在其他服务器登陆测试!

mysql> grant all on . to' root'@'192.168.10.%' identified by 'oldboy123';

mysql> flush privileges;

3、测试高可用性

1、通过Mysql客户端通过VIP连接,看是否连接成功。

2、停止master这台mysql服务,是否能正常切换过去,可通过ip addr命令来查看VIP在哪台服务器上。

3、可通过查看/var/log/messges日志,看出主备切换过程

4、master服务器故障恢复后,是否主动抢占资源,成为活动服务器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值