您的位置:首页 > 运维架构 > 网站架构

mysql高可用 主从MHA

2016-07-06 18:43 441 查看
MHA里有两个角色一个是node节点 一个是manager节点,要实现这个MHA,必须最少要三台数据库服务器,一主多备,即一台充当master,一台充当master的备份机,另外一台是从属机(淘宝也在该基础上进行了改造,目前淘宝TMHA已经支持一主一从),这里实验为了实现更好的效果使用四台机器,需要说明的是一旦主服务器宕机,备份机即开始充当master提供服务,如果主服务器上线也不会再成为master了,因为如果这样数据库的一致性就被改变了。

172.16.57.55 主机

172.16.57.56 从1

172.16.57.57 从2

172.16.57.88 manager

配置ssh无密码登录

#主机:master执行命令
root@bd-qa-mongodb-55:~#ssh-keygen -t rsa
root@bd-qa-mongodb-55:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.56
root@bd-qa-mongodb-55:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.57
root@bd-qa-mongodb-55:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.58

从1
root@bd-qa-oracle-56:~#ssh-keygen -t rsa
root@bd-qa-oracle-56:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.56
root@bd-qa-oracle-56:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.57
root@bd-qa-oracle-56:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.58

从2
root@bd-qa-redis-57:~#ssh-keygen -t rsa
root@bd-qa-redis-57:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.56
root@bd-qa-redis-57:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.57
root@bd-qa-redis-57:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.58

manager
root@bd-qa-vertica-58:~#ssh-keygen -t rsa
root@bd-qa-vertica-58:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.56
root@bd-qa-vertica-58:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.57
root@bd-qa-vertica-58:~#ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.57.58

二、规划mysql
1、安装mysql
#在master、slave01和slave02上安装mysql服务
主库配置

[root@T2 mysql-5.7.9]# cat /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
#skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = ROW
log-slave-updates
expire_logs_days = 30
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

[root@T2 mysql-5.7.9]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

[root@T2 mysql-5.7.9]# service mysqld start
Starting MySQL. SUCCESS!
[root@T2 mysql-5.7.9]# export MYSQL_HOME=/usr/local/mysql
[root@T2 mysql-5.7.9]# export PATH=$PATH:$MYSQL_HOME/bin

从1库配置

[root@T3 mysql-5.7.9]# cat /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
server-id = 11
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
#skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = ROW
log-slave-updates
expire_logs_days = 30
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

[root@T3 mysql-5.7.9]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
[root@T3 mysql-5.7.9]# service mysqld start
Starting MySQL. SUCCESS!
[root@T3 mysql-5.7.9]# export MYSQL_HOME=/usr/local/mysql
[root@T3 mysql-5.7.9]# export PATH=$PATH:$MYSQL_HOME/bin

从2配置

[root@T4 mysql-5.7.9]# cat /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
server-id = 111
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
#skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = ROW
log-slave-updates

expire_logs_days = 30
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

注意这里必须都设置,log-slave-updates 以便切换

[root@T4 mysql-5.7.9]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
[root@T4 mysql-5.7.9]# service mysqld start
Starting MySQL. SUCCESS!
[root@T4 mysql-5.7.9]# export MYSQL_HOME=/usr/local/mysql
[root@T4 mysql-5.7.9]# export PATH=$PATH:$MYSQL_HOME/bin

2、配置master、slave01和slave02之间的主从复制
在MySQL5.6 的Replication配置中,master端同样要开启两个重要的选项,server-id和log-bin,并且选项server-id在全局架构中并且唯一,不能被其它主机使用,这里采用主机ip地址的最后一位充当server-id的值;slave端要开启relay-log,这里我们已经在配置文件配置;
3、在master、slave01上创建主从同步的账号。slave01是备用master,这个也需要建立授权用户。

mysql> set password=password('fafa123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on *.* to dba_user@'%' identified by 'fafa123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

[root@T2 mysql-5.7.9]# mysql -uroot -pfafa123 -e "grant replication slave on *.* to 'backup'@'172.16.57.%' identified by 'backup';flush privileges;"
Warning: Using a password on the command line interface can be insecure.

从1库设置
mysql> set password=password('fafa123');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> grant all on *.* to dba_user@'%' identified by 'fafa123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

[root@T3 mysql-5.7.9]# mysql -uroot -pfafa123 -e "grant replication slave on *.* to 'backup'@'172.16.57.%' identified by 'backup';flush privileges;"
Warning: Using a password on the command line interface can be insecure.

4、在master上执行命令,查看master状态信息
[root@T2 mysql-5.7.9]# mysql -uroot -pfafa123 -e 'show master status;'
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.000002 | 1900 | | | |
+------------------+----------+--------------+------------------+-------------------+
5、在slave01和slave02上执行主从同步
#slave01配置主从
mysql> change master to master_host='172.16.57.55',master_user='backup',master_password='backup',master_port=3306,master_log_file='mysql-bin.000002',master_log_pos=1900;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.57.55
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1900
Relay_Log_File: T3-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
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: 1900
Relay_Log_Space: 524
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: 1
Master_UUID: 8ae37b24-3c1f-11e6-bc4e-5254008cd584
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
1 row in set (0.00 sec)

ERROR:
No query specified

出现此错误是sql不合法原因:
如:select * from abc\G;
或者
select * from abc;;
\G后面不需要再加分号;
正确写法是

mysql> show slave status\G

#slave02配置主从

mysql> set password=password('fafa123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on *.* to dba_user@'%' identified by 'fafa123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

[root@T4 mysql-5.7.9]# mysql -uroot -pfafa123 -e "grant replication slave on *.* to 'backup'@'172.16.57.%' identified by 'backup';flush privileges;"
Warning: Using a password on the command line interface can be insecure.

[root@T4 mysql-5.7.9]# mysql -uroot -pfafa123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.9-log Source distribution

Copyright (c) 2000, 2016, 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> change master to master_host='172.16.57.55',master_user='backup',master_password='backup',master_port=3306,master_log_file='mysql-bin.000002',master_log_pos=1900;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.57.55
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1900
Relay_Log_File: T4-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
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: 1900
Relay_Log_Space: 524
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: 1
Master_UUID: 8ae37b24-3c1f-11e6-bc4e-5254008cd584
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
1 row in set (0.00 sec)

#实验到这里表示主从已经配置完成!
三、规划mha
1、创建mha管理用的复制账号,每台数据库(master、slave01、slave02)上都要创建账号,在这里以其中master为例.。


[root@T2 mysql-5.7.9]# mysql -uroot -pfafa123 -e "grant all privileges on *.* to 'mha_rep'@'172.16.57.%' identified by 'fafa123';flush privileges;"
Warning: Using a password on the command line interface can be insecure.

从1

[root@T3 mysql-5.7.9]# mysql -uroot -pfafa123 -e "grant all privileges on *.* to 'mha_rep'@'172.16.57.%' identified by 'fafa123';flush privileges;"
Warning: Using a password on the command line interface can be insecure.

从2

[root@T4 mysql-5.7.9]# mysql -uroot -pfafa123 -e "grant all privileges on *.* to 'mha_rep'@'172.16.57.%' identified by 'fafa123';flush privileges;"
Warning: Using a password on the command line interface can be insecure.

主库检查

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user;
+-------------+-----------+
| host | user |
+-------------+-----------+
| % | dba_user |
| 172.16.57.% | backup |
| 172.16.57.% | mha_rep |
| localhost | mysql.sys |
| localhost | root |
+-------------+-----------+
5 rows in set (0.00 sec)

2、在3台主机上(master、slave01和slave02)上分别安装mha4mysql-node包,这里以master为例,其它主机同理。


[root@T2 mysql]# yum install perl-DBD-MySQL -y

出现冲突的报错

file /usr/share/mysql/ukrainian/errmsg.sys from install of mysql-libs-5.1.73-7.el6.x86_64 conflicts with file from package MySQL-Cluster-server-gpl-7.4.11-1.el6.x86_64

[root@T2 mysql]# yum remove MySQL-Cluster-server-*

[root@T2 mysql-5.7.9]# rpm -ivh https://downloads.mariadb.com/files/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm

Retrieving https://downloads.mariadb.com/files/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-node ########################################### [100%]

从1

[root@T3 mysql-5.7.9]# yum install perl-DBD-MySQL -y

[root@T3 mysql-5.7.9]# rpm -ivh https://downloads.mariadb.com/files/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm
Retrieving https://downloads.mariadb.com/files/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-node ########################################### [100%]

从2

[root@T4 mysql-5.7.9]# yum install perl-DBD-MySQL -y

[root@T4 mysql-5.7.9]# rpm -ivh https://downloads.mariadb.com/files/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm
Retrieving https://downloads.mariadb.com/files/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-node ########################################### [100%]

3、在manager上安装mha4mysql-manager和mha4mysql-node包
这里是172.16.57.58这台机器
[root@oracle mysql]# yum install perl cpan perl-DBD-MySQL perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Net-Telnet -y
[root@oracle mysql]# rpm -ivh https://downloads.mariadb.com/files/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm 下载不到,直接安装

[root@oracle mysql]# rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
Preparing... ########################################### [100%]
1:mha4mysql-node ########################################### [100%]

[root@manager ~]# wget https://downloads.mariadb.com/files/MHA/mha4mysql-manager-0.56.tar.gz
[root@oracle mysql]# ls
boost_1_59_0 config mha4mysql-manager-0.56.tar.gz mysql-5.7.9
boost_1_59_0.tar.gz data mha4mysql-node-0.54-0.el6.noarch.rpm mysql-5.7.9.tar.gz

[root@oracle mysql]# tar zvxf mha4mysql-manager-0.56.tar.gz

[root@oracle mysql]# cd mha4mysql-manager-0.56
[root@oracle mha4mysql-manager-0.56]# perl Makefile.PL
*** Module::AutoInstall version 1.03
*** Checking for Perl dependencies...
[Core Features]
- DBI ...loaded. (1.609)
- DBD::mysql ...loaded. (4.013)
- Time::HiRes ...missing.
- Config::Tiny ...loaded. (2.12)
- Log::Dispatch ...loaded. (2.26)
- Parallel::ForkManager ...loaded. (0.7.9)
- MHA::NodeConst ...loaded. (0.54)
==> Auto-install the 1 mandatory module(s) from CPAN? [y] y
CPAN is the world-wide archive of perl resources. It consists of about
300 sites that all replicate the same contents around the globe. Many
countries have at least one CPAN site already. The resources found on
CPAN are easily accessible with the CPAN.pm module. If you want to use
CPAN.pm, lots of things have to be configured. Fortunately, most of
them can be determined automatically. If you prefer the automatic
configuration, answer 'yes' below.
If you prefer to enter a dialog instead, you can answer 'no' to this
question and I'll let you configure in small steps one thing after the
other. (Note: you can revisit this dialog anytime later by typing 'o
conf init' at the cpan prompt.)
Would you like me to configure as much as possible automatically? [yes] yes
后面会自动创建
注意此时需要有网安装
[root@oracle mha4mysql-manager-0.56]# make && make install
[root@oracle mha4mysql-manager-0.56]# mkdir -p /usr/local/mha/scripts
[root@oracle mha4mysql-manager-0.56]# cp samples/conf/app1.cnf /usr/local/mha/mha.cnf
[root@oracle mha4mysql-manager-0.56]# cp samples/scripts/* //usr/local/mha/scripts/
4、修改manager端mha的配置文件,如下
解释
user=mha_rep #MHA管理mysql的用户名
password=fafa123 #MHA管理mysql的密码
manager_workdir=/usr/local/mha #MHA的工作目录
manager_log=/usr/local/mha/manager.log #MHA的日志路径
ssh_user=root #免秘钥登陆的用户名
repl_user=backup #主从复制账号,用来在主从之间同步数据
ping_interval=1 #ping间隔时间,用来检查master是否正常
no_master=1 #设置na_master=1,使服务器不能成为master

[root@oracle ~]# cat /usr/local/mha/mha.cnf
[server default]
user=mha_rep
password=fafa123
manager_workdir=/usr/local/mha
manager_log=/usr/local/mha/manager.log
ssh_user=root
repl_user=backup
ping_interval=1
[server1]
hostname=172.16.57.55
master_binlog_dir=/data/mysql/
candidate_master=1 #master宕机后,优先启用这台作为master

[server2]
hostname=172.16.57.56
master_binlog_dir=/data/mysql/
candidate_master=1

[server3]
hostname=172.16.57.57
master_binlog_dir=/data/mysql/
candidate_master=1

5、检查ssh是否畅通

[root@oracle mha4mysql-manager-0.56]# pwd
/opt/app/mysql/mha4mysql-manager-0.56
[root@oracle mha4mysql-manager-0.56]# masterha_check_ssh --conf=/usr/local/mha/mha.cnf
Mon Jun 27 15:16:19 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Jun 27 15:16:19 2016 - [info] Reading application default configurations from /usr/local/mha/mha.cnf..
Mon Jun 27 15:16:19 2016 - [info] Reading server configurations from /usr/local/mha/mha.cnf..
Mon Jun 27 15:16:19 2016 - [info] Starting SSH connection tests..
Mon Jun 27 15:16:20 2016 - [debug]
Mon Jun 27 15:16:19 2016 - [debug] Connecting via SSH from root@172.16.57.55(172.16.57.55:22) to root@172.16.57.56(172.16.57.56:22)..
Mon Jun 27 15:16:19 2016 - [debug] ok.
Mon Jun 27 15:16:19 2016 - [debug] Connecting via SSH from root@172.16.57.55(172.16.57.55:22) to root@172.16.57.57(172.16.57.57:22)..
Mon Jun 27 15:16:19 2016 - [debug] ok.
Mon Jun 27 15:16:20 2016 - [debug]
Mon Jun 27 15:16:19 2016 - [debug] Connecting via SSH from root@172.16.57.56(172.16.57.56:22) to root@172.16.57.55(172.16.57.55:22)..
Mon Jun 27 15:16:20 2016 - [debug] ok.
Mon Jun 27 15:16:20 2016 - [debug] Connecting via SSH from root@172.16.57.56(172.16.57.56:22) to root@172.16.57.57(172.16.57.57:22)..
Mon Jun 27 15:16:20 2016 - [debug] ok.
Mon Jun 27 15:16:21 2016 - [debug]
Mon Jun 27 15:16:20 2016 - [debug] Connecting via SSH from root@172.16.57.57(172.16.57.57:22) to root@172.16.57.55(172.16.57.55:22)..
Mon Jun 27 15:16:20 2016 - [debug] ok.
Mon Jun 27 15:16:20 2016 - [debug] Connecting via SSH from root@172.16.57.57(172.16.57.57:22) to root@172.16.57.56(172.16.57.56:22)..
Mon Jun 27 15:16:21 2016 - [debug] ok.
Mon Jun 27 15:16:21 2016 - [info] All SSH connection tests passed successfully.

#如果得到以上结果,表明主机之间ssh互信是畅通的
6、masterha_check_repl工具检查mysql主从复制是否成功,masterha_check_repl是一个很好的工具,以后可以用在监控脚本中

[root@oracle mha4mysql-manager-0.56]# masterha_check_repl --conf=/usr/local/mha/mha.cnf
Mon Jun 27 15:52:31 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Jun 27 15:52:31 2016 - [info] Reading application default configurations from /usr/local/mha/mha.cnf..
Mon Jun 27 15:52:31 2016 - [info] Reading server configurations from /usr/local/mha/mha.cnf..
Mon Jun 27 15:52:31 2016 - [info] MHA::MasterMonitor version 0.56.
Mon Jun 27 15:52:31 2016 - [info] Dead Servers:
Mon Jun 27 15:52:31 2016 - [info] Alive Servers:
Mon Jun 27 15:52:31 2016 - [info] 172.16.57.55(172.16.57.55:3306)
Mon Jun 27 15:52:31 2016 - [info] 172.16.57.56(172.16.57.56:3306)
Mon Jun 27 15:52:31 2016 - [info] 172.16.57.57(172.16.57.57:3306)
Mon Jun 27 15:52:31 2016 - [info] Alive Slaves:
Mon Jun 27 15:52:31 2016 - [info] 172.16.57.56(172.16.57.56:3306) Version=5.7.9-log (oldest major version between slaves) log-bin:enabled
Mon Jun 27 15:52:31 2016 - [info] Replicating from 172.16.57.55(172.16.57.55:3306)
Mon Jun 27 15:52:31 2016 - [info] Primary candidate for the new Master (candidate_master is set)
Mon Jun 27 15:52:31 2016 - [info] 172.16.57.57(172.16.57.57:3306) Version=5.7.9-log (oldest major version between slaves) log-bin:enabled
Mon Jun 27 15:52:31 2016 - [info] Replicating from 172.16.57.55(172.16.57.55:3306)
Mon Jun 27 15:52:31 2016 - [info] Primary candidate for the new Master (candidate_master is set)
Mon Jun 27 15:52:31 2016 - [info] Current Alive Master: 172.16.57.55(172.16.57.55:3306)
Mon Jun 27 15:52:31 2016 - [info] Checking slave configurations..
Mon Jun 27 15:52:31 2016 - [info] read_only=1 is not set on slave 172.16.57.56(172.16.57.56:3306).
Mon Jun 27 15:52:31 2016 - [warning] relay_log_purge=0 is not set on slave 172.16.57.56(172.16.57.56:3306).
Mon Jun 27 15:52:31 2016 - [info] read_only=1 is not set on slave 172.16.57.57(172.16.57.57:3306).
Mon Jun 27 15:52:31 2016 - [warning] relay_log_purge=0 is not set on slave 172.16.57.57(172.16.57.57:3306).
Mon Jun 27 15:52:31 2016 - [info] Checking replication filtering settings..
Mon Jun 27 15:52:31 2016 - [info] binlog_do_db= , binlog_ignore_db=
Mon Jun 27 15:52:31 2016 - [info] Replication filtering check ok.
Mon Jun 27 15:52:31 2016 - [info] Starting SSH connection tests..
Mon Jun 27 15:52:33 2016 - [info] All SSH connection tests passed successfully.
Mon Jun 27 15:52:33 2016 - [info] Checking MHA Node version..
Mon Jun 27 15:52:34 2016 - [info] Version check ok.
Mon Jun 27 15:52:34 2016 - [info] Checking SSH publickey authentication settings on the current master..
Mon Jun 27 15:52:34 2016 - [info] HealthCheck: SSH to 172.16.57.55 is reachable.
Mon Jun 27 15:52:34 2016 - [info] Master MHA Node version is 0.54.
Mon Jun 27 15:52:34 2016 - [info] Checking recovery script configurations on the current master..
Mon Jun 27 15:52:34 2016 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/data/mysql/ --output_file=/var/tmp/save_binary_logs_test --manager_version=0.56 --start_file=mysql-bin.000003
Mon Jun 27 15:52:34 2016 - [info] Connecting to root@172.16.57.55(172.16.57.55)..
Creating /var/tmp if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /data/mysql/, up to mysql-bin.000003
Mon Jun 27 15:52:34 2016 - [info] Master setting check done.
Mon Jun 27 15:52:34 2016 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Mon Jun 27 15:52:34 2016 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha_rep' --slave_host=172.16.57.56 --slave_ip=172.16.57.56 --slave_port=3306 --workdir=/var/tmp --target_version=5.7.9-log --manager_version=0.56 --relay_log_info=/data/mysql/relay-log.info --relay_dir=/data/mysql/ --slave_pass=xxx
Mon Jun 27 15:52:34 2016 - [info] Connecting to root@172.16.57.56(172.16.57.56:22)..
Checking slave recovery environment settings..
Opening /data/mysql/relay-log.info ... ok.
Relay log found at /data/mysql, up to T3-relay-bin.000007
Temporary relay log file is /data/mysql/T3-relay-bin.000007
Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Mon Jun 27 15:52:34 2016 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha_rep' --slave_host=172.16.57.57 --slave_ip=172.16.57.57 --slave_port=3306 --workdir=/var/tmp --target_version=5.7.9-log --manager_version=0.56 --relay_log_info=/data/mysql/relay-log.info --relay_dir=/data/mysql/ --slave_pass=xxx
Mon Jun 27 15:52:34 2016 - [info] Connecting to root@172.16.57.57(172.16.57.57:22)..
Checking slave recovery environment settings..
Opening /data/mysql/relay-log.info ... ok.
Relay log found at /data/mysql, up to T4-relay-bin.000012
Temporary relay log file is /data/mysql/T4-relay-bin.000012
Testing mysql connection and privileges..Warning: Using a password on the command line interface can be insecure.
done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Mon Jun 27 15:52:35 2016 - [info] Slaves settings check done.
Mon Jun 27 15:52:35 2016 - [info]
172.16.57.55 (current master)
+--172.16.57.56
+--172.16.57.57

Mon Jun 27 15:52:35 2016 - [info] Checking replication health on 172.16.57.56..
Mon Jun 27 15:52:35 2016 - [info] ok.
Mon Jun 27 15:52:35 2016 - [info] Checking replication health on 172.16.57.57..
Mon Jun 27 15:52:35 2016 - [info] ok.
Mon Jun 27 15:52:35 2016 - [warning] master_ip_failover_script is not defined.
Mon Jun 27 15:52:35 2016 - [warning] shutdown_script is not defined.
Mon Jun 27 15:52:35 2016 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

四、mha实验模拟
1、在每次做mha实验的时候,我们都最好先执行如下命令做检测
[root@manager ~]# masterha_check_ssh --conf=/usr/local/mha/mha.cnf
[root@manager ~]# masterha_check_repl --conf=/usr/local/mha/mha.cnf
#确定两条命令的返回结果都是无异常的,然后启动mha服务
2、在manager端启动mha服务并时刻监控日志文件的输出变化

[root@oracle mha4mysql-manager-0.56]# nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 &
[1] 17213
[root@oracle mha4mysql-manager-0.56]# ps -ef |grep masterha |grep -v 'grep'
root 17213 24725 0 15:56 pts/3 00:00:00 perl /usr/local/bin/masterha_manager --conf=/usr/local/mha/mha.cnf

3、测试master宕机后,时候会自动切换
#测试前查看slave01,slave02的主从同步情况
#slave01

[root@T3 mysql-5.7.9]# mysql -uroot -pfafa123 -e 'show slave status\G' |egrep 'Slave_IO_Running:|Slave_SQL_Running:'
Warning: Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

#slave02

[root@T4 network-scripts]# mysql -uroot -pfafa123 -e 'show slave status\G' |egrep 'Slave_IO_Running:|Slave_SQL_Running:'
Warning: Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

#停止master的mysql服务

[root@T2 mysql-5.7.9]# service mysqld stop
Shutting down MySQL............ SUCCESS!

#我们查看slave02的主从同步信息

[root@T4 network-scripts]# mysql -uroot -pfafa123 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:'
Warning: Using a password on the command line interface can be insecure.
Master_Host: 172.16.57.56
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

4、恢复master服务
#删除故障转移文件,此步以后才能重新转换回来,即172.16.57.58节点

[root@oracle mha4mysql-manager-0.56]# rm -rf /usr/local/mha/mha.failover.complete

#重启mysql服务,即172.16.57.55节点

[root@T2 mysql-5.7.9]# service mysqld start
Starting MySQL. SUCCESS!

此时启动它不在集群里面
#在manager的日子文件中找到主从同步的sql语句,即172.16.57.58节点

[root@oracle mha4mysql-manager-0.56]# grep MASTER_HOST /usr/local/mha/manager.log
Mon Jun 27 16:00:12 2016 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='172.16.57.56', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=154, MASTER_USER='backup', MASTER_PASSWORD='xxx';

#在master上启动主从同步,密码为backup,即172.16.57.56节点,依据上面日志记录的检查点

mysql> change master to master_host='172.16.57.56',master_user='backup',master_password='backup',master_port=3306,master_log_file='mysql-bin.000004', MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

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

#在master和slave02上执行,检查主从同步是否都正常,这里以master为例,slave02同理

[root@T4 network-scripts]# mysql -uroot -pfafa123 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:'
Warning: Using a password on the command line interface can be insecure.
Master_Host: 172.16.57.56
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

然而在老主库报错

[root@T2 mysql-5.7.9]# mysql -uroot -pfafa123 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:'
Warning: Using a password on the command line interface can be insecure.
Master_Host: 172.16.57.55
Slave_IO_Running: No
Slave_SQL_Running: Yes

2016-06-27T08:23:15.600018Z 8 [ERROR] Slave I/O for channel '': Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it). Error_code: 1593

检查

mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)

没发现问题
新主库查看是否是/data/mysql/auto.cnf,相同导致,即172.16.57.56机器上

mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 111 | | 3306 | 11 | e34ec4dd-3c25-11e6-ba9a-52540034e4ef |
+-----------+------+------+-----------+--------------------------------------+
1 row in set (0.00 sec)

mysql> show variables like 'server_uuid';
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | 69577b5a-3c25-11e6-ae06-5254006b272f |
+---------------+--------------------------------------+
1 row in set (0.00 sec)

发现不能识别172.16.57.55的从机器
原理是上面改变主的时候,指定旧的主了,要指定新的主即172.16.57.56

[root@T2 mysql]# mysql -uroot -pfafa123 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:'
Warning: Using a password on the command line interface can be insecure.
Master_Host: 172.16.57.56
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

最后正确
5、再次启动MHA的manager服务,并停止slave01

[root@oracle mha4mysql-manager-0.56]# nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 &
[1] 17593

#关闭slave01的mysql服务

[root@T3 mysql-5.7.9]# service mysqld stop
Shutting down MySQL............ SUCCESS!

manager节点查看情况

[root@oracle mha4mysql-manager-0.56]# tail -f /usr/local/mha/manager.log

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

#在slave02上查看主从同步情况

[root@T4 network-scripts]# mysql -uroot -pfafa123 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:'
Warning: Using a password on the command line interface can be insecure.
Master_Host: 172.16.57.55
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

可以看到主节点成功切换到172.16.57.55节点上了
下面需要对slave01服务进行恢复,这种切换是一个曲折的过程
6、恢复slave01服务
#删除故障转移文件,manager节点进行

[root@oracle mha4mysql-manager-0.56]# rm -rf /usr/local/mha/mha.failover.complete

从1节点重启服务

[root@T3 mysql-5.7.9]# service mysqld start
Starting MySQL. SUCCESS!

#在manager的日子文件中找到主从同步的sql语句,即172.16.57.58

[root@oracle mha4mysql-manager-0.56]# grep MASTER_HOST /usr/local/mha/manager.log
Mon Jun 27 16:00:12 2016 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='172.16.57.56', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=154, MASTER_USER='backup', MASTER_PASSWORD='xxx';
Mon Jun 27 16:42:48 2016 - [info] All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='172.16.57.55', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000007', MASTER_LOG_POS=154, MASTER_USER='backup', MASTER_PASSWORD='xxx';

#在slave01上启动主从同步,密码为backup,注意此时的主节点是172.16.57.55

mysql> change master to master_host='172.16.57.55',master_user='backup',master_password='backup',master_port=3306,master_log_file='mysql-bin.000007', MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

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

mysql> exit
Bye

#在slave01和slave02上执行,检查主从同步是否都正常,这里以slave01为例,slave02同理。
从1

[root@T3 mysql-5.7.9]# mysql -uroot -pfafa123 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:'
Warning: Using a password on the command line interface can be insecure.
Master_Host: 172.16.57.55
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

从2

[root@T4 network-scripts]# mysql -uroot -pfafa123 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:'
Warning: Using a password on the command line interface can be insecure.
Master_Host: 172.16.57.55
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

7、再次启动MHA的manager服务,在172.16.57.58

[root@oracle mha4mysql-manager-0.56]# nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 &
[1] 17662

五、通过vip实现mysql的高可用
1、修改/usr/local/mha/mha.cnf
[server default]
user=mha_rep
password=123456
manager_workdir=/usr/local/mha
manager_log=/usr/local/mha/manager.log
master_ip_failover_script=/usr/local/mha/scripts/master_ip_failover #添加管理vip的脚本
ssh_user=root
repl_user=backup
repl_password=backup
ping_interval=1

[server1]
hostname=192.168.253.241
master_binlog_dir=/data/mysql/
candidate_master=1

[server2]
hostname=192.168.253.242
master_binlog_dir=/data/mysql/
candidate_master=1

[server3]
hostname=192.168.253.243
master_binlog_dir=/data/mysql/
no_master=1
2、修改脚本/usr/local/mha/scripts/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';

use Getopt::Long;

my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);

my $vip = '192.168.253.240'; #vip地址
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip"; #绑定在指定的网卡上面
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";

GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);

exit &main();

sub main {

print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

if ( $command eq "stop" || $command eq "stopssh" ) {

my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {

my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}

sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port
--new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
3、模拟故障进行切换
#停止master的mysql服务
[root@master ~]# service mysqld stop
Shutting down MySQL... SUCCESS!
#查看slave02的同步信息
[root@slave02 ~]# mysql -uroot -p123456 -e 'show slave status\G' |egrep 'Master_Host|Slave_IO_Running:|Slave_SQL_Running:'
Warning: Using a password on the command line interface can be insecure.
Master_Host: 192.168.253.242
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#查看slave01的IP信息
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether a2:28:26:4d:72:f6 brd ff:ff:ff:ff:ff:ff
inet 192.168.253.242/24 brd 192.168.253.255 scope global eth0
#这里能看到vip已自动添加
inet 192.168.253.240/24 brd 192.168.253.255 scope global secondary eth0:1
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
link/ether 56:17:7e:16:50:3c brd ff:ff:ff:ff:ff:ff
4、恢复master的mysql服务同开始恢复方法一样。

六.MHA日常维护命令
1.查看ssh登陆是否成功
masterha_check_ssh --conf=/usr/local/mha/mha.cnf
2.查看复制是否建立好
masterha_check_repl --conf=/usr/local/mha/mha.cnf
3.启动mha
nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 &
4.检查启动的状态
masterha_check_status --conf=/usr/local/mha/mha.cnf
5.停止mha
masterha_stop masterha_check_status --conf=/usr/local/mha/mha.cnf
6.failover后下次重启
#每次failover切换后会在管理目录生成文件app1.failover.complete ,下次在切换的时候会发现有这个文件导致切换不成功,需要手动清理掉。
rm -rf /usr/local/mha/mha.failover.complete
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: