目录
一、MySQL简介
MySQL是一个功能强大、性能高效、可扩展性强的关系型数据库管理系统[RDBMS],使用结构化查询语言(SQL)来管理或操作数据库。无论是小型项目还是大型企业级应用,MySQL都能提供可靠的数据存储和管理解决方案。
特点:
-
开源免费:给用户免费使用,可以根据需求对其进行修改和定制,并有丰富的资源和支持。
-
性能高效:具有快速的数据存储和检索能力,支持多种存储引擎,可以根据不同的应用场景选择合适的存储引擎来优化性能。
-
可扩展性强:可以轻松应对不断增长的数据量和用户需求;支持主从复制、集群等技术,实现高可用性和负载均衡。
-
跨平台性:在多种操作系统上运行,用户可以根据自己的需求选择合适的操作系统,不用考虑数据库的兼容性问题。
-
安全性:提供多种安全机制,保护数据的安全性和完整性;对用户进行精细的权限管理,控制用户对数据库的访问权限。
应用场景:
-
网站和应用程序:用于存储用户信息、文章内容、商品数据等;支持高并发访问,满足大规模用户的需求。
-
企业级应用:应用在企业资源规划、客户管理等系统;可以与其他企业级软件集成,提供可靠的数据存储和管理解决方案。
-
数据分析:可以作为数据源,通过SQL查询和数据分析工具,对数据进行深入分析和挖掘;支持数据仓库和数据集市的建设,为企业决策提供支持。
-
移动应用:适用于移动应用的后端数据库,存储用户数据、应用配置等信息;开发框架集成,提供便捷的数据存储和访问接口。
二、MySQL部署方法
链接:百度网盘 请输入提取码 提取码:r79k
####安装依赖性
[root@mysql-node1 ~]# yum install cmake gcc-c++ openssl-devel -y
[root@mysql-node1 ~]# yum install ncurses-devel.x86_64 rpcgen.x86_64 -y
[root@mysql-node1 ~]# yum install libtirpc-devel-0.2.4-0.16.el7.x86_64.rpm -y
###下载并解压资源包
[root@mysql-node1 ~]# tar zxf mysql-boost-5.7.44.tar.gz
[root@mysql-node1 ~]# cd mysql-5.7.44/
####源码编译安装mysql
[root@mysql-node1 mysql-5.7.44]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #指定安装路径
-DMYSQL_DATADIR=/data/mysql \ #指定数据目录
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \ #指定套接字文件
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #指定启用INNODB存储引擎,默认用myisam
-DWITH_EXTRA_CHARSETS=all \ #扩展字符集
-DDEFAULT_CHARSET=utf8mb4 \ #指定默认字符集
-DDEFAULT_COLLATION=utf8mb4_unicode_ci \ #指定默认校验字符集
-DWITH_BOOST=/root/mysql-5.7.44/boost/boost_1_59_0/ #指定c++库依赖
[root@mysql_1 mysql-5.7.44]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_BOOST=/root/mysql-5.7.44/boost/boost_1_59_0/
####生成信息拷贝到系统存放的位置
#-j8 这里虚拟机用的8核
[root@mysql_1 mysql-5.7.44]# make -j8 && make install
【mysql-node1 、mysql-node2、mysql-node3 做相同配置】
[root@mysql-node1 ~]# cd /usr/local/mysql
[root@mysql-node1 mysql]# useradd -s /sbin/nologin -M mysql
[root@mysql-node1 mysql]# mkdir -p /data/mysql
#用户组
[root@mysql-node1 mysql]# chown mysql.mysql -R /data/mysql
[root@mysql-node1 mysql]# cd support-files/
[root@mysql-node1 support-files]# cp mysql.server /etc/init.d/mysqld
#修改配置文件
[root@mysql-node1 support-files]# vim /etc/my.cnf
[root@mysql-node1 support-files]# vim ~/.bash_profile
[root@mysql-node1 support-files]# source ~/.bash_profile
[root@mysql-node1 support-files]# cd
#如果初始化有问题,就 rm -rf /usr/local/mysql/*,再重新初始化
[root@mysql-node1 ~]# mysqld --user mysql --initialize
....
2024-08-22T11:40:55.479816Z 1 [Note] A temporary password is generated for root@localhost: lxPjiz1jVg?w
....
#初始密码备份
[root@mysql-node1 ~]# echo lxPjiz1jVg?w > passwd
#启动mysqld
[root@mysql-node1 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql-node1.err'.
SUCCESS!
[root@mysql-node1 ~]# yum install chkconfig -y
[root@mysql-node1 ~]# chkconfig mysqld on
[root@mysql-node1 ~]# chkconfig --list
#安全初始化
[root@mysql-node1 ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: #初始密码--->lxPjiz1jVg?w
The existing password for the user account root has expired. Please set a new password.
New password: #新密码---> redhat
Re-enter new password: #确认密码---> redhat
...
#设置VALIDATE PASSWORD插件
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: no
Using existing password for root.
#修改root密码
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no
#删除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
#删除访问的数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
#刷新表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
[root@mysql-node1 ~]# mysql -uroot -predhat
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
#传输配置文件,另外两台主机做相同操作
[root@mysql-node1 local]# rsync -al -r mysql root@172.25.254.60:/usr/local/
root@172.25.254.60's password:
[root@mysql-node1 local]# rsync -al -r mysql root@172.25.254.70:/usr/local/
root@172.25.254.60's password:
三、Mysql组从复制
172.25.254.50为主,172.25.254.60/70为从
3.1 master配置
[root@mysql-node1 ~]# vim /etc/my.cnf
[root@mysql-node1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
log-bin=mysql-bin
server-id=50
[root@mysql-node1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@mysql-node1 ~]# mysql -uroot -p -e "SELECT @@server_id"
Enter password:
+-------------+
| @@server_id |
+-------------+
| 50 |
#进入数据库配置用户权限
[root@mysql-node1 ~]# mysql -uroot -predhat
...
#生成slave端认证用的用户
mysql> CREATE USER 'chuling'@'%' IDENTIFIED BY 'redhat';
Query OK, 0 rows affected (0.00 sec)
#对用户进行授权
mysql> GRANT REPLICATION SLAVE ON *.* TO chuling@'%';
Query OK, 0 rows affected (0.00 sec)
#查看master状态
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 601 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@mysql-node1 ~]# cd /data/mysql
#查看二进制文件日志
[root@mysql-node1 ~]# mysqlbinlog mysql-bin.000001 -vv
3.2 slave配置
[root@mysql-node2 ~]# vim /etc/my.cnf
[root@mysql-node2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=60
[root@mysql-node2 ~]# /etc/init.d/mysqld restart
[root@mysql-node2 ~]# mysql -uroot -predhat
...
mysql> CHANGE MASTER TO
MASTER_HOST='172.25.254.50',MASTER_USER='chuling',MASTER_PASSWORD='redhat',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=601;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
#开启服务
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
#查看服务状态
mysql> SHOW SLAVE STATUS\G;
测试:
【mysql-node1】
#创建库
mysql> CREATE DATABASE Chuling;
Query OK, 1 row affected (0.01 sec)
#创建表
mysql> CREATE TABLE Chuling.userlist ( username varchar(20) not null, password varchar(50) not null );
Query OK, 0 rows affected (0.00 sec)
#插入数据
mysql> INSERT INTO Chuling.userlist VALUE ('chuling','redhat');
Query OK, 1 row affected (0.02 sec)
#查看表信息
mysql> SELECT * FROM Chuling.userlist;
+----------+----------+
| username | password |
+----------+----------+
| chuling | redhat |
+----------+----------+
1 row in set (0.00 sec)
【mysql-node2】
mysql> SELECT * FROM Chuling.userlist;
+----------+----------+
| username | password |
+----------+----------+
| chuling | redhat |
+----------+----------+
1 row in set (0.00 sec)
#在node2上添加数据不能在node1上查看
3.3 数据添加slave2
#只演示不同部分
[root@mysql-node3 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=70
从master节点备份数据
[root@mysql-node1 ~]# mysqldump -uroot -p Chuling > Chuling.sql
Enter password:
[root@mysql-node1 ~]# scp Chuling.sql root@172.25.254.70:/mnt/
root@172.25.254.70's password:
Chuling.sql 100% 1954 649.0KB/s 00:00
锁表操作
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
利用备份的Chuling.sql在slave2拉平数据
[root@mysql-node3 ~]# cd /mnt/
[root@mysql-node3 mnt]# mysql -uroot -predhat -r "CREATE DATABASES Chuling";
[root@mysql-node3 mnt]# mysql -uroot -predhat -e "CREATE DATABASE Chuling;"
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-node3 mnt]# mysql -uroot -predhat Chuling < Chuling.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-node3 mnt]# mysql -uroot -predhat -e "SELECT * FROM Chuling.userlist;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------+----------+
| username | password |
+----------+----------+
| chuling | redhat |
+----------+----------+
配置slave功能
#查询日志pos
[root@mysql-node1 ~]# mysql -uroot -predhat -e "SHOW MASTER STATUS;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
[root@mysql-node3 mnt]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@mysql-node3 mnt]# mysql -uroot -predhat
mysql> change master to master_host='172.25.254.50',master_user='chuling',master_password='redhat',master_log_file='mysql-bin.000003',master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW SLAVE STATUS\G;
测试:
[root@mysql-node1 ~]# mysql -uroot -predhat -e "INSERT INTO Chuling.userlist VALUES('user1','123');"
[root@mysql-node2 ~]# mysql -uroot -predhat -e 'select * from Chuling.userlist;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------+----------+
| username | password |
+----------+----------+
| chuling | redhat |
| user1 | 123 |
+----------+----------+
[root@mysql-node3 ~]# mysql -uroot -predhat -e 'select * from Chuling.userlist;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------+----------+
| username | password |
+----------+----------+
| chuling | redhat |
| user1 | 123 |
+----------+----------+
3.4 延迟复制
slave节点
[root@mysql-node2 ~]# mysql -uroot -predhat
mysql> STOP SLAVE SQL_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_DELAY=60;
Query OK, 0 rows affected (0.00 sec)
mysql> START SLAVE SQL_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW SLAVE STATUS\G;
测试:在master节点插入数据,等待slave节点同步消息
[root@mysql-node1 ~]# mysql -uroot -predhat -e "INSERT INTO Chuling.userlist VALUES('user2','123');"
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql> select * from Chuling.userlist;
+----------+----------+
| username | password |
+----------+----------+
| chuling | redhat |
| user1 | 123 |
+----------+----------+
2 rows in set (0.00 sec)
mysql> select * from Chuling.userlist;
+----------+----------+
| username | password |
+----------+----------+
| chuling | redhat |
| user1 | 123 |
| user2 | 123 |
+----------+----------+
ma
3.5 慢查询
-
当执行SQL超过long_query_time参数设定的时间阈值(默认10s)
-
慢查询被记录在慢查询日志里
-
慢查询默认不开启
在master节点开启慢查询日志
mysql> SET GLOBAL slow_query_log=ON;
Query OK, 0 rows affected (0.00 sec)
mysql> SET long_query_time=4;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES like "long%";
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 4.000000 |
+-----------------+----------+
1 row in set (0.01 sec)
mysql> SHOW VARIABLES like "slow%";
+---------------------+----------------------------------+
| Variable_name | Value |
+---------------------+----------------------------------+
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /data/mysql/mysql-node1-slow.log |
+---------------------+----------------------------------+
3 rows in set (0.00 sec)
#查看慢查询日志
[root@mysql-node1 ~]# cat /data/mysql/mysql-node1-slow.log
/usr/local/mysql/bin/mysqld, Version: 5.7.44-log (Source distribution). started with:
Tcp port: 3306 Unix socket: /data/mysql/mysql.sock
Time Id Command Argument
测试:
3.6 Mysql并行复制
-
查看slave中的线程信息
-
默认情况下的slave中使用的sql单线程回放
-
在master中时多用户读写,使用sql单线程回放会造成主从延迟严重
在slave节点设置
[root@mysql-node2 ~]# vim /etc/my.cnf
[root@mysql-node2 ~]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL. SUCCESS!
[root@mysql-node2 ~]# mysql -uroot -predhat
[root@mysql-node2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=60
gtid_mode=ON
enforce-gtid-consistency=ON
slave-parallel-type=LOGICAL_CLOCK #基于组提交,
slave-parallel-workers=16 #开启线程数量
master_info_repository=TABLE #master信息在表中记录,默认记录在/data/mysql//master.info
relay_log_info_repository=TABLE #回放日志信息在表中记录,默认记录在/data/mysql/relay-log.info
relay_log_recovery=ON #日志回放恢复功能开启
测试:
3.7 原理刨析
三个线程
-
实际上主从同步的原理就是基于 binlog 进行数据同步的。在主从复制过程中,会基于3个线程来操作,一个主库线程,两个从库线程。
-
二进制日志转储线程(Binlog dump thread)是一个主库线程。当从库线程连接的时候, 主库可以将二进制日志发送给从库,当主库读取事(Event)的时候,会在 Binlog 上加锁,读取完成之后,再将锁释放掉。
-
从库 I/O 线程会连接到主库,向主库发送请求更新 Binlog。这时从库的 I/O 线程就可以读取到主库的二进制日志转储线程发送的 Binlog 更新部分,并且拷贝到本地的中继日志 (Relay log)。
-
从库 SQL 线程会读取从库中的中继日志,并且执行日志中的事件,将从库中的数据与主库保持同步。
复制步骤
-
Master将写操作记录到二进制日志(binlog)。
-
Slave将Master的binary log events拷贝到它的中继日志(relay log);
-
Slave重做中继日志中的事件,将改变应用到自己的数据库中。 MySQL复制是异步的且串行化的,而且重启后从接入点开始复制。
具体操作
-
slaves端中设置了master端的ip,用户,日志,和日志Position,通过这些信息取得master的认证及信息。
-
master端在设定好binlog启动后会开启binlog dump的线程。
-
master端的binlog dump把二进制的更新发送到slave端的
-
slave端开启两个线程,一个是I/O线程,一个是sql线程,i/o线程用于接收master端的二进制日志,此线程会在本地打开relaylog中继日志,并且保存到本地磁盘,sql线程读取本地relog中继日志进行回放
-
多个slave:当读取的而操作远远高与写操作时。我们采用一主多从架构数据库外层接入负载均衡层并搭配高可用机制
3.8 架构缺陷
master更新完成后直接发送二进制日志到slave,但是slaves是否真正保存了数据master端不会检测master端直接保存二进制日志到磁盘 当master端到slave端的网络出现问题时或者master端直接挂掉,二进制日志可能根本没有到达slavemaster出现问题slave端接管master,这个过程中造成数据丢失。
四、半同步模式
4.1 半同步模式原理
-
用户线程写入完成后master中的dump会把日志推送到slave端
-
slave中的io线程接收后保存到relaylog中继日志
-
保存完成后slave向master端返回ack
-
在未接受到slave的ack时master端时不做提交的,一直处于等待当收到ack后提交到存储引擎
-
在5.6版本中用到的时after_commit模式,after_commit模式时先提交在等待ack返回后输出ok
4.2 gid模式
[root@mysql-node1 ~]# mysqlbinlog -vv /data/mysql/mysql-bin.000003
在master和slave端开启gtid模式
[root@mysql-node1 ~]# vim /etc/my.cnf
[root@mysql-node1 ~]# /etc/init.d/mysqld restart
Shutting down MySQL........... SUCCESS!
Starting MySQL. SUCCESS!
[root@mysql-node1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=50
log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
symbolic-links=0
[root@mysql-node2 ~]# vim /etc/my.cnf
[root@mysql-node2 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@mysql-node2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=60
log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
symbolic-links=0
[root@mysql-node3 ~]# vim /etc/my.cnf
[root@mysql-node3 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@mysql-node3 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=70
log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
symbolic-links=0
停止slave节点
[root@mysql-node2 ~]# mysql -predhat
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
[root@mysql-node3 ~]# mysql -predhat
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
开启slave节点的gtid
mysql> change master to master_host='172.25.254.50',master_user='chuling',master_password='redhat',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave ststus\G;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ststus' at line 1
ERROR:
No query specified
mysql> show slave status\G;
4.3 启动半同步模式
在master节点配置半同步模式
[root@mysql-node1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=50
log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
rpl_semi_sync_master_enabled=1
symbolic-links=0
#安装半同步插件
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.00 sec)
#查看插件情况
mysql> SELECT PLUGIN_NAME,PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE '%semi%';
+----------------------+---------------+
| PLUGIN_NAME | PLUGIN_STATUS |
+----------------------+---------------+
| rpl_semi_sync_master | ACTIVE |
+----------------------+---------------+
1 row in set (0.00 sec)
#打开同步功能
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
Query OK, 0 rows affected (0.00 sec)
#查看半同步功能状态
mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';
+-------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_for_slave_count | 1 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.00 sec)
mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+--------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients | 0 |
| Rpl_semi_sync_master_net_avg_wait_time | 0 |
| Rpl_semi_sync_master_net_wait_time | 0 |
| Rpl_semi_sync_master_net_waits | 0 |
| Rpl_semi_sync_master_no_times | 0 |
| Rpl_semi_sync_master_no_tx | 0 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
+--------------------------------------------+-------+
14 rows in set (0.01 sec)
mysql> show plugins;
在slave端开启半同步功能
[root@mysql-node3 ~]# vim /etc/my.cnf
[root@mysql-node3 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=70
log-bin=mysql-bin
gtid_mode=ON
enforce-gtid-consistency=ON
rpl_semi_sync_master_enabled=1
[root@mysql-node3 ~]# mysql -uroot -predhat
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;
Query OK, 0 rows affected (0.00 sec)
#重启io线程,半同步生效
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.00 sec)
mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.00 sec)
测试:
在master端写入数据
五、Mysql高可用之组复制(MGR)
MGR(MySQL Group Replication)->高可用与高扩展的解决方案
MGR 是 MySQL 数据库的一种强大的高可用、高扩展解决方案,它提供了数据一致性、自动成员管理、多主复制等功能,适用于各种对数据库可用性和扩展性要求较高的场景。
5.1 实现mysql组复制
[root@mysql-node3 ~]# /etc/init.d/mysqld stop
[root@mysql-node3 ~]# ps aux | grep mysqld
root 2510 0.0 0.0 112808 964 pts/0 S+ 23:57 0:00 grep --color=auto mysqld
[root@mysql-node2 ~]# rm -rf /data/mysql/*
[root@mysql-node2 ~]# mysqld --user=mysql --initialize
[root@mysql-node3 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! Starting MySQL.Logging to '/data/mysql/mysql-node3.err'.
SUCCESS!
【mysql-node1】
[root@mysql-node1 ~]# rm -rf /data/mysql/*
[root@mysql-node1 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=50 #配置server唯一标识号
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY" #禁用指定存储引擎
gtid_mode=ON #启用全局事件标识
enforce_gtid_consistency=ON #强制gtid一致
master_info_repository=TABLE #复制事件数据到表中而不记录在数据目录中
relay_log_info_repository=TABLE
binlog_checksum=NONE #禁止对二进制日志校验
log_slave_updates=ON #打开数据库中继,
#当slave中sql线程读取日志后也会写入到自己的binlog中
log_bin=binlog #重新指定log名称
binlog_format=ROW #使用行日志格式
plugin_load_add='group_replication.so' #加载组复制插件
transaction_write_set_extraction=XXHASH64 #把每个事件编码为加密散列
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" #通知插件正式加入
#或创建的组名
#名称为uuid格式
group_replication_start_on_boot=off #在server启动时不自动启动组复制
group_replication_local_address="172.25.254.50:33061" #指定插件接受其他成员的信息端口
group_replication_group_seeds="172.25.254.50:33061,172.25.254.60:33061,172.25.254.70:33061" #本地地址允许访问成员列表
group_replication_ip_whitelist="172.25.254.0/24,127.0.0.1/8" #主机白名单
group_replication_bootstrap_group=off #不随系统自启而启动
group_replication_single_primary_mode=OFF #使用多主模式
group_replication_enforce_update_everywhere_checks=ON #组同步中有任何改变检测更新
group_replication_allow_local_disjoint_gtids_join=1 #放弃自己信息以master事件为主
[root@mysql-node1 ~]# mysqld --user=mysql --initialize
初始化密码:lxPjiz1jVg?w
[root@mysql-node1 ~]# /etc/init.d/mysqld start
[root@mysql-node1 ~]# mysql -uroot -plxPjiz1jVg?w -e "alter user root@localhost identified by 'redhat';"
配置sql
[root@mysql-node1 ~]# mysql -predhat
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'redhat';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='redhat' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected, 1 warning (2.19 sec)
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
| group_replication_applier | 1fb84241-61e5-11ef-a0ad-000c296b9a74 | mysql-node1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
1 row in set (0.00 sec)
将配置文件复制到mysql-node2和mysql-node3
[root@mysql-node1 ~]# scp /etc/my.cnf root@172.25.254.60:/etc/my.cnf
[root@mysql-node1 ~]# scp /etc/my.cnf root@172.25.254.70:/etc/my.cnf
#修改mysql—node2和mysl-node3中的配置,以node2为例
[root@mysql-node2 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=60
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address="172.25.254.60:33061"
group_replication_group_seeds="172.25.254.50:33061,172.25.254.60:33061,172.25.254.70:33061"
group_replication_ip_whitelist="172.25.254.0/24,127.0.0.1/8"
group_replication_bootstrap_group=off
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=1
[root@mysql-node2 ~]# mysqld --user=mysql --initialize
初始密码:TSVI@7jdigwb
[root@mysql-node2 ~]# /etc/init.d/mysqld start
[root@mysql-node2 ~]# mysql -uroot -pTSVI@7jdigwb -e "alter user root@localhost identified by 'redhat';"
在mysql-node2和mysql-node3上配置sql,相同配置
[root@mysql-node2 ~]# mysql -predhat
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'redhat';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='redhat' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected, 1 warning (2.19 sec)
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
| group_replication_applier | 1fb84241-61e5-11ef-a0ad-000c296b9a74 | mysql-node2 | 3306 | ONLINE |
+---------------------------+--------------------------------------+---------------------------+-------------+--------------+
1 row in set (0.00 sec)
本地解析(三台)
vim /etc/hosts
172.25.254.50 mysql-node1.chuling.org
172.25.254.60 mysql-node2.chuling.org
172.25.254.70 mysql-node3.chuling.org
测试:
#mysql-node1
[root@mysql-node1 ~]# mysql -predhat
mysql> CREATE DATABASE chuling;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE TABLE chuling.userlist(
-> username VARCHAR(10) PRIMARY KEY NOT NULL,
-> password VARCHAR(50) NOT NULL
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO chuling.userlist VALUES ('user1','111');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM chuling.userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 111 |
+----------+----------+
1 row in set (0.00 sec)
#mysql-node2
[root@mysql-node2 ~]# mysql -predhat
mysql> INSERT INTO chuling.userlist values ('user2','222');
Query OK, 1 row affected (0.00 sec)
mysql> select * from chuling.userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 111 |
| user2 | 222 |
+----------+----------+
2 rows in set (0.00 sec)
#mysql-node3
[root@mysql-node3 ~]# mysql -predhat
mysql> INSERT INTO chuling.userlist values ('user3','333');
Query OK, 1 row affected (0.00 sec)
mysql> select * from chuling.userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 111 |
| user2 | 222 |
| user3 | 333 |
+----------+----------+
3 rows in set (0.00 sec)
六、Mysql-router
MySQL Router是一个对应用程序透明的InnoDB Cluster连接路由服务,提供负载均衡、应用连接故障转移和客户端路由。利用路由器利用路由器的连接路由特性,用户可以编写应用程序来连接到路由器,并令路由器使用相应的路由策略来处理连接,使其连接到正确的MySQL数据库服务器。
部署Mysql route
172.25.254.50 --> mysql router
#安装mysql-router
[root@mysql-node1 ~]# rpm -ivh mysql-router-community-8.4.0-1.el7.x86_64.rpm
#配置mysql-router
[root@mysql-node1 ~]# vim /etc/mysqlrouter/mysqlrouter.conf
[routing:ro]
bind_address = 0.0.0.0
bind_port = 7001
destinations = 172.25.254.60:3306,172.25.254.70:3306
routing_strategy = round-robin
[routing:rw]
bind_address = 0.0.0.0
bind_port = 7002
destinations = 172.25.254.50:3306,172.25.254.60:3306
routing_strategy = first-available #谁最先响应访问谁
#重启服务
[root@mysql-node1 ~]# systemctl start mysqlrouter.service
测试:
#建立测试用户
[root@mysql-node2 ~]# mysql -predhat
mysql> CREATE USER chuling@'%' IDENTIFIED BY 'redhat';
mysql> GRANT ALL ON chuling.* TO redhat@'%';
[root@mysql-node3 ~]# mysql -predhat
mysql> CREATE USER chuling@'%' IDENTIFIED BY 'redhat';
mysql> GRANT ALL ON chuling.* TO redhat@'%';
#查看调度效果
[root@mysql-node2 & 3 ~]# watch -1 lsof -i :3306;
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 9879 mysql 22u IPv6 56697 0t0 TCP *:mysql (LISTEN)
[root@mysql-node1 ~]# mysql -uchuling -predhat -h 172.25.254.50 -P 7001
mysql> select @@server_id;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 60 |
+---------------+-------+
1 row in set (0.00 sec)
七、Mysql高可用MHA
7.1 MHA简介
什么是 MHA?
MHA(Master High Availability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件。
MHA 的出现就是解决MySQL 单点的问题。
MySQL故障切换过程中,MHA能做到0-30秒内自动完成故障切换操作。
MHA能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用。
MHA 的组成
MHA由两部分组成:MHAManager (管理节点) MHA Node (数据库节点),
MHA Manager 可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave 节点上。
MHA Manager 会定时探测集群中的 master 节点。
当 master 出现故障时,它可以自动将最新数据的 slave 提升为新的 master, 然后将所有其他的slave 重新指向新的 master。
MHA 的特点
自动故障切换过程中,MHA从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
使用半同步复制,可以大大降低数据丢失的风险,如果只有一个slave已经收到了最新的二进制日志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性
目前MHA支持一主多从架构,最少三台服务,即一主两从
故障切换备选主库的算法
1.一般判断从库的是从(position/GTID)判断优劣,数据有差异,最接近于master的slave,成为备选主。
2.数据一致的情况下,按照配置文件顺序,选择备选主库。
3.设定有权重(candidate_master=1),按照权重强制指定备选主。
(1)默认情况下如果一个slave落后master 100M的relay logs的话,即使有权重,也会失效。
(2)如果check_repl_delay=0的话,即使落后很多日志,也强制选择其为备选主。
MHA工作原理
-
目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群必须最少有3台数据库服务器,一主二从,即一台充当Master,台充当备用Master,另一台充当从库。
-
MHA Node 运行在每台 MySQL 服务器上
-
MHAManager 会定时探测集群中的master 节点
-
当master 出现故障时,它可以自动将最新数据的slave 提升为新的master
-
然后将所有其他的slave 重新指向新的master,VIP自动漂移到新的master。
-
整个故障转移过程对应用程序完全透明。
7.2 MHA部署实施
搭建一主两从架构
在master节点
[root@mysql-node1 ~]# /etc/init.d/mysqld stop
[root@mysql-node1 ~]# rm -fr /data/mysql/*
[root@mysql-node1 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
server-id=1
log-bin=mysql-bin
gtid_mode=ON
log_slave_updates=ON
enforce-gtid-consistency=ON
symbolic-links=0
[root@mysql-node1 ~]# mysqld --user mysql --initialize
[root@mysql-node1 ~]# /etc/init.d/mysqld start
[root@mysql-node1 ~]# mysql_secure_installation
[root@mysql-node1 ~]# mysql -p
mysql> alter user root@localhost identified by 'redhat';
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY 'redhat';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.02 sec)
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
Query OK, 0 rows affected (0.00 sec)
在slave和slave2节点
[root@mysql-node2 & 3 ~]# /etc/init.d/mysqld stop
[root@mysql-node2 & 3 ~]# rm -fr /data/mysql/*
[root@mysql-node2 & 3 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
server-id=1
log-bin=mysql-bin
gtid_mode=ON
log_slave_updates=ON
enforce-gtid-consistency=ON
symbolic-links=0
[root@mysql-node2 & 3 ~]# mysqld --user mysql --initialize
[root@mysql-node2 & 3 ~]# /etc/init.d/mysqld start
[root@mysql-node2 & 3 ~]# mysql -p
mysql> alter user root@localhost identified by 'redhat';
Query OK, 0 rows affected (0.01 sec)
mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.50', MASTER_USER='repl', MASTER_PASSWORD='redhat', MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.01 sec)
mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;
Query OK, 0 rows affected (0.00 sec)
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.01 sec)
安装MHA需要的软件
#在MHA中
[root@mysql-mha ~]# unzip MHA-7.zip
[root@mysql-mha MHA-7]# ls
mha4mysql-manager-0.58-0.el7.centos.noarch.rpm perl-Mail-Sender-0.8.23-
1.el7.noarch.rpm
mha4mysql-manager-0.58.tar.gz perl-Mail-Sendmail-0.79-
21.el7.noarch.rpm
mha4mysql-node-0.58-0.el7.centos.noarch.rpm perl-MIME-Lite-3.030-
1.el7.noarch.rpm
perl-Config-Tiny-2.14-7.el7.noarch.rpm perl-MIME-Types-1.38-
2.el7.noarch.rpm
perl-Email-Date-Format-1.002-15.el7.noarch.rpm perl-Net-Telnet-3.03-
19.el7.noarch.rpm
perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm perl-Parallel-ForkManager-1.18-
2.el7.noarch.rpm
[root@mysql-mha MHA-7]# yum install *.rpm -y
[root@mysql-mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.50:/mnt
[root@mysql-mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.60:/mnt
[root@mysql-mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.70:/mnt
#在sql-node中
[root@mysql-node10 ~]# yum install /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
[root@mysql-node20 ~]# yum install /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
[root@mysql-node30 ~]# yum install /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
配置MHA的管理环境
#生成配置文件
[root@mysql-mha ~]# mkdir /etc/masterha
[root@mysql-mha MHA-7]# tar zxf mha4mysql-manager-0.58.tar.gz
[root@mysql-mha MHA-7]# cd mha4mysql-manager-0.58/samples/conf/
[root@mysql-mha conf]# cat masterha_default.cnf app1.cnf > /etc/masterha/app1.cnf
#编辑配置文件
[root@mysql-mha ~]# vim /etc/masterha/app1.cnf
[server default]
user=root #mysql管理员用户,因为需要做自动化配置
password=lee #mysql密码
ssh_user=root #ssh远程登陆用户
repl_user=repl #mysql主从复制中负责认证的用户
repl_password=lee #mysql主从复制中负责认证的用户密码
master_binlog_dir= /data/mysql #二进制日志目录
remote_workdir=/tmp #远程工作目录
检测配置
(1)免密认证:
[root@mysql-mha ~]# ssh-keygen
[root@mysql-mha ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.50
[root@mysql-mha ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.60
[root@mysql-mha ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.70
[root@mysql-mha ~]# cd .ssh/
[root@mysql-mha .ssh]# ls
id_rsa id_rsa.pub known_hosts
[root@mysql-mha .ssh]# scp id_rsa root@172.25.254.50:/root/.ssh/
id_rsa 100% 1679 1.2MB/s 00:00
[root@mysql-mha .ssh]# scp id_rsa root@172.25.254.60:/root/.ssh/
id_rsa 100% 1679 1.4MB/s 00:00
[root@mysql-mha .ssh]# scp id_rsa root@172.25.254.70:/root/.ssh/
id_rsa 100% 1679 1.3MB/s 00:00
(2)检测网络和ssh 免密
[root@mysql-mha ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
....
Warning: Permanently added '172.25.254.50' (ECDSA) to the list of known hosts.
Fri Aug 2 16:57:42 2024 - [debug] ok.
Fri Aug 2 16:57:42 2024 - [debug] Connecting via SSH from
root@172.25.254.20(172.25.254.60:22) to root@172.25.254.30(172.25.254.70:22)..
Warning: Permanently added '172.25.254.70' (ECDSA) to the list of known hosts.
Fri Aug 2 16:57:42 2024 - [debug] ok.
Fri Aug 2 16:57:43 2024 - [debug]
Fri Aug 2 16:57:42 2024 - [debug] Connecting via SSH from
root@172.25.254.30(172.25.254.70:22) to root@172.25.254.10(172.25.254.50:22)..
Warning: Permanently added '172.25.254.50' (ECDSA) to the list of known hosts.
Fri Aug 2 16:57:42 2024 - [debug] ok.
Fri Aug 2 16:57:42 2024 - [debug] Connecting via SSH from
root@172.25.254.30(172.25.254.70:22) to root@172.25.254.20(172.25.254.60:22)..
Warning: Permanently added '172.25.254.60' (ECDSA) to the list of known hosts.
Fri Aug 2 16:57:42 2024 - [debug] ok.
Fri Aug 2 16:57:43 2024 - [info] All SSH connection tests passed successfully.
(3)检测数据主从复制情况
#在数据节点master端
mysql> GRANT ALL ON *.* TO root@'%' identified by 'lee'; #允许root远程登陆
#执行检测
[root@mysql-mha ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
.....
MHA的故障切换
MHA的故障切换过程共包括以下的步骤:
-
配置文件检查阶段,这个阶段会检查整个集群配置文件配置
-
宕机的master处理,这个阶段包括虚拟ip摘除操作,主机关机操作
-
复制dead master和最新slave相差的relay log,并保存到MHA Manger具体的目录下
-
识别含有最新更新的slave
-
应用从master保存的二进制日志事件(binlog events)
-
提升一个slave为新的master进行复制
-
使其他的slave连接新的master进行复制
#在master数据节点还在正常工作情况下
[root@mysql-mha ~]# masterha_master_switch \
--conf=/etc/masterha/app1.cnf \ #指定配置文件
--master_state=alive \ #指定master节点状态
--new_master_host=172.25.254.20 \ #指定新master节点
--new_master_port=3306 \ #执行新master节点端口
--orig_master_is_new_slave \ #原始master会变成新的slave
--running_updates_limit=10000 #切换的超时时间
#切换过程如下:
[root@mysql-mha masterha]# masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=172.25.254.20 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
.....
检测:
[root@mysql-mha masterha]# masterha_check_repl --conf=/etc/masterha/app1.cnf
master故障手动切换
#模拟master故障
[root@mysql-node20 mysql]# /etc/init.d/mysqld stop
#在MHA-master中做故障切换
[root@mysql-mha masterha]# masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=192.168.56.12 --dead_master_port=3306 --new_master_host=192.168.56.11 --new_master_port=3306 --ignore_last_failover
masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=172.25.254.20 --dead_master_port=3306 --new_master_host=172.25.254.10 --new_master_port=3306 --ignore_last_failover --dead_master_ip=<dead_master_ip>
--ignore_last_failover #表示忽略在/etc/masterha/目录中在切换过程中生成的锁文件
恢复故障mysql节点
[root@mysql-node20 tmp]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@mysql-node20 tmp]# mysql -p
mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.50', MASTER_USER='repl', MASTER_PASSWORD='redhat', MASTER_AUTO_POSITION=1;
mysql> show slave\G
#测试一主两从是否正常
[root@mysql-mha masterha]# masterha_check_repl --conf=/etc/masterha/app1.cnf
....
自动切换
#删掉切换锁文件
[root@mysql-mha masterha]# rm -fr app1.failover.complete
#监控程序通过指定配置文件监控master状态,当master出问题后自动切换并退出避免重复做故障切换
[root@mysql-mha masterha]# masterha_manager --conf=/etc/masterha/app1.cnf
恢复故障节点
[root@mysql-node20 mysql]# /etc/init.d/mysqld start
mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.50',MASTER_USER='repl',MASTER_PASSWORD='redhat', MASTER_AUTO_POSITION=1
清除锁文件
[root@mysql-mha masterha]# rm -rf app1.failover.complete manager.log
为MHA添加VIP功能
链接:百度网盘 请输入提取码 提取码:iq35
[root@mysql-mha ~]# ls
master_ip_failover master_ip_online_change MHA-7 MHA-7.zip
[root@mysql-mha ~]# cp master_ip_failover master_ip_online_change /usr/local/bin/
[root@mysql-mha ~]# chmod +x /usr/local/bin/master_ip_*
[root@mysql-mha ~]# vim /usr/local/bin/master_ip_failover
my $vip = '172.25.254.100/24';
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";
[root@mysql-mha ~]# vim /usr/local/bin/master_ip_online_change
my $vip = '172.25.254.100/24';
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";
my $exit_code = 0;
#启动监控程序
[root@mysql-mha masterha]# masterha_manager --conf=/etc/masterha/app1.cnf
#在master节点添加VIP
[root@mysql-node10 tmp]# ip a a 172.25.254.100/24 dev eth0
模拟场景:
#模拟故障
[root@mysql-node10 ~]# /etc/init.d/mysqld stop #关闭主节点服务
[root@mysql-mha masterha]# cat manager.log
#恢复故障主机
[root@mysql-node20 mysql]# /etc/init.d/mysqld start
mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.50', MASTER_USER='repl',
MASTER_PASSWORD='redhat', MASTER_AUTO_POSITION=1
[root@mysql-mha masterha]# rm -rf app1.failover.complete manager.log
#手动切换后查看vip变化
[root@mysql-mha masterha]# masterha_master_switch --conf=/etc/masterha/app1.cnf
--master_state=alive --new_master_host=172.25.254.50 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
[root@mysql-node10 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
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
group default qlen 1000
link/ether 00:0c:29:cb:63:ce brd ff:ff:ff:ff:ff:ff
inet 172.25.254.10/24 brd 172.25.254.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 172.25.254.100/24 scope global secondary eth0
valid_lft forever preferred_lft forever