MySQL高可用架构之MHA原理及其部署,三种切换方式以及vip的漂移

            MySQL高可用架构之MHA

一.简介:

MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于 Facebook公司)开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件。在MySQL故障切换过程中,MHA能做到在 0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用。

该软件由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)MHA Manager可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台slave节点上。MHA Node运行在每台MySQL服务器上,MHA Manager会定时探测集群中的master节点,当master出现故障时,它可以自动将最新数据的slave提升为新的master,然后将所有其 他的slave重新指向新的master。整个故障转移过程对应用程序完全透明。

在MHA自动故障切换过程中,MHA试图从宕机的主服务器上保存二进制日志,最大程度的保证数据的不丢失,但这并不总是可行的。例如,如果主服务器 硬件故障或无法通过ssh访问,MHA没法保存二进制日志,只进行故障转移而丢失了最新的数据。使用MySQL 5.5的半同步复制,可以大大降低数据丢失的风险。MHA可以与半同步复制结合起来。如果只有一个slave已经收到了最新的二进制日志,MHA可以将最 新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数据一致性。

目前MHA主要支持一主多从的架构要搭建MHA,要求一个复制集群中必须最少有三台数据库服务器一主二从,即一台充当master,一台充当备用master,另外一台充当从库,因为至少需要三台服务器,出于机器成本的考虑,淘宝也在该基础上进行了改造,目前淘宝TMHA已经支持一主一从。

MHA工作原理总结为如下:

 

   

 

(1)从宕机崩溃的master保存二进制日志事件(binlog events);

(2)识别含有最新更新的slave;

(3)应用差异的中继日志(relay log)到其他的slave;

(4)应用从master保存的二进制日志事件(binlog events);

(5)提升一个slave为新的master;

(6)使其他的slave连接新的master进行复制;

MHA软件由两部分组成,Manager工具包和Node工具包.

二.部署MHA

实验环境介绍;
server1     172.25.17.1    node1
server2     172.25.17.2    node2
server4     172.25.17.4    node3
server5     172.25.17.5    作为监测节点;manager

 

步骤一:搭建一主两从

(1)主数据库:server1:

此时的server1为master,server2,server4为两个slave

  所需安装包: mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar

server1   搭建mysql 环境:
[root@server1 ~]# tar xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar #解压缩
[root@server1 ~]# yum install -y mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-common-5.7.24-1.el7.x86_64.rpm  mysql-community-libs-5.7.24-1.el7.x86_64.rpm mysql-community-server-5.7.24-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm   #安装包

[root@server1 ~]# systemctl start mysqld   #启动Mysqld
[root@server1 ~]#  cat /var/log/mysqld.log | grep password  #寻找初始密码
2019-02-26T04:20:30.795660Z 1 [Note] A temporary password is generated for root@localhost: vkgfv-Y2bq5Y
[root@server1 ~]# mysql_secure_installation  #数据库初始化

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

数据库搭建完成!!!!

接下来就是配置主数据库的部署操作了:

1.首先编辑配置文件:
[root@server1 ~]# vim /etc/my.cnf

 29 server_id=1
 30 gtid_mode=ON
 31 enforce_gtid_consistency=ON
 32 log_slave_updates=ON
 33 log_bin=binlog
[root@server1 ~]# systemctl restart mysqld
2.进入数据库设置主节点:
[root@server1 ~]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#安装主节点插件
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.12 sec)
#安装从节点插件
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.04 sec)


mysql>  SET GLOBAL rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)

mysql> SET GLOBAL rpl_semi_sync_master_timeout=10000;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%rpl%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled              | OFF        |
| 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 |
| rpl_semi_sync_slave_enabled               | ON         |
| rpl_semi_sync_slave_trace_level           | 32         |
| rpl_stop_slave_timeout                    | 31536000   |
+-------------------------------------------+------------+
9 rows in set (0.00 sec)

mysql> show status like '%rpl%';
+--------------------------------------------+-------+
| 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                | OFF   |
| 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     |
| Rpl_semi_sync_slave_status                 | OFF   |
+--------------------------------------------+-------+
15 rows in set (0.00 sec)

mysql> create database westos;
Query OK, 1 row affected (0.09 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| westos             |
+--------------------+
5 rows in set (0.00 sec)

mysql> exit
Bye
[root@server1 ~]# ls
mha4mysql-manager-0.58-0.el7.centos.noarch.rpm           mysql-community-minimal-debuginfo-5.7.24-1.el7.x86_64.rpm
mha4mysql-manager-0.58.tar.gz                            mysql-community-server-5.7.24-1.el7.x86_64.rpm
mha4mysql-node-0.58-0.el7.centos.noarch.rpm              mysql-community-server-minimal-5.7.24-1.el7.x86_64.rpm
mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar                 mysql-community-test-5.7.24-1.el7.x86_64.rpm
mysql-community-client-5.7.24-1.el7.x86_64.rpm           perl-Config-Tiny-2.14-7.el7.noarch.rpm
mysql-community-common-5.7.24-1.el7.x86_64.rpm           perl-Email-Date-Format-1.002-15.el7.noarch.rpm
mysql-community-devel-5.7.24-1.el7.x86_64.rpm            perl-Log-Dispatch-2.41-1.el7.1.noarch.rpm
mysql-community-embedded-5.7.24-1.el7.x86_64.rpm         perl-Mail-Sender-0.8.23-1.el7.noarch.rpm
mysql-community-embedded-compat-5.7.24-1.el7.x86_64.rpm  perl-Mail-Sendmail-0.79-21.el7.noarch.rpm
mysql-community-embedded-devel-5.7.24-1.el7.x86_64.rpm   perl-MIME-Lite-3.030-1.el7.noarch.rpm
mysql-community-libs-5.7.24-1.el7.x86_64.rpm             perl-MIME-Types-1.38-2.el7.noarch.rpm
mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm      perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm



[root@server1 ~]# yum install -y mha4mysql-node-0.58-0.el7.centos.noarch.rpm
                 
  
Complete!

 

(2)从数据库:server2.server4:

此部分server4与server2的操作基本相同,所以不再赘述server4.

server2数据库的安装以及初始化:

server2:

[root@server2 ~]# tar xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar 
[root@server2 ~]# yum install -y mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-common-5.7.24-1.el7.x86_64.rpm  mysql-community-libs-5.7.24-1.el7.x86_64.rpm mysql-community-server-5.7.24-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm

Complete!
[root@server2 ~]# systemctl start mysqld
[root@server2 ~]#  cat /var/log/mysqld.log | grep password
2019-02-26T04:20:29.641252Z 1 [Note] A temporary password is generated for root@localhost: ukAa3edg#_&f
[root@server2 ~]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : yy
Success.

All done! 

从节点一的设置【server2】

[root@server2 ~]# vim /etc/my.cnf

 29  server_id=2    #此时server4设置的server_id=3
 30 gtid_mode=ON
 31 enforce_gtid_consistency=ON
 32 log_slave_updates=ON
 33 log_bin=binlog
 34 
[root@server2 ~]# systemctl restart mysqld
[root@server2 ~]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

#设置server2作为172.25.17.1的从节点:
mysql> change master to master_host='172.25.17.1',master_user='repl',master_password='Westos-6',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.46 sec)

#启动从节点

mysql> start slave;
Query OK, 0 rows affected (0.08 sec)

server2作为从节点1启动后查看状态时:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.25.17.1
                  Master_User: repl
                  Master_Port: 3306
     
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值