设为首页 收藏本站
查看: 867|回复: 0

[经验分享] Centos 6.5 安装配置Mysql MHA

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-2-24 08:16:32 | 显示全部楼层 |阅读模式
MHA是什么?
MHA是由日本Mysql专家用Perl写的一套Mysql故障切换方案,来保障数据库的高可用性,它的功能是能在0-30s之内实现主Mysql故障转移(failover),MHA故障转移可以很好的帮我们解决从库数据的一致性问题,同时最大化挽回故障发生后数据的一致性。
MHA里有两个角色一个是node节点 一个是manager节点,要实现这个MHA,必须最少要三台数据库服务器,一主多备,即一台充当master,一台充当master的备份机,另外一台是从属机,这里实验为了实现更好的效果使用四台机器,需要说明的是一旦主服务器宕机,备份机即开始充当master提供服务,如果主服务器上线也不会再成为master了,因为如果这样数据库的一致性就被改变了。
环境介绍:
操作系统版本:CentOS-6.5-x86_64-minimal.iso
192.168.253.241    master       主库
192.168.253.242    slave01       从库01+备库
192.168.253.243    slave02       从库02
192.168.253.244    manager    集群管理
架构图:
QQ截图20160224081609.jpg
一、环境初始化
1、修改每台主机名

1
2
3
4
192.168.253.241    master
192.168.253.242    slave01
192.168.253.243    slave02
192.168.253.244    manager



2、主机名解析
#每台服务器执行修改主机名解析

1
2
3
4
5
echo '''
192.168.253.241    master
192.168.253.242    slave01
192.168.253.243    slave02
192.168.253.244    manager''' >>/etc/hosts



3、ssh无密码登录

#主机:master执行命令
1
2
3
4
[iyunv@master ~]# ssh-keygen -t rsa
[iyunv@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager
[iyunv@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave01
[iyunv@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave02



#主机:slave01执行命令

1
2
3
4
[iyunv@slave01 ~]# ssh-keygen -t rsa
[iyunv@slave01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager
[iyunv@slave01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master
[iyunv@slave01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave02



#主机: slave02执行命令

1
2
3
4
[iyunv@slave02 ~]# ssh-keygen -t rsa
[iyunv@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@manager
[iyunv@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master
[iyunv@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave01



#主机:manager执行命令

1
2
3
4
[iyunv@manager ~]# ssh-keygen -t rsa
[iyunv@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master
[iyunv@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave01
[iyunv@manager ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@slave02



二、规划mysql
1、安装mysql
#在master、slave01和slave02上安装mysql服务,具体安装方法不介绍了。网上很多资料,去百度吧。

#master配置文件/usr/local/lnmp/mysql/etc/my.cnf 核心配置如下:

1
2
3
4
5
6
7
8
basedir = /usr/local/lnmp/mysql
datadir = /data/mysql
port = 3306
server_id = 241
socket = /tmp/mysql.sock
log-bin=mysql-bin
log-slave-updates
expire_logs_days = 10



#slave01配置文件/usr/local/lnmp/mysql/etc/my.cnf 核心配置如下:

1
2
3
4
5
6
7
8
basedir = /usr/local/lnmp/mysql
datadir = /data/mysql
port = 3306
server_id = 242
socket = /tmp/mysql.sock
log-bin=mysql-bin
log-slave-updates
expire_logs_days = 10



#slave02配置文件/usr/local/lnmp/mysql/etc/my.cnf 核心配置如下:

1
2
3
4
5
6
7
8
9
basedir = /usr/local/lnmp/mysql
datadir = /data/mysql
port = 3306
server_id = 243
socket = /tmp/mysql.sock
log-bin=mysql-bin
log-slave-updates
expire_logs_days = 10
read_only = 1



2、配置master、slave01和slave02之间的主从复制
在MySQL5.6 的Replication配置中,master端同样要开启两个重要的选项,server-id和log-bin,并且选项server-id在全局架构中并且唯一,不能被其它主机使用,这里采用主机ip地址的最后一位充当server-id的值;slave端要开启relay-log;

#主机: master执行命令

1
2
3
[iyunv@master ~]# egrep "log-bin|server_id" /usr/local/lnmp/mysql/etc/my.cnf
server_id = 241
log-bin=mysql-bin



#主机: slave01执行命令
1
2
3
[iyunv@slave01 ~]# egrep "log-bin|server_id" /usr/local/lnmp/mysql/etc/my.cnf
server_id = 242
log-bin=mysql-bin



#主机: slave02执行命令
1
2
3
[iyunv@slave02 ~]# egrep "log-bin|server_id" /usr/local/lnmp/mysql/etc/my.cnf
server_id = 243
log-bin=mysql-bin



3、在master、slave01上创建主从同步的账号。slave01是备用master,这个也需要建立授权用户。

1
2
[iyunv@master ~]# mysql -uroot -p123456 -e "grant replication slave on *.* to 'backup'@'192.168.253.%' identified by 'backup';flush privileges;"
[iyunv@slave01 ~]# mysql -uroot -p123456 -e "grant replication slave on *.* to 'backup'@'192.168.253.%' identified by 'backup';flush privileges;"



4、在master上执行命令,查看master状态信息

1
2
3
4
5
6
7
[iyunv@master ~]# mysql -uroot -p123456 -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.000004 |      411 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+



5、在slave01和slave02上执行主从同步
#slave01配置主从
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[iyunv@slave01 ~]# mysql -uroot -p123456
mysql>    change master to
    ->    master_host='192.168.253.241',master_user='backup',master_password='backup',master_port=3306,master_log_file='mysql-bin.000004',master_log_pos=411;
Query OK, 0 rows affected, 2 warnings (0.56 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.253.241
                  Master_User: backup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 411
               Relay_Log_File: slave01-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: 411
              Relay_Log_Space: 458
              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: 241
                  Master_UUID: cb4af7bc-d600-11e5-8101-26c537b62ad9
             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 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.00 sec)



#slave02配置主从
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
[iyunv@slave02 ~]# mysql -uroot -p123456
mysql>    change master to
    ->    master_host='192.168.253.241',master_user='backup',master_password='backup',master_port=3306,master_log_file='mysql-bin.000004',master_log_pos=411;
Query OK, 0 rows affected, 2 warnings (0.56 sec)

mysql> start slave;
Query OK, 0 rows affected (0.05 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.253.241
                  Master_User: backup
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 411
               Relay_Log_File: slave01-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: 411
              Relay_Log_Space: 458
              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: 241
                  Master_UUID: cb4af7bc-d600-11e5-8101-26c537b62ad9
             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 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.00 sec)



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

1
2
3
4
5
6
7
8
9
10
[iyunv@master ~]# mysql -uroot -p123456 -e "grant all privileges on *.* to 'mha_rep'@'192.168.253.%' identified by '123456';flush privileges;"
mysql> select host,user from user;
+---------------+---------+
| host          | user    |
+---------------+---------+
| 192.168.253.% | backup  |
| 192.168.253.% | mha_rep |
| localhost     | root    |
+---------------+---------+
3 rows in set (0.00 sec)



2、在3台主机上(master、slave01和slave02)上分别安装mha4mysql-node包,这里以master为例,其它主机同理。
1
2
[iyunv@master ~]# yum install perl-DBD-MySQL -y
[iyunv@master ~]# rpm -ivh https://downloads.mariadb.com/fi ... 54-0.el6.noarch.rpm



3、在manager上安装mha4mysql-manager和mha4mysql-node包
1
2
3
4
5
6
7
8
9
10
[iyunv@manager ~]# yum install perl cpan perl-DBD-MySQL perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Net-Telnet -y
[iyunv@manager ~]# rpm -ivh https://downloads.mariadb.com/fi ... 54-0.el6.noarch.rpm
[iyunv@manager ~]# wget https://downloads.mariadb.com/fi ... manager-0.56.tar.gz
[iyunv@manager ~]# tar zvxf mha4mysql-manager-0.56.tar.gz
[iyunv@manager ~]# cd mha4mysql-manager-0.56
[iyunv@manager ~]# perl Makefile.PL
[iyunv@manager mha4mysql-manager-0.56]# make && make install
[iyunv@manager mha4mysql-manager-0.56]# mkdir -p /usr/local/mha/scripts
[iyunv@manager mha4mysql-manager-0.56]# cp samples/conf/app1.cnf /usr/local/mha/mha.cnf
[iyunv@manager mha4mysql-manager-0.56]# cp samples/scripts/* //usr/local/mha/scripts/



4、修改manager端mha的配置文件,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[iyunv@manager ~]# cat /usr/local/mha/mha.cnf
[server default]
user=mha_rep                                    #MHA管理mysql的用户名
password=123456                                 #MHA管理mysql的密码
manager_workdir=/usr/local/mha                  #MHA的工作目录
manager_log=/usr/local/mha/manager.log          #MHA的日志路径
ssh_user=root                                   #免秘钥登陆的用户名
repl_user=backup                                #主从复制账号,用来在主从之间同步数据
repl_password=backup
ping_interval=1                                 #ping间隔时间,用来检查master是否正常

[server1]
hostname=192.168.253.241
master_binlog_dir=/data/mysql/
candidate_master=1                              #master宕机后,优先启用这台作为master

[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                                     #设置na_master=1,使服务器不能成为master



5、检查ssh是否畅通
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[iyunv@manager mha4mysql-manager-0.56]# masterha_check_ssh --conf=/usr/local/mha/mha.cnf
Mon Feb 22 10:28:05 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Feb 22 10:28:05 2016 - [info] Reading application default configurations from /usr/local/mha/mha.cnf..
Mon Feb 22 10:28:05 2016 - [info] Reading server configurations from /usr/local/mha/mha.cnf..
Mon Feb 22 10:28:05 2016 - [info] Starting SSH connection tests..
Mon Feb 22 10:28:06 2016 - [debug]
Mon Feb 22 10:28:05 2016 - [debug]  Connecting via SSH from root@192.168.253.241(192.168.253.241:22) to root@192.168.253.242(192.168.253.242:22)..
Mon Feb 22 10:28:06 2016 - [debug]   ok.
Mon Feb 22 10:28:06 2016 - [debug]  Connecting via SSH from root@192.168.253.241(192.168.253.241:22) to root@192.168.253.243(192.168.253.243:22)..
Mon Feb 22 10:28:06 2016 - [debug]   ok.
Mon Feb 22 10:28:07 2016 - [debug]
Mon Feb 22 10:28:06 2016 - [debug]  Connecting via SSH from root@192.168.253.242(192.168.253.242:22) to root@192.168.253.241(192.168.253.241:22)..
Mon Feb 22 10:28:06 2016 - [debug]   ok.
Mon Feb 22 10:28:06 2016 - [debug]  Connecting via SSH from root@192.168.253.242(192.168.253.242:22) to root@192.168.253.243(192.168.253.243:22)..
Mon Feb 22 10:28:07 2016 - [debug]   ok.
Mon Feb 22 10:28:07 2016 - [debug]
Mon Feb 22 10:28:06 2016 - [debug]  Connecting via SSH from root@192.168.253.243(192.168.253.243:22) to root@192.168.253.241(192.168.253.241:22)..
Mon Feb 22 10:28:07 2016 - [debug]   ok.
Mon Feb 22 10:28:07 2016 - [debug]  Connecting via SSH from root@192.168.253.243(192.168.253.243:22) to root@192.168.253.242(192.168.253.242:22)..
Mon Feb 22 10:28:07 2016 - [debug]   ok.
Mon Feb 22 10:28:07 2016 - [info] All SSH connection tests passed successfully.



#如果得到以上结果,表明主机之间ssh互信是畅通的
6、masterha_check_repl工具检查mysql主从复制是否成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
[iyunv@manager mha4mysql-manager-0.56]#  masterha_check_repl --conf=/usr/local/mha/mha.cnf
Mon Feb 22 10:30:49 2016 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Feb 22 10:30:49 2016 - [info] Reading application default configurations from /usr/local/mha/mha.cnf..
Mon Feb 22 10:30:49 2016 - [info] Reading server configurations from /usr/local/mha/mha.cnf..
Mon Feb 22 10:30:49 2016 - [info] MHA::MasterMonitor version 0.56.
Mon Feb 22 10:30:50 2016 - [info] Dead Servers:
Mon Feb 22 10:30:50 2016 - [info] Alive Servers:
Mon Feb 22 10:30:50 2016 - [info]   192.168.253.241(192.168.253.241:3306)
Mon Feb 22 10:30:50 2016 - [info]   192.168.253.242(192.168.253.242:3306)
Mon Feb 22 10:30:50 2016 - [info]   192.168.253.243(192.168.253.243:3306)
Mon Feb 22 10:30:50 2016 - [info] Alive Slaves:
Mon Feb 22 10:30:50 2016 - [info]   192.168.253.242(192.168.253.242:3306)  Version=5.6.15-log (oldest major version between slaves) log-bin:enabled
Mon Feb 22 10:30:50 2016 - [info]     Replicating from 192.168.253.241(192.168.253.241:3306)
Mon Feb 22 10:30:50 2016 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Feb 22 10:30:50 2016 - [info]   192.168.253.243(192.168.253.243:3306)  Version=5.6.15-log (oldest major version between slaves) log-bin:enabled
Mon Feb 22 10:30:50 2016 - [info]     Replicating from 192.168.253.241(192.168.253.241:3306)
Mon Feb 22 10:30:50 2016 - [info]     Not candidate for the new Master (no_master is set)
Mon Feb 22 10:30:50 2016 - [info] Current Alive Master: 192.168.253.241(192.168.253.241:3306)
Mon Feb 22 10:30:50 2016 - [info] Checking slave configurations..
Mon Feb 22 10:30:50 2016 - [info]  read_only=1 is not set on slave 192.168.253.242(192.168.253.242:3306).
Mon Feb 22 10:30:50 2016 - [warning]  relay_log_purge=0 is not set on slave 192.168.253.242(192.168.253.242:3306).
Mon Feb 22 10:30:50 2016 - [warning]  relay_log_purge=0 is not set on slave 192.168.253.243(192.168.253.243:3306).
Mon Feb 22 10:30:50 2016 - [info] Checking replication filtering settings..
Mon Feb 22 10:30:50 2016 - [info]  binlog_do_db= , binlog_ignore_db=
Mon Feb 22 10:30:50 2016 - [info]  Replication filtering check ok.
Mon Feb 22 10:30:50 2016 - [info] Starting SSH connection tests..
Mon Feb 22 10:30:51 2016 - [info] All SSH connection tests passed successfully.
Mon Feb 22 10:30:51 2016 - [info] Checking MHA Node version..
Mon Feb 22 10:30:53 2016 - [info]  Version check ok.
Mon Feb 22 10:30:53 2016 - [info] Checking SSH publickey authentication settings on the current master..
Mon Feb 22 10:30:53 2016 - [info] HealthCheck: SSH to 192.168.253.241 is reachable.
Mon Feb 22 10:30:53 2016 - [info] Master MHA Node version is 0.54.
Mon Feb 22 10:30:53 2016 - [info] Checking recovery script configurations on the current master..
Mon Feb 22 10:30:53 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.000004
Mon Feb 22 10:30:53 2016 - [info]   Connecting to root@192.168.253.241(192.168.253.241)..
  Creating /var/tmp if not exists..    ok.
  Checking output directory is accessible or not..
   ok.
  Binlog found at /data/mysql/, up to mysql-bin.000004
Mon Feb 22 10:30:54 2016 - [info] Master setting check done.
Mon Feb 22 10:30:54 2016 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Mon Feb 22 10:30:54 2016 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha_rep' --slave_host=192.168.253.242 --slave_ip=192.168.253.242 --slave_port=3306 --workdir=/var/tmp --target_version=5.6.15-log --manager_version=0.56 --relay_log_info=/data/mysql/relay-log.info  --relay_dir=/data/mysql/  --slave_pass=xxx
Mon Feb 22 10:30:54 2016 - [info]   Connecting to root@192.168.253.242(192.168.253.242:22)..
  Checking slave recovery environment settings..
    Opening /data/mysql/relay-log.info ... ok.
    Relay log found at /data/mysql, up to slave01-relay-bin.000002
    Temporary relay log file is /data/mysql/slave01-relay-bin.000002
    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 Feb 22 10:30:55 2016 - [info]   Executing command : apply_diff_relay_logs --command=test --slave_user='mha_rep' --slave_host=192.168.253.243 --slave_ip=192.168.253.243 --slave_port=3306 --workdir=/var/tmp --target_version=5.6.15-log --manager_version=0.56 --relay_log_info=/data/mysql/relay-log.info  --relay_dir=/data/mysql/  --slave_pass=xxx
Mon Feb 22 10:30:55 2016 - [info]   Connecting to root@192.168.253.243(192.168.253.243:22)..
  Checking slave recovery environment settings..
    Opening /data/mysql/relay-log.info ... ok.
    Relay log found at /data/mysql, up to slave02-relay-bin.000002
    Temporary relay log file is /data/mysql/slave02-relay-bin.000002
    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 Feb 22 10:30:57 2016 - [info] Slaves settings check done.
Mon Feb 22 10:30:57 2016 - [info]
192.168.253.241 (current master)
+--192.168.253.242
+--192.168.253.243

Mon Feb 22 10:30:57 2016 - [info] Checking replication health on 192.168.253.242..
Mon Feb 22 10:30:57 2016 - [info]  ok.
Mon Feb 22 10:30:57 2016 - [info] Checking replication health on 192.168.253.243..
Mon Feb 22 10:30:57 2016 - [info]  ok.
Mon Feb 22 10:30:57 2016 - [warning] master_ip_failover_script is not defined.
Mon Feb 22 10:30:57 2016 - [warning] shutdown_script is not defined.
Mon Feb 22 10:30:57 2016 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.



四、mha实验模拟
1、在每次做mha实验的时候,我们都最好先执行如下命令做检测
1
2
[iyunv@manager ~]# masterha_check_ssh --conf=/usr/local/mha/mha.cnf
[iyunv@manager ~]# masterha_check_repl --conf=/usr/local/mha/mha.cnf



#确定两条命令的返回结果都是无异常的,然后启动mha服务
2、在manager端启动mha服务并时刻监控日志文件的输出变化
1
2
3
[iyunv@manager ~]# nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 &
[iyunv@manager ~]# ps -ef |grep masterha |grep -v 'grep'
root      2840  2470  2 10:53 pts/0    00:00:00 perl /usr/local/bin/masterha_manager --conf=/usr/local/mha/mha.cnf



3、测试master宕机后,时候会自动切换
#测试前查看slave01,slave02的主从同步情况
#slave01
1
2
3
4
[iyunv@slave01 ~]# mysql -uroot -p123456 -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
1
2
3
4
[iyunv@slave02 ~]# mysql -uroot -p123456 -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服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@master ~]# service mysqld stop
Shutting down MySQL (Percona Server)..... SUCCESS!

----- Failover Report -----
mha: MySQL Master failover 192.168.253.241 to 192.168.253.242 succeeded
Master 192.168.253.241 is down!
Check MHA Manager logs at manager:/usr/local/mha/manager.log for details.

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



#我们查看slave02的主从同步信息
1
2
3
4
5
[iyunv@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  #表明已经转移新的ip
             Slave_IO_Running: Yes     #表示主从ok
            Slave_SQL_Running: Yes



4、恢复master服务
#删除故障转移文件
1
[iyunv@manager mha]# rm -rf /usr/local/mha/mha.failover.complete



#重启mysql服务

1
2
[iyunv@master ~]# service mysqld start
Starting MySQL (Percona Server)... SUCCESS!



#在manager的日子文件中找到主从同步的sql语句
1
2
[iyunv@manager mha]# grep MASTER_HOST /usr/local/mha/manager.log
Mon Feb 22 11:00:38 2016 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.253.242', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=700, MASTER_USER='backup', MASTER_PASSWORD='xxx';



#在master上启动主从同步,密码为backup
1
2
3
4
5
6
mysql> change master to
    ->        master_host='192.168.253.242',master_user='backup',master_password='backup',master_port=3306,master_log_file='mysql-bin.000004',master_log_pos=700;
Query OK, 0 rows affected, 2 warnings (0.75 sec)

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



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

1
2
3
4
5
[iyunv@master ~]# 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



5、再次启动MHA的manager服务,并停止slave01
1
[iyunv@manager ~]# nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 &



#关闭slave01的mysql服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@slave01 ~]# service mysqld stop
Shutting down MySQL... SUCCESS
[iyunv@manager mha]# tail -f /usr/local/mha/manager.log
----- Failover Report -----
mha: MySQL Master failover 192.168.253.242 to 192.168.253.241 succeeded
Master 192.168.253.242 is down!
Check MHA Manager logs at manager:/usr/local/mha/manager.log for details.
Started automated(non-interactive) failover.
The latest slave 192.168.253.241(192.168.253.241:3306) has all relay logs for recovery.
Selected 192.168.253.241 as a new master.
192.168.253.241: OK: Applying all logs succeeded.
192.168.253.243: This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.253.243: OK: Applying all logs succeeded. Slave started, replicating from 192.168.253.241.
192.168.253.241: Resetting slave info succeeded.
Master failover to 192.168.253.241(192.168.253.241:3306) completed successfully.



#在slave02上查看主从同步情况
1
2
3
4
5
[iyunv@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.241
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes



6、恢复slave01服务
#删除故障转移文件
1
[iyunv@manager mha]# rm -rf /usr/local/mha/mha.failover.complete



#重启mysql服务
1
2
[iyunv@slave01 ~]# service mysqld start
Starting MySQL (Percona Server)... SUCCESS!



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

1
2
[iyunv@manager mha]# grep MASTER_HOST /usr/local/mha/manager.log
Mon Feb 22 11:27:21 2016 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='192.168.253.241', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=120, MASTER_USER='backup', MASTER_PASSWORD='xxx';



#在slave01上启动主从同步,密码为backup

1
2
3
4
5
6
mysql>  change master to
    ->        master_host='192.168.253.241',master_user='backup',master_password='backup',master_port=3306,master_log_file='mysql-bin.000005',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.61 sec)

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



#在slave01和slave02上执行,检查主从同步是否都正常,这里以slave01为例,slave02同理。
1
2
3
4
5
[iyunv@slave01 ~]# 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.241
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes



7、再次启动MHA的manager服务
1
[iyunv@manager ~]# nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 &



五、通过vip实现mysql的高可用
1、修改/usr/local/mha/mha.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/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服务

1
2
[iyunv@master ~]# service mysqld stop
Shutting down MySQL... SUCCESS!



#查看slave02的同步信息

1
2
3
4
5
[iyunv@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
2
3
4
5
6
7
8
9
10
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登陆是否成功
1
masterha_check_ssh --conf=/usr/local/mha/mha.cnf



2.查看复制是否建立好
1
masterha_check_repl --conf=/usr/local/mha/mha.cnf



3.启动mha

1
nohup masterha_manager --conf=/usr/local/mha/mha.cnf > /tmp/mha_manager.log 2>&1 &



4.检查启动的状态
1
masterha_check_status --conf=/usr/local/mha/mha.cnf



5.停止mha
1
masterha_stop masterha_check_status --conf=/usr/local/mha/mha.cnf



6.failover后下次重启
#每次failover切换后会在管理目录生成文件app1.failover.complete ,下次在切换的时候会发现有这个文件导致切换不成功,需要手动清理掉。
1
rm -rf /usr/local/mha/mha.failover.complete



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-181976-1-1.html 上篇帖子: MySQL高可用方案之多级复制 下篇帖子: pxe安装centos系统
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表