MySQL高可用群集
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL高可用群集相关的知识,希望对你有一定的参考价值。
mysql高可用群集
技能目标
- 熟悉MMM高可用架构
- 熟悉MHA高可用架构
- 学会配置MMM高可用
- 学会配置MHA高可用
关于MMM高可用架构说明
- mmm_mon:监控进程负责所有监控工作、决定和处理节点角色活动
- mmm_agent:运行在每个MySQL服务器上的代理进程完成监控的探针工作和执行简单的远端服务设置
- mmm_control:一个简单的脚本,提供管理mmm_mond的进程命令
- mysql-mmm的监管会提供多个虚拟IP(VIP),包括一个可写VIP,多个可读VIP,通过监管的管理绑定在可用MySQL之上,当某一台MySQL关闭时,监管会将VIP迁移至其他MySQL
实验环境
mysql-monitor 是监控服务器
主机 | 操作系统 | IP地址 | 只要软件 |
---|---|---|---|
mysql-master1 | Centos 7 x86_64 | 192.168.32.222 | MySQL-MMM |
mysql-master2 | Centos 7 x86_64 | 192.168.32.223 | MySQL-MMM |
mysql-slave1 | Centos 7 x86_64 | 192.168.32.224 | MySQL-MMM |
mysql-slave2 | Centos 7 x86_64 | 192.168.32.225 | MySQL-MMM |
mysql-monitor | Centos 7 x86_64 | 192.168.32.226 | MySQL-MMM |
master1,2 | Centos 7 x86_64 | VIP:192.168.32.221 | MySQL-MMM |
slave1 | Centos 7 x86_64 | VIP1:192.168.32.220 | MySQL-MMM |
slave2 | Centos 7 x86_64 | VIP1:192.168.32.199 | MySQL-MMM |
搭建主服务器master1
修改master1主配置文件
[[email protected] ~] vim /etc/my.cnf #删除从第一行开始到第九行删除添加下列语句
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1 #server-id每台数据库server值都是唯一的
log-error=/usr/local/mysql/data/mysql_error.log #开启错误日志
general_log=ON #开启访问日志
general_log_file=/usr/local/mysql/data/mysql_general.log
log_bin=mysql-bin #开启二进制日志
slow_query_log=ON #开启慢日志
slow_query_log_file=mysql_slow_query.log
long_query_time=5
log_slave_updates=true
sync_binlog=1 #同步
auto_increment_increment=2 #增量值
auto_increment_offset=1 #起点值
binlog-ignore-db=mysql,information_schema #关闭mysql,information_schema 这两个二进制日志
[[email protected] ~]# systemctl restart mysqld.service #重启数据库
[[email protected] data]# netstat -anpt | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 3074/mysqld
#查看端口有没开启
master2、slave1、slave2复制master1的配置文件把server-id改掉改成不一样的
做主从同步 主主复制,主从同步
我在这是一台一台配的有一条命令可以节省改配置文件的时间
scp /etc/my.cnf [email protected]:/etc/
配置主主复制-两台主服务器相互复制
在m1上为m2授予从的权限,在m2上也要为m1授予从的权限
#先用show master status 查看两台主服务器的二进制日志文件和偏移量
#master1
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000004 | 154 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
#master2
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000004 | 154 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
先把master2授权给master1
mysql> grant replication slave on *.* to ‘replication‘@‘192.168.32.%‘ identified by ‘123456‘;
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> change master to master_host=‘192.168.32.184‘,master_user=‘replication‘,master_passwordd=‘123456‘,master_log_file=‘mysql_bin.000002‘,master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
### 开启同步功能 #slave代表开启同步不是代表从服务器,我上面设置的只是个名称与这边的意思不同
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
先把master1授权给master2
mysql> grant replication slave on *.* to ‘replication‘@‘192.168.32.%‘ identified by ‘123456‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host=‘192.168.32.151‘,master_user=‘replication‘,master_password=‘123456‘,master_log__file=‘mysql_bin.000004‘,master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
#开启同步功能
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
#查看IO线程和SQL线程是否都已经开启是否显示为yes
mysql>show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.32.151
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000011
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 625
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_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: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 111eb6e9-78dd-11e8-9337-000c2940c252
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
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:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
如果只开启了SQL线程而IO线程没有开启
那么检查防火墙有无关闭 增强性安全模块有没有关闭
再执行下列语句
mysql>stop slave; #先停止slave
mysql>reset slave; #刷新slave同步功能
mysql>start slave; #最后再开启然后查看
创建一个数据库看是否能同步
- 主1
MariaDB [(none)]> create database info;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| info |
| mysql |
| performance_schema |
| test |
+--------------------+
- 主2
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| info |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
- 从1
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| info |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
- 从2
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| info |
| info1 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
在从2 上创建数据库看主1 主2 从1 会不会同步不会同步就说明从服务器只会同步主服务器、主服务器不会同步从服务器
配置从服务器
两个从服务器要能同时能同步两个主服务器
安装MMM在所有服务器上安装注意,epel源要配置好
先配置epel源
配置ALI云源,然后安装epel-release源。
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all && yum makecache
yum -y install epel-release
yum -y install mysql-mmm*
修改配置文件 //所有主机上都要配置,直接复制多份
[[email protected] etc]# cd /etc/mysql-mmm/
[[email protected] mysql-mmm]# vim mmm_common.conf
active_master_role writer
<host default>
cluster_interface ens33 #修改网卡默认是eth0
pid_path /run/mysql-mmm-agent.pid
bin_path /usr/libexec/mysql-mmm/
replication_user replicant #登陆用户是replicat
replication_password 123456 #复制同步登陆密码
agent_user mmm_agent
agent_password 123456 #登陆代理密码
</host>
<host db1>
ip 192.168.32.222 ——> 主服务器1的IP地址
mode master ——>主服务器
peer db2
</host>
<host db2>
ip 192.168.32.223 ——>主服务器2的IP地址
mode master ——>主服务器
peer db1
</host>
<host db3>
ip 192.168.32.224 ——>从服务器1的IP地址
mode slave ——>从服务器
</host>
<host db4>
ip 192.168.32.225 ——>从服务器2的IP地址
mode slave ——>从服务器
</host>
<role writer> #权限可写
hosts db1, db2
ips 192.168.32.221 ——>主服务器1、2的虚拟IP
mode exclusive
</role>
<role reader> #权限可读
hosts db3, db4 #主机名
ips 192.168.32.220 192.168.32.199 #从服务器1、2的虚拟IP
mode balanced
</role>
上传配置文件
scp mmm_common.conf [email protected]:/etc/mysql-mmm/
scp mmm_common.conf [email protected]:/etc/mysql-mmm/
scp mmm_common.conf [email protected]:/etc/mysql-mmm/
------------------在monitor(监听)服务器上配置----
#修改IP两主两从的IP地址在ping_ips后面,auto_set_online 自动设置在线时长为10秒
<monitor>
ip 127.0.0.1
pid_path /run/mysql-mmm-monitor.pid
bin_path /usr/libexec/mysql-mmm
status_path /var/lib/mysql-mmm/mmm_mond.status
ping_ips 192.168.32.222,192.168.32.223,192.168.32.224,192.168.32.225
auto_set_online 10
cd /etc/mysql-mmm/ //改密码
vi mmm_mon.conf
<host default>
monitor_user mmm_monitor
monitor_password 123456
</host>
--------------在所有数据库上为mmm_moniter和mmm_agent授权-----
#为mmm_agent授权
MariaDB [(none)]> grant super, replication client, process on *.* to ‘mmm_agent‘@‘192.168.32.%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.01 sec)
#为mmm_moniter授权
MariaDB [(none)]> grant replication client on *.* to ‘mmm_monitor‘@‘192.168.32.%‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.01 sec)
# 刷新
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
---------------修改所有数据库的mmm_agent.conf----
vi /etc/mysql-mmm/mmm_agent.conf
this db1 //根据规划进行逐一调整
include mmm_common.conf
# The ‘this‘ variable refers to this server. Proper operation requires
# that ‘this‘ server (db1 by default), as well as all other servers, have the
# proper IP addresses set in mmm_common.conf.
this db4
--------------在所有数据库服务器上启动mysql-mmm-agent---
#开启服务
systemctl start mysql-mmm-agent.service
#设置开机自启动
systemctl enable mysql-mmm-agent.service
#用 mmm_control show 查看各个节点
[[email protected] mysql-mmm]# mmm_control show
db1(192.168.32.222) master/ONLINE. Roles: writer(192.168.32.221)
db2(192.168.32.223) master/ONLINE. Roles:
db3(192.168.32.224) slave/ONLINE. Roles: reader(192.168.32.199)
db4(192.168.32.225) slave/ONLINE. Roles: reader(192.168.32.220)
故障测试
停止db1 确认 虚拟地址 221 是否移动到 db2 上。注意:主不会抢占
[[email protected] mysql-mmm]# systemctl stop mariadb.service
[[email protected] mysql-mmm]# mmm_control show
db1(192.168.32.222) master/HARD_OFFLINE. Roles:
db2(192.168.32.223) master/ONLINE. Roles: writer(192.168.32.221)
db3(192.168.32.224) slave/ONLINE. Roles: reader(192.168.32.199)
db4(192.168.32.225) slave/ONLINE. Roles: reader(192.168.32.220)
再开一台mysql服务器作为客户端
在db1服务器上为最后一台客户端服务器地址授权登录
MariaDB [(none)]> grant all on *.* to ‘testdba‘@‘192.168.32.222‘ identified by ‘123456‘;
MariaDB [(none)]> flush privileges;
#客户端登陆虚拟IP
[[email protected] ~]# mysql -utestdba -p -h 192.168.32.221
Enter password: #设置代理授权是的登陆密码
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 1358
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.
#在客户端创建数据库,测试同步
MariaDB [(none)]> create database info; #在客户端创建的
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| info |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
#在服务端查询
MariaDB [(none)]> show databases; #db1
+--------------------+
| Database |
+--------------------+
| information_schema |
| info |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> show databases; #db2
+--------------------+
| Database |
+--------------------+
| information_schema |
| info |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> show databases; #db3
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| info |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> show databases; #db4
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| info |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
#在客户端创建就是在主服务器上创建从服务器同步
---验证将从服务器停止一台,另一台将接管两个虚拟IP,以保证业务不停止-----
[[email protected] mysql-mmm]# mmm_control show
db1(192.168.32.222) master/ONLINE. Roles:
db2(192.168.32.223) master/ONLINE. Roles: writer(192.168.32.221)
db3(192.168.32.224) slave/ONLINE. Roles: reader(192.168.32.199), reader(192.168.32.220)
db4(192.168.32.225) slave/HARD_OFFLINE. Roles:
==上述同步做完了存在一个问题在做主从同步的时候同步的是主服务器上面的真实IP但是在down掉同步的那个主服务器的时候从服务器将不会在同步所以我们再设置主从同步的时候IP地址用他们设置的VIP(虚拟IP)==
以上是关于MySQL高可用群集的主要内容,如果未能解决你的问题,请参考以下文章