Percona-mysql MHA高可用实战方案

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Percona-mysql MHA高可用实战方案相关的知识,希望对你有一定的参考价值。

前言

MHA(Master High Availability)目前在mysql高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是一套优秀的作为MySQLy高可用环境下故障切换和主从提升的高可用软件。在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。整个故障转移过程对应用程序是完全透明的。

1.     安装部署MHA前准备

MHA架构图

技术分享

 

 

 

 

 

 

 

 

 

具体搭建如表:

角色

IP地址

主机名

serverID

类型

Monitor host

192.168.127.100

MHA

监控集群组

Master

192.168.127.101

master

101

写入

Candicate master

192.168.127.102

slave01

102

slave

192.168.127.103

slave02

103

 

vi /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.127.100     MHA

192.168.127.101     master

192.168.127.102     slave01

192.168.127.103     slave02

1.1.   percona-mysql安装(masterslave01slave02 三台安装)

注意:三台的server_id 不一样,为了做主从同步

创建mysql用户:

useradd mysql

创建安装目录与数据目录:

mkdir /app

mkdir -p /data/mysql3306

解决percona-mysql软件:

tar zxvf Percona-Server-5.6.27-rel75.0-Linux.x86_64.ssl101.tar.gz

注意:安装的软件需要根据openssl版本来下载

rpm -qa | grep ssl

openssl-1.0.1e-15.el6.x86_64

把解压文件移动相应目录:

mv Percona-Server-5.6.27-rel75.0-Linux.x86_64.ssl101 /app/mysql5.6

 

创建放慢查询日志目录:

mkdir /app/mysql5.6/logs

给目录权限:

chown -R mysql:mysql /app/mysql5.6

chown -R mysql:mysql /data/mysql3306

 

创建配置文件

vi /app/mysql5.6/my.cnf

 

[client]

socket=/app/mysql5.6/mysql.sock

default-character-set=utf8

port=3306

[mysql]

prompt=\\[email protected]\\d \\r:\\m:\\s>

no-auto-rehash

[mysqld_safe]

log-error=/data/mysql3306/mysqld.error

[mysqld]

socket=/app/mysql5.6/mysql.sock

pid-file=/app/mysql5.6/mysqld.pid

basedir=/app/mysql5.6

datadir=/data/mysql3306

port=3306

server_id=101

character-set-server=utf8

skip-external-locking

skip-name-resolve

max_connections=1024

max_connect_errors=1000

wait_timeout =  400

interactive_timeout = 400

table_definition_cache=500

table_open_cache=500

sort_buffer_size = 16M

tmp_table_size = 200M

 

read_buffer_size = 1M

read_rnd_buffer_size = 4M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_type=0

query_cache_size=0

thread_concurrency = 16

lower_case_table_names = 1

log_bin_trust_function_creators = 1

#################slow log####################

slow-query_log=1

slow-query_log_file=/app/mysql5.6/logs/mysql.slow

long_query_time=2

####################binlog######################

log-bin=mysql-bin

binlog-format=ROW

expire_logs_days=5

sync_binlog=1

################replication##########

log-slave-updates=1

################INNODB################

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

transaction-isolation=READ-COMMITTED

innodb_buffer_pool_size=10G

innodb_flush_log_at_trx_commit=2

innodb_strict_mode=1

innodb_flush_method=O_DIRECT

innodb_file_format=Barracuda

innodb_log_files_in_group=3

innodb_file_per_table=1

innodb_io_capacity=500

innodb_support_xa=1

innodb_additional_mem_pool_size=16M

innodb_log_buffer_size=64M

 

 

[mysqldump]

quick

max_allowed_packet=128M

myisam_max_sort_sort_file_size=2G

 

 

初始化数据库

/app/mysql5.6/scripts/mysql_install_db --user=mysql --basedir=/app/mysql5.6 --datadir=/data/mysql3306   --defaults-file=/app/mysql5.6/my.cnf

 

启动脚本

cp  /app/mysql5.6/support-files/mysql.server /etc/init.d/mysql

vi /etc/init.d/mysql

 

basedir=/app/mysql5.6

datadir=/data/mysql3306

 

注意:修改以上两处即可

 

启动数据库

 

/etc/init.d/mysql start

 

Starting MySQL (Percona Server)....                        [  OK  ]

 

环境变量配置

vi /etc/profile

 

export MYSQL_HOME=/app/mysql5.6

export MY_BASEDIR_VERSION=/app/mysql5.6

export PATH=/app/mysql5.6/bin:/app/mysql5.6/scripts:$PATH

export LD_LIBRARY_PATH=/app/mysql5.6/lib

 

生效环境变量

source /etc/profile

1.2   .主从同步搭建

注意:防火墙需要关闭

创建复制账号(masterslave1(mha新主)

GRANT REPLICATION  SLAVE ON *.*  TO ‘repl‘@‘192.168.127.%‘  IDENTIFIED BY ‘repl‘;

flush privileges;

 

查看master binlog  POS点信息

[email protected](none) 06:47:05>show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000004 |      409 |              |                  |                   |

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

1 row in set (0.01 sec)

 

建立主从复制(slave01slave02

[[email protected] ~]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.6.27-75.0-log Percona Server (GPL), Release 75.0, Revision 8bb53b6

 

Copyright (c) 2009-2015 Percona LLC and/or its affiliates

Copyright (c) 2000, 2015, 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.

 

[email protected](none) 07:03:39>CHANGE MASTER TO MASTER_HOST=‘192.168.127.101‘,MASTER_PORT=3306,MASTER_USER=‘repl‘,MASTER_PASSWORD=‘repl‘,MASTER_LOG_FILE=‘mysql-bin.000004‘,MASTER_LOG_POS=409;

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

 

[email protected](none) 07:03:41>start slave;

Query OK, 0 rows affected (0.02 sec)

 

查看主从复制

[email protected](none) 07:03:42>show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.127.101

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000004

          Read_Master_Log_Pos: 409

               Relay_Log_File: mysqld-relay-bin.000002

                Relay_Log_Pos: 283

        Relay_Master_Log_File: mysql-bin.000004

             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: 409

              Relay_Log_Space: 457

              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: 101

                  Master_UUID: 8b1cf62d-e063-11e5-84ba-000c2908253f

             Master_Info_File: /data/mysql3306/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

           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

1 row in set (0.02 sec)

 

ERROR:

No query specified

 

以上主从已经搭建好,下面我们安装与配置MHA

1slave服务器(192.168.127.102,192.168.103)设置read only;

mysql> set global read_only=1;

2)设置relay log清除方式(在每个slave 下)

mysql> set global relay_log_purge=0;

(3)创建监控用户,在所有MYSQL上执行

mysql> grant all privileges on *.* to ‘root‘@‘192.168.127.%‘ identified by ‘123456‘;

mysql>flush privileges;

 

 

4)在slave01(192.168.127.102)上创建复制用户:

mysql> grant replication slave on *.* to ‘repl‘@‘192.168.127.%‘ identified by ‘repl‘;

mysql>flush privileges;

 

 

2.安装部署MHA

2.1安装MHA node(在所有Mysql服务器上安装)

1)安装依赖包

  rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Config-IniFiles perl-Time-HiRes  perl-Time-HiRes  perl-CPAN

 

2)在所有的节点上安装mha node:

tar  zxvf  mha4mysql-node-0.56.tar.gz

cd  mha4mysql-node-0.56

perl  Makefile.PL

make

make install

 

2.2.安装MHA Manager

MHA Manager中主要包括了几个管理员的命令行工具,例如masterha_managermasterha_master_switch等。

(1)       安装依赖包

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Config-IniFiles perl-Time-HiRes  perl-Time-HiRes  perl-CPAN

(2)       安装MHA node软件包。注意,在MHA Manger的主机上也要安装MHA node.

tar  zxvf  mha4mysql-node-0.56.tar.gz

cd  mha4mysql-node-0.56

perl  Makefile.PL

make

make install

(3)       安装MHA Manager软件包。

tar zxvf mha4mysql-manager-0.56.tar.gz

cd mha4mysql-manager-0.56

perl Makefile.PL

make

make install

 

2.3. 配置SSH 登录无密码验证

(1)       manager 192.168.127.100上配置到所有节点的无密码验证

ssh-keygen -t rsa

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

(2)       MHA Node master(192.168.127.101)上:

 ssh-keygen -t rsa

 ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

     ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

(3)       MHA Node slave01(192.168.127.102)上:

 ssh-keygen -t rsa

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

 ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

     ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

(4)       MHA Node slave02(192.168.127.103)上:

 ssh-keygen -t rsa

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

 ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

     ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

 

   在每台做以下步骤

ln -s /app/mysql5.6/bin/* /usr/local/bin/

把脚本拷贝相关目录

[[email protected] ~]# cp /root/mha4mysql-manager-0.56/samples/scripts/master_ip_failover /usr/local/bin/

 

[[email protected] ~]# cp /root/mha4mysql-manager-0.56/samples/scripts/master_ip_online_change /usr/local/bin/

 

[[email protected]~]#cp /root/mha4mysql-manager-0.56/samples/scripts/send_report  /usr/local/bin/

 

[[email protected] ~]# cp /root/mha4mysql-manager-0.56/bin/masterha_secondary_check /usr/bin/

3.配置MHA

配置MHA的步骤如下。

(1)       创建MHA工作目录,并且创建相关配置文件:

mkdir -p /etc/masterha

mkdir -p /masterha/app1

      配置如下

vi /etc/masterha/app1.cnf

[server default]

manager_workdir=/masterha/app1

manager_log=/masterha/app1/app1.log

master_ip_failover_script=/usr/local/bin/master_ip_failover

master_ip_online_change_script=/usr/local/bin/master_ip_online_change

 

user=root

password=123456

ssh_user=root

repl_user=repl

repl_password=repl

ping_interval=1

remote_workdir=/tmp

report_script=/usr/local/bin/send_report

secondary_check_script=/usr/bin/masterha_secondary_check  -s MHA -s slave02  --user=root --master_host=master --master_ip=192.168.127.101 --master_port=3306 --password=123456

shutdown_script=""

report_script=""

 

 

[server1]

hostname=192.168.127.101

master_binlog_dir=/data/mysql3306

candidate_master=1

[server2]

hostname=192.168.127.102

master_binlog_dir=/data/mysql3306

candidate_master=1

check_repl_delay=0

 

[server3]

hostname=192.168.127.103

master_binlog_dir=/data/mysql3306

no_master=1

 

4.检查SSH的配置

检查MHA Manager到所有MHA nodeSSH连接状态:

[[email protected] ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf

Wed Mar  2 19:03:30 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Wed Mar  2 19:03:30 2016 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Wed Mar  2 19:03:30 2016 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Wed Mar  2 19:03:30 2016 - [info] Starting SSH connection tests..

Wed Mar  2 19:03:31 2016 - [debug]

Wed Mar  2 19:03:30 2016 - [debug]  Connecting via SSH from [email protected](192.168.127.101:22) to [email protected](192.168.127.102:22)..

Wed Mar  2 19:03:30 2016 - [debug]   ok.

Wed Mar  2 19:03:30 2016 - [debug]  Connecting via SSH from [email protected](192.168.127.101:22) to [email protected](192.168.127.103:22)..

Wed Mar  2 19:03:30 2016 - [debug]   ok.

Wed Mar  2 19:03:31 2016 - [debug]

Wed Mar  2 19:03:30 2016 - [debug]  Connecting via SSH from [email protected](192.168.127.102:22) to [email protected](192.168.127.101:22)..

Wed Mar  2 19:03:31 2016 - [debug]   ok.

Wed Mar  2 19:03:31 2016 - [debug]  Connecting via SSH from [email protected](192.168.127.102:22) to [email protected](192.168.127.103:22)..

Wed Mar  2 19:03:31 2016 - [debug]   ok.

Wed Mar  2 19:03:32 2016 - [debug]

Wed Mar  2 19:03:31 2016 - [debug]  Connecting via SSH from [email protected](192.168.127.103:22) to [email protected](192.168.127.101:22)..

Wed Mar  2 19:03:31 2016 - [debug]   ok.

Wed Mar  2 19:03:31 2016 - [debug]  Connecting via SSH from [email protected](192.168.127.103:22) to [email protected](192.168.127.102:22)..

Wed Mar  2 19:03:32 2016 - [debug]   ok.

Wed Mar  2 19:03:32 2016 - [info] All SSH connection tests passed successfully.

5.检查整个复制环境

[[email protected] ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf

Wed Mar  2 19:03:30 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Wed Mar  2 19:03:30 2016 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Wed Mar  2 19:03:30 2016 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Wed Mar  2 19:03:30 2016 - [info] Starting SSH connection tests..

Wed Mar  2 19:03:31 2016 - [debug]

Wed Mar  2 19:03:30 2016 - [debug]  Connecting via SSH from [email protected](192.168.127.101:22) to [email protected](192.168.127.102:22)..

Wed Mar  2 19:03:30 2016 - [debug]   ok.

Wed Mar  2 19:03:30 2016 - [debug]  Connecting via SSH from [email protected](192.168.127.101:22) to [email protected](192.168.127.103:22)..

Wed Mar  2 19:03:30 2016 - [debug]   ok.

Wed Mar  2 19:03:31 2016 - [debug]

以上是关于Percona-mysql MHA高可用实战方案的主要内容,如果未能解决你的问题,请参考以下文章

基于半同步复制的MHA高可用MySql集群架构搭建实战

MySQL数据库——MHA高可用集群架构(实战!!!)

MHA架构概述及实战

MySQL架构之MHA架构实战

MySQL高可用之MHA架构企业实战

MySQL高可用之MHA架构企业实战