当前位置:Gxlcms > 数据库问题 > MySQL的高可用(MHA)

MySQL的高可用(MHA)

时间:2021-07-01 10:21:17 帮助过:13人阅读

MySQL的高可用(MHA)

MHA简介

MHA:Master High Availability,对主节点进行监控,可实现自动故障转移至其他从节点;通过提升某一从节点为新的主节点,基于主从复制实现,还需要客户端配合实现,目前MHA主要支持一主二从,即一台充当master,一台充当备用master,另外一台充当从数据库,出于机器成本的考虑,淘宝进行了改造,目前淘宝TMHA已经一主一从。

MHA架构

技术图片

MHA的工作原理

技术图片
MHA是由一台manager服务器远程监控主服务器,当主服务器挂了提升一台从服务器作为主服务器。
当主节点挂了,manager首先要查看哪台从节点,同步的数据最多,然后提升同步最多的从节点为主节点,再将其余的MySQL服务器对他做从节点。
如果原主节点没彻底死透,manager会让新的主机通过ssh协议远程连接到原先的主节点,拉取二进制日志进行同步。如果主节死透了那就放弃。  


MHA搭建

环境准备

一、准备4台主机,管理节点1台,主节点MySQL服务器1台,从节点MySQL服务器2台

主机 IP
Manager 192.168.73.111
Master 192.168.73.110
Slave1 192.168.73.112
Slave2 192.168.73.113

二、将Manager管理节点配置为时间服务器,向所有MySQL服务器提供时间同步。

1.安装chrony服务

  1. <code class="language-bash">[root@Manager ~]# yum install -y chrony</code>

2.修改chrony配置文件

  1. <code class="language-bash">[root@Manager ~]# vim /etc/chrony.conf
  2. server 172.22.0.1 iburst
  3. allow 192.168.0.0/16
  4. local stratum 10</code>

3.启动chrony服务

  1. <code class="language-bash">[root@Manager ~]# systemctl start chronyd</code>

4.将MySQL服务器与Manager服务器进行时间同步
4.1在所有MySQL主机上修改配置文件并启动,并启动服务

  1. <code class="language-bash">[root@Master ~]# sed -i ‘/^server 0/i server 192.168.73.111 iburst‘ /etc/chrony.conf
  2. [root@Master ~]# systemctl start chronyd</code>

4.2确认时间同步

  1. <code class="language-bash">[root@Master ~]# chronyc sources -v
  2. 210 Number of sources = 1
  3. .-- Source mode ‘^‘ = server, ‘=‘ = peer, ‘#‘ = local clock.
  4. / .- Source state ‘*‘ = current synced, ‘+‘ = combined , ‘-‘ = not combined,
  5. | / ‘?‘ = unreachable, ‘x‘ = time may be in error, ‘~‘ = time too variable.
  6. || .- xxxx [ yyyy ] +/- zzzz
  7. || Reachability register (octal) -. | xxxx = adjusted offset,
  8. || Log2(Polling interval) --. | | yyyy = measured offset,
  9. || \ | | zzzz = estimated error.
  10. || | | MS Name/IP address Stratum Poll Reach LastRx Last sample
  11. ===============================================================================
  12. ^* 192.168.73.111 4 6 377 54 +25us[ +41us] +/- 105ms</code>

三、配置ssh为的密钥认证登陆

当主节点宕机,manager会让从节点通过ssh协议去尝试连接主节点,并拉取二进制日志,所以要时用密钥的认证方式让从节点登陆到主节点拉取数据。
1.在manager服务器上生成私钥文件

  1. <code class="language-bash">[root@Manager ~]# ssh-keygen
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:yAvC2PJUlRyAf1udlrVXzmIsUljTdUdW6X6FVpQ3Ajo root@Manager
  10. The key‘s randomart image is:
  11. +---[RSA 2048]----+
  12. | ..ooo ++. +%|
  13. | . .o o oo.=*|
  14. | .. E = oo*o|
  15. | + ...... B o B.+|
  16. |o = ..ooS. . =...|
  17. | + . ... ..|
  18. | . . .|
  19. | |
  20. | |
  21. +----[SHA256]-----+</code>

2.将公钥文件复制给自己

  1. <code class="language-bash">[root@Manager ~]# ssh-copy-id 127.0.0.1</code>

3.将整个~/.ssh目录复制给所有的MySQL主机

  1. <code class="language-bash">[root@Manager ~]# scp -r ~/.ssh 192.168.73.110:/root</code>

至此所有环境准备完毕

一、配置主从复制

主节点配置

1.修改配置文件

  1. <code class="language-bash">[root@Master ~]# vim /etc/my.cnf
  2. [mysqld]
  3. server-id=1
  4. log-bin
  5. binlog-format=row
  6. skip_name_resolve</code>

2.启动数据库服务

  1. <code class="language-bash">[root@Master ~]# systemctl start mariadb</code>

3.创建主从复制账号

  1. <code class="language-bash">[root@Master ~]# mysql -e "GRANT REPLICATION SLAVE ON *.* TO ‘repluser‘@‘192.168.73.%‘ IDENTIFIED BY ‘centos‘;"</code>

4.添加mha的管理账号,让管理节点远程连接到主机用来设置主从调整

  1. <code class="language-bash">[root@Master ~]# mysql -e "GRANT ALL ON *.* TO ‘mhauser‘@‘192.168.73.%‘ IDENTIFIED BY ‘centos‘;"</code>

从节点配置

1.修改配置文件

  1. <code class="language-bash">[root@Slave1 ~]# vim /etc/my.cnf
  2. [mysqld]
  3. server-id=2
  4. read-only
  5. log-bin
  6. relay_log_purge=0
  7. skip_name_resolve</code>

2.启动服务

  1. <code class="language-bash">[root@Slave1 ~]# systemctl start mariadb</code>

3.配置CHANGE MASTER TO

  1. <code class="language-bash">MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.73.110‘, MASTER_USER=‘repluser‘,MASTER_PASSWORD=‘centos‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mariadb-bin.000001‘,MASTER_LOG_POS=245;
  2. Query OK, 0 rows affected (0.00 sec)</code>

4.启动线程

  1. <code class="language-bash">MariaDB [(none)]> START SLAVE;
  2. Query OK, 0 rows affected (0.00 sec)</code>

在Slave2节点上也执行相同的操作,此处步骤省略,需要注意server-id需要修改为和其他主从节点不同

5.测试
主节点导入hellodb库

  1. <code class="language-bash">[root@Master ~]# mysql < hellodb_innodb.sql</code>

从节点查看是否同步
slave1

  1. <code class="language-bash">[root@Slave1 ~]# mysql -e "SHOW DATABASES;"
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | hellodb |
  7. | mysql |
  8. | performance_schema |
  9. | test |
  10. +--------------------+</code>

Slave2

  1. <code class="language-bash">[root@Slave2 ~]# mysql -e "SHOW DATABASES;"
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | hellodb |
  7. | mysql |
  8. | performance_schema |
  9. | test |
  10. +--------------------+</code>

二、配置管理节点及被管理节点

1.在管理节上安装mha4mysql-manager、mha4mysql-node,将两个包放在同一目录下

  1. <code class="language-bash">[root@Manager ~]# yum install *.rpm -y #这两个包有依赖管理需要一起安装</code>

2.在所有被管理节点上安装mha4mysql-node

  1. <code class="language-bash">[root@Master ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y</code>
  1. <code class="language-bash">[root@Slave1 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y</code>
  1. <code class="language-bash">[root@Slave2 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y</code>

3.在管理节点上创建配置文件

  1. <code class="language-bash">[root@Manager ~]# vim /etc/mha/aap1.conf
  2. [server default]
  3. user=mhauser
  4. password=centos
  5. manager_workdir=/data/mastermha/app1/
  6. manager_log=/data/mastermha/app1/manager.log
  7. remote_workdir=/data/mastermha/app1/
  8. ssh_user=root
  9. repl_user=repluser
  10. repl_password=centos
  11. ping_interval=1
  12. [server1]
  13. hostname=192.168.73.110
  14. candidate_master=1
  15. [server2]
  16. hostname=192.168.73.112
  17. candidate_master=1
  18. [server3]
  19. hostname=192.168.73.113
  20. candidate_master=1 </code>

4.做检查
4.1检查ssh连接

  1. <code class="language-bash">[root@Manager ~]# masterha_check_ssh --conf=/etc/mha/aap1.conf </code>

4.2检查主从复制

  1. <code class="language-bash">[root@Manager ~]# masterha_check_repl --conf=/etc/mha/aap1.conf </code>

5.以上两项全部成功后启动程序
mha这个程序是跑在前台的,一次性的可以使用nohub或screen来解决跑在前台的问题

  1. <code class="language-bash">[root@Manager ~]# masterha_manager --conf=/etc/mha/aap1.conf </code>

三、测试

1.在master上跑个存储过程,导入存储过程

  1. <code class="language-bash">[root@Master ~]# mysql hellodb < testlog.sql </code>

2.调用存储过程

  1. <code class="language-bash">MariaDB [(none)]> USE hellodb
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. MariaDB [hellodb]> call pro_testlog;</code>

3.另起一个主节点窗口将主节点断网

  1. <code class="language-bash">[root@Master ~]# ifdown ens33</code>

4.manager端完成切换退出,查看日志,查看新的主节点是哪台slave

  1. <code class="language-bash">[root@Manager app1]# tail /data/mastermha/app1/manager.log
  2. Started automated(non-interactive) failover.
  3. The latest slave 192.168.73.112(192.168.73.112:3306) has all relay logs for recovery.
  4. Selected 192.168.73.112(192.168.73.112:3306) as a new master.
  5. 192.168.73.112(192.168.73.112:3306): OK: Applying all logs succeeded.
  6. 192.168.73.113(192.168.73.113:3306): This host has the latest relay log events.
  7. Generating relay diff files from the latest slave succeeded.
  8. 192.168.73.113(192.168.73.113:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.73.112(192.168.73.112:3306)
  9. 192.168.73.112(192.168.73.112:3306): Resetting slave info succeeded.
  10. Master failover to 192.168.73.112(192.168.73.112:3306) completed successfully.
  11. #此处显示最新的主节点为192.168.73.112</code>

由于从节点在配置文件中定义的为read-only,此时被提升为主能执行写操作时应为管理服务器上有管理账号,他将从节点的服务器全局变量read_only给关闭了

  1. <code class="language-bash">[root@Slave1 ~]# mysql -e "SELECT @@read_only;"
  2. +-------------+
  3. | @@read_only |
  4. +-------------+
  5. | 0 |
  6. +-------------+</code>

为了防止服务服务重启再次变为read-only,此时需要对新主节点的配置文件进行修改将read-only行注释

  1. <code class="language-bash">[mysqld]
  2. server-id=2
  3. #read-only
  4. log-bin
  5. relay_log_purge=0
  6. skip_name_resolve</code>

四、测试新的主节点

1.对hellodb.teachers表插入数据

  1. <code class="language-bash">[root@Slave1 ~]# mysql -e "INSERT hellodb.teachers VALUES(5,‘Tang San‘,30,‘M‘);"</code>

2.Slave2主机上查看是否同步

  1. <code class="language-bash">[root@Slave2 ~]# mysql -e "SELECT * FROM hellodb.teachers;"
  2. +-----+---------------+-----+--------+
  3. | TID | Name | Age | Gender |
  4. +-----+---------------+-----+--------+
  5. | 1 | Song Jiang | 45 | M |
  6. | 2 | Zhang Sanfeng | 94 | M |
  7. | 3 | Miejue Shitai | 77 | F |
  8. | 4 | Lin Chaoying | 93 | F |
  9. | 5 | Tang San | 30 | M | #已经同步
  10. +-----+---------------+-----+--------+</code>

其他事项

当原主节点被修复后,将其添加为从节点使用。

MySQL的高可用(MHA)

标签:复制   erp   logs   ash   love   nlog   let   cloc   ali   

人气教程排行