mariadb搭建MHA高可用

配置环境

-   数据库: mariadb5.5.6
-   操作系统:centos7.6.1810
- 	服务器ip
	mha1-master-ip:192.168.117.135
	mha2-slave1-ip:192.168.117.141(manager_mha)
	mha3-slave2-ip:192.168.117.142

操作步骤

安装Mariadb

[root@localhost package]# yum install mariadb-server mariadb
[root@localhost package]# rpm -q mariadb mariadb-server
mariadb-5.5.56-2.el7.x86_64
mariadb-server-5.5.56-2.el7.x86_64

安装完成后,将MariaDB设置为开机启动,操作如下:
# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service

接着,启动|停止|重启|状态MariaDB,执行如下命令:
# systemctl start|stop|restart|status mariadb

安装成功:
[root@localhost package]# systemctl start mariadb
[root@localhost package]# systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-07-26 10:46:48 CST; 24s ago
  Process: 6609 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
  Process: 6530 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
 Main PID: 6608 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─6608 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─6770 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/...

Jul 26 10:46:46 localhost.localdomain mariadb-prepare-db-dir[6530]: MySQL manual for more instructions.
Jul 26 10:46:46 localhost.localdomain mariadb-prepare-db-dir[6530]: Please report any problems at http://mariadb.org/jira
Jul 26 10:46:46 localhost.localdomain mariadb-prepare-db-dir[6530]: The latest information about MariaDB is available at http://...rg/.
Jul 26 10:46:46 localhost.localdomain mariadb-prepare-db-dir[6530]: You can find additional information about the MySQL part at:
Jul 26 10:46:46 localhost.localdomain mariadb-prepare-db-dir[6530]: http://dev.mysql.com
Jul 26 10:46:46 localhost.localdomain mariadb-prepare-db-dir[6530]: Consider joining MariaDB's strong and vibrant community:
Jul 26 10:46:46 localhost.localdomain mariadb-prepare-db-dir[6530]: https://mariadb.org/get-involved/
Jul 26 10:46:46 localhost.localdomain mysqld_safe[6608]: 190726 10:46:46 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
Jul 26 10:46:46 localhost.localdomain mysqld_safe[6608]: 190726 10:46:46 mysqld_safe Starting mysqld daemon with databases from...mysql
Jul 26 10:46:48 localhost.localdomain systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.

进入数据库:
[root@localhost package]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 



注:
配置集群最好把防火墙关闭 systemctl stop firewalld

主库配置

  • 1.配置主库
配置主库
[root@localhost package]# vim /etc/my.cnf
配置内容(/etc/my.cnf)

[root@localhost package]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks

# 配置主从时需要添加以下信息 start
innodb_file_per_table=NO
log-bin=/var/lib/mysql/master-bin #log-bin没指定存储目录,则是默认datadir指向的目录
binlog_format=mixed
server-id=135 #服务器ip
# 配置主从时需要添加一下信息 end

symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

重启数据库
[root@localhost package]# systemctl restart mariadb
  • 2.创建账号并赋予replication的权限
GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.117.%' IDENTIFIED BY '12345678;

[root@localhost package]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.117.%' IDENTIFIED BY '12345678';
Query OK, 0 rows affected (0.00 sec)

  • 3.备份数据库,并同步到从库
-   1.加锁备份,记录主库log文件及其当前位置
MariaDB [(none)]>  FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      553 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

-   2.退出mariadb,开始备份到从库
[root@localhost ~]# scp db.sql [email protected]:/root/db.sql
[email protected]'s password: 
db.sql                                                                                               100%    0     0.0KB/s   00:00

[root@localhost ~]# scp db.sql [email protected]:/root/db.sql
[email protected]'s password: 
db.sql                                                                                               100%    0     0.0KB/s   00:00
现在去从库查看/root下已经同步了存主库的db.sql

-    3.释放锁
MariaDB [(none)]> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

到此为止,主库配置完成

从库配置

  • 从库配置
配置从库(/etc/my.cnf)
[root@localhost package]# vim /etc/my.cnf
添加如下内容(每个从库都有配置):

#配置主从时需要添加以下信息 start
innodb_file_per_table=NO
server-id=141 #一般与服务器ip的最后数字一致
relay-log=/var/lib/mysql/relay-bin
log_bin=on #mha用到log-bin日志
##配置主从时需要添加以下信息 end 

注:不要忘记配置后重启
重启数据库
[root@localhost package]# systemctl restart mariadb
进入从库,设置主从复制,同步中继日志和bin-log日志(每个从库都要执行)
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.117.135',MASTER_USER='root', MASTER_PASSWORD='12345678', MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=553;
Query OK, 0 rows affected (0.05 sec)



这个命令完成以下几个任务:
    a.设置当前服务器为主服务器(192.168.117.135)的从库
    b.提供当前数据库(从库)从主库复制数据时所需的用户名和密码,即上面的GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168。117.%' IDENTIFIED BY '12345678';设置的
    c.指定从库开始复制主库时需要使用的日志文件和文件位置,即上面主库执行SHOW MASTER STATUS;显示结果中的File和Position
开启主从复制(每个从库都要执行)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
查看主从同步是否成功(每个从库都要执行)

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Queueing master event to the relay log
                  Master_Host: 192.168.117.135
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 556
               Relay_Log_File: relay-bin.000880
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Yes //io是否连接
            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: 556
              Relay_Log_Space: 1103
              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: 135
1 row in set (0.01 sec)

ERROR: No query specified

测试是否主从同步
-   1.可以再主库上创建一个k库,看看从库是否同步,主库创建test_master_slave库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> create database test_master_slave;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| test_master_slave  |
+--------------------+
5 rows in set (0.00 sec)



-   2.从库查看是否同步此库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| test_master_slave  |
+--------------------+
5 rows in set (0.00 sec)

到此为止,主从复制同步成功

开启ssh免密

  • 开启ssh免密
每个库都执行如下命令

-   1.生成ssh秘钥
ssh-keygen

-   2.免密登录
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

注:遇到是否yes,yes,输入管理员密码

安装node和manager相关包和配置

  • 1.安装node和manager相关包
-   1.rpm 相关包下载地址(使用wget下载)
https://github.com/yoshinorim/mha4mysql-manager/releases
https://github.com/yoshinorim/mha4mysql-node/releases

-   2.安装perl相关依赖(每台服务器都安装)
        yum install epel-release -y
        yum -y install perl-DBD-MySQL perl-ExtUtils-MakeMaker perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-CPAN


-   3.安装node(每台服务器都安装)
        [root@localhost mha4mysql-node-0.56]# tar xvf mha4mysql-node-0.56.tar.gz
        [root@localhost mha4mysql-node-0.56]# cd mha4mysql-node-0.56
        [root@localhost mha4mysql-node-0.56]# perl Makefile.PL  
        [root@localhost mha4mysql-node-0.56]# perl Makefile.PL
        *** Module::AutoInstall version 1.03
        *** Checking for Perl dependencies...
        [Core Features]
        - DBI        ...loaded. (1.627)
        - DBD::mysql ...loaded. (4.023)
        *** Module::AutoInstall configuration finished.
        Checking if your kit is complete...
        Looks good
        Writing Makefile for mha4mysql::node
        [root@localhost mha4mysql-node-0.56]# make && make install
        [root@localhost mha4mysql-node-0.56]# ll /usr/local/bin/
        total 44
        -r-xr-xr-x. 1 root root 16367 Jul 26 14:44 apply_diff_relay_logs
        -r-xr-xr-x. 1 root root  4807 Jul 26 14:44 filter_mysqlbinlog
        -r-xr-xr-x. 1 root root  8261 Jul 26 14:44 purge_relay_logs
        -r-xr-xr-x. 1 root root  7525 Jul 26 14:44 save_binary_logs
        
        至此,MHA node节点安装完毕,会在/usr/local/bin下生成如上脚本文件

-   4.安装manager(作为manager监听的服务器安装)
        [root@localhost package]# tar xvf mha4mysql-manager-0.56.tar.gz 
        [root@localhost package]# cd mha4mysql-manager-0.56
        [root@localhost mha4mysql-manager-0.56]#  perl Makefile.PL
        [root@localhost mha4mysql-manager-0.56]# ll /usr/local/bin/
        total 84
        -r-xr-xr-x. 1 root root 16367 Jul 26 14:46 apply_diff_relay_logs
        -r-xr-xr-x. 1 root root  4807 Jul 26 14:46 filter_mysqlbinlog
        -r-xr-xr-x. 1 root root  1995 Jul 26 14:47 masterha_check_repl
        -r-xr-xr-x. 1 root root  1779 Jul 26 14:47 masterha_check_ssh
        -r-xr-xr-x. 1 root root  1865 Jul 26 14:47 masterha_check_status
        -r-xr-xr-x. 1 root root  3201 Jul 26 14:47 masterha_conf_host
        -r-xr-xr-x. 1 root root  2517 Jul 26 14:47 masterha_manager
        -r-xr-xr-x. 1 root root  2165 Jul 26 14:47 masterha_master_monitor
        -r-xr-xr-x. 1 root root  2373 Jul 26 14:47 masterha_master_switch
        -r-xr-xr-x. 1 root root  5171 Jul 26 14:47 masterha_secondary_check
        -r-xr-xr-x. 1 root root  1739 Jul 26 14:47 masterha_stop
        -r-xr-xr-x. 1 root root  8261 Jul 26 14:46 purge_relay_logs
        -r-xr-xr-x. 1 root root  7525 Jul 26 14:46 save_binary_logs

        至此,MHA manager节点安装完毕,会在/usr/local/bin下生成如上脚本文件
    
    注:如果缺少manager和node包,请到我的网盘下载: https://pan.baidu.com/s/1twuJfJT8DButq5xjx7xMUw 
提取码:cvca 
  • 2.配置mha相关配置(manager服务器上配置)
-   1.创建masterha目录并配置
        [root@localhost package]# mkdir -p /etc/masterha/{conf,logs,work} 
        [root@localhost package]# cat > /etc/masterha/app1.cnf <
  • 3.检测ssh连接和检测mysql同步配置
[root@localhost package]# masterha_check_ssh --conf=/etc/masterha/app1.cnf
Fri Jul 26 15:36:44 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Jul 26 15:36:44 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Jul 26 15:36:44 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Jul 26 15:36:44 2019 - [info] Starting SSH connection tests..
Fri Jul 26 15:36:47 2019 - [debug] 
Fri Jul 26 15:36:45 2019 - [debug]  Connecting via SSH from [email protected](192.168.117.141:22) to [email protected](192.168.117.135:22)..
Fri Jul 26 15:36:45 2019 - [debug]   ok.
Fri Jul 26 15:36:45 2019 - [debug]  Connecting via SSH from [email protected](192.168.117.141:22) to [email protected](192.168.117.142:22)..
Fri Jul 26 15:36:46 2019 - [debug]   ok.
Fri Jul 26 15:36:48 2019 - [debug] 
Fri Jul 26 15:36:44 2019 - [debug]  Connecting via SSH from [email protected](192.168.117.135:22) to [email protected](192.168.117.141:22)..
Fri Jul 26 15:36:46 2019 - [debug]   ok.
Fri Jul 26 15:36:46 2019 - [debug]  Connecting via SSH from [email protected](192.168.117.135:22) to [email protected](192.168.117.142:22)..
Fri Jul 26 15:36:47 2019 - [debug]   ok.
Fri Jul 26 15:36:48 2019 - [debug] 
Fri Jul 26 15:36:45 2019 - [debug]  Connecting via SSH from [email protected](192.168.117.142:22) to [email protected](192.168.117.135:22)..
Fri Jul 26 15:36:46 2019 - [debug]   ok.
Fri Jul 26 15:36:46 2019 - [debug]  Connecting via SSH from [email protected](192.168.117.142:22) to [email protected](192.168.117.141:22)..
Fri Jul 26 15:36:47 2019 - [debug]   ok.
Fri Jul 26 15:36:48 2019 - [info] All SSH connection tests passed successfully.



[root@localhost package]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Fri Jul 26 15:36:51 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Jul 26 15:36:51 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Jul 26 15:36:51 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Jul 26 15:36:51 2019 - [info] MHA::MasterMonitor version 0.56.
Fri Jul 26 15:36:52 2019 - [info] GTID failover mode = 0
Fri Jul 26 15:36:52 2019 - [info] Dead Servers:
Fri Jul 26 15:36:52 2019 - [info] Alive Servers:
Fri Jul 26 15:36:52 2019 - [info]   192.168.117.135(192.168.117.135:3306)
Fri Jul 26 15:36:52 2019 - [info]   192.168.117.141(192.168.117.141:3306)
Fri Jul 26 15:36:52 2019 - [info]   192.168.117.142(192.168.117.142:3306)
Fri Jul 26 15:36:52 2019 - [info] Alive Slaves:
Fri Jul 26 15:36:52 2019 - [info]   192.168.117.141(192.168.117.141:3306)  Version=5.5.60-MariaDB (oldest major version between slaves) log-bin:enabled
Fri Jul 26 15:36:52 2019 - [info]     Replicating from 192.168.117.135(192.168.117.135:3306)
Fri Jul 26 15:36:52 2019 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Jul 26 15:36:52 2019 - [info]   192.168.117.142(192.168.117.142:3306)  Version=5.5.60-MariaDB (oldest major version between slaves) log-bin:enabled
Fri Jul 26 15:36:52 2019 - [info]     Replicating from 192.168.117.135(192.168.117.135:3306)
Fri Jul 26 15:36:52 2019 - [info] Current Alive Master: 192.168.117.135(192.168.117.135:3306)
Fri Jul 26 15:36:52 2019 - [info] Checking slave configurations..
Fri Jul 26 15:36:52 2019 - [info]  read_only=1 is not set on slave 192.168.117.141(192.168.117.141:3306).
Fri Jul 26 15:36:52 2019 - [warning]  relay_log_purge=0 is not set on slave 192.168.117.141(192.168.117.141:3306).
Fri Jul 26 15:36:52 2019 - [info]  read_only=1 is not set on slave 192.168.117.142(192.168.117.142:3306).
Fri Jul 26 15:36:52 2019 - [warning]  relay_log_purge=0 is not set on slave 192.168.117.142(192.168.117.142:3306).
Fri Jul 26 15:36:52 2019 - [info] Checking replication filtering settings..
Fri Jul 26 15:36:52 2019 - [info]  binlog_do_db= , binlog_ignore_db= 
Fri Jul 26 15:36:52 2019 - [info]  Replication filtering check ok.
Fri Jul 26 15:36:52 2019 - [info] GTID (with auto-pos) is not supported
Fri Jul 26 15:36:52 2019 - [info] Starting SSH connection tests..
Fri Jul 26 15:36:56 2019 - [info] All SSH connection tests passed successfully.
Fri Jul 26 15:36:56 2019 - [info] Checking MHA Node version..
Fri Jul 26 15:36:57 2019 - [info]  Version check ok.
Fri Jul 26 15:36:57 2019 - [info] Checking SSH publickey authentication settings on the current master..
Fri Jul 26 15:36:57 2019 - [info] HealthCheck: SSH to 192.168.117.135 is reachable.
Fri Jul 26 15:36:57 2019 - [info] Master MHA Node version is 0.56.
Fri Jul 26 15:36:57 2019 - [info] Checking recovery script configurations on 192.168.117.135(192.168.117.135:3306)..
Fri Jul 26 15:36:57 2019 - [info]   Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql --output_file=/var/tmp/save_binary_logs_test --manager_version=0.56 --start_file=master-bin.000001 
Fri Jul 26 15:36:57 2019 - [info]   Connecting to [email protected](192.168.117.135:22).. 
  Creating /var/tmp if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /var/lib/mysql, up to master-bin.000001
Fri Jul 26 15:36:58 2019 - [info] Binlog setting check done.
Fri Jul 26 15:36:58 2019 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Fri Jul 26 15:36:58 2019 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=192.168.117.141 --slave_ip=192.168.117.141 --slave_port=3306 --workdir=/var/tmp --target_version=5.5.60-MariaDB --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Fri Jul 26 15:36:58 2019 - [info]   Connecting to [email protected](192.168.117.141:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to relay-bin.015011
    Temporary relay log file is /var/lib/mysql/relay-bin.015011
    Testing mysql connection and privileges.. done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Fri Jul 26 15:36:58 2019 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='root' --slave_host=192.168.117.142 --slave_ip=192.168.117.142 --slave_port=3306 --workdir=/var/tmp --target_version=5.5.60-MariaDB --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info  --relay_dir=/var/lib/mysql/  --slave_pass=xxx
Fri Jul 26 15:36:58 2019 - [info]   Connecting to [email protected](192.168.117.142:22).. 
  Checking slave recovery environment settings..
    Opening /var/lib/mysql/relay-log.info ... ok.
    Relay log found at /var/lib/mysql, up to relay-bin.012281
    Temporary relay log file is /var/lib/mysql/relay-bin.012281
    Testing mysql connection and privileges.. done.
    Testing mysqlbinlog output.. done.
    Cleaning up test file(s).. done.
Fri Jul 26 15:36:59 2019 - [info] Slaves settings check done.
Fri Jul 26 15:36:59 2019 - [info] 
192.168.117.135(192.168.117.135:3306) (current master)
 +--192.168.117.141(192.168.117.141:3306)
 +--192.168.117.142(192.168.117.142:3306)

Fri Jul 26 15:36:59 2019 - [info] Checking replication health on 192.168.117.141..
Fri Jul 26 15:36:59 2019 - [info]  ok.
Fri Jul 26 15:36:59 2019 - [info] Checking replication health on 192.168.117.142..
Fri Jul 26 15:36:59 2019 - [info]  ok.
Fri Jul 26 15:36:59 2019 - [warning] master_ip_failover_script is not defined.
Fri Jul 26 15:36:59 2019 - [warning] shutdown_script is not defined.
Fri Jul 26 15:36:59 2019 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.


满足上面的条件下,现在就开始使用开启manager了

  • 4.开启manager
[root@localhost package]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /masterha/app1/manager.log 2>&1 &
[root@localhost package]# masterha_check_status --conf=/etc/masterha/app1.cnf 
app1 (pid:22751) is running(0:PING_OK), master:192.168.117.135

到此为止MHAan执行成功  

-   停止manager
[root@localhost package]# masterha_stop --conf=/etc/masterha/app1.cnf 
Stopped app1 successfully.
[1]+  Exit 1                  nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /masterha/app1/manager.log 2>&1

vip设置

  • vip设置
在/etc/masterha/app1下[server default]添加master_ip_failover_script=/etc/mha/conf/master_ip_failover

[root@localhost package]# cat /etc/masterha/app1.cnf 
[server default]
master_ip_failover_script=/etc/masterha/master_ip_failover
manager_workdir=/etc/masterha/work
manager_log=/etc/masterha/logs/manager.log
master_binlog_dir=/var/lib/mysql
user=root
password=12345678
ssh_user=root
ping_interval=1
repl_user=root
repl_password=12345678

[server1]
hostname=192.168.117.135
port=3306

[server2]
hostname=192.168.117.141
port=3306
candidate_master=1
[server3]
hostname=192.168.117.142
port=3306

创建master_ip_failover脚本
具体脚本请到我的云盘下载:链接:https://pan.baidu.com/s/1DsZPK5y1xa0wf7_TCczLzg 
提取码:dbgv 

停止manager并重启

[root@localhost package]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /masterha/app1/manager.log 2>&1 &
[2] 23858
[root@localhost package]# masterha_check_status --conf=/etc/masterha/app1.cnf 
app1 monitoring program is now on initialization phase(10:INITIALIZING_MONITOR). Wait for a while and try checking again.
[root@localhost package]# masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover
Fri Jul 26 16:43:38 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Jul 26 16:43:38 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Jul 26 16:43:38 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
[2]+  Exit 1                  nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /masterha/app1/manager.log 2>&1
[root@localhost package]# masterha_check_status --conf=/etc/masterha/app1.cnf 
app1 (pid:23498) is running(0:PING_OK), master:192.168.117.135

测试

  • 测试
停掉master服务器(192.167.117.135),观察 mha-maager日志/etc/masterha/logs/manager.log日志

----- Failover Report -----

app1: MySQL Master failover 192.168.117.135(192.168.117.135:3306) to 192.168.117.141(192.168.117.141:3306)

Master 192.168.117.135(192.168.117.135:3306) is down!

Check MHA Manager logs at localhost.localdomain:/etc/masterha/logs/manager.log for details.

Started automated(non-interactive) failover.
Invalidated master IP address on 192.168.117.135(192.168.117.135:3306)
The latest slave 192.168.117.141(192.168.117.141:3306) has all relay logs for recovery.
Selected 192.168.117.141(192.168.117.141:3306) as a new master.
192.168.117.141(192.168.117.141:3306): OK: Applying all logs succeeded.
192.168.117.141(192.168.117.141:3306): OK: Activated master IP address.
192.168.117.142(192.168.117.142:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.117.142: OK: Applying all logs succeeded. Slave started, replicating from mysql_slave1.
192.168.117.141: Resetting slave info succeeded.
Master failover to 192.168.117.141(192.168.117.141:3306) completed successfully.

你可能感兴趣的:(架构与集群,linux,数据库,高可用,集群,mha,mysql,架构)