MySQL MHA高可用方案【四、MHA部署】

4.1 创建普通用户

所有服务器上创建普通用户toor、设置密码及sudo授权ifconfig,加入mysql用户组

useradd -u 900 toor

echo "chenliang"|passwd --stdin toor

id toor

sed -i '92a toor ALL=(ALL) NOPASSWD:/sbin/ifconfig' /etc/sudoers

visudo -c

usermod -g mysql toor

4.2 创建密钥对

#切换到toor用户下,生成密钥,复制公钥到其它服务器(包含自身)

#切换到toor普通用户下,生成密钥对,每一台服务器都要操作

[[email protected] ~]$ su - toor

[[email protected] ~]$ ssh-keygen -t rsa -P "" -f /home/toor/.ssh/id_rsa

Generating public/private rsa key pair.

Created directory '/home/toor/.ssh'.

Your identification has been saved in /home/toor/.ssh/id_dsa.

Your public key has been saved in /home/toor/.ssh/id_dsa.pub.

The key fingerprint is:

16:11:a5:3a:6d:f6:9d:a9:69:7f:03:a3:91:95:34:a3 [email protected]

The key's randomart image is:

+--[ DSA 1024]----+

| oo. |

| o + |

| o o + |

| o .E o |

| o S o |

| = .o.oo |

| .o+o |

| oo o |

| .o... . |

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

#拷贝公钥到其它服务器,包括自身,在每一台服务器上都要执行以下四条命令

#且是在toor普通用户下

ssh-copy-id -i .ssh/id_rsa.pub "-p 921 [email protected]"

ssh-copy-id -i .ssh/id_rsa.pub "-p 921 [email protected]"

ssh-copy-id -i .ssh/id_rsa.pub "-p 921 [email protected]"

ssh-copy-id -i .ssh/id_rsa.pub "-p 921 [email protected]"

4.3 相关命令做软链接

对mysqlbinlog和mysql命令做软链接,所有服务器都需要进行操作,这里以db01为例

[[email protected] ~]# ln -sv /apps/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog

`/usr/bin/mysqlbinlog' -> `/apps/mysql/bin/mysqlbinlog'

[[email protected] ~]# ln -sv /apps/mysql/bin/mysql /usr/bin/mysql

`/usr/bin/mysql' -> `/apps/mysql/bin/mysql'

[[email protected] ~]# ll /usr/bin/{mysqlbinlog,mysql}

lrwxrwxrwx 1 root root 21 Nov 16 11:32 /usr/bin/mysql -> /apps/mysql/bin/mysql

lrwxrwxrwx 1 root root 27 Nov 16 11:31 /usr/bin/mysqlbinlog -> /apps/mysql/bin/mysqlbinlog

4.4 创建mha用户

这里需要创建mha用户(在mysql里面创建),因前面做了主从复制了,所以只需要在

db01(master)服务器上创建。

#在主库上创建mha用户

[[email protected] ~]# mysql -uroot -pchenliang -S /data/3306/mysql.sock

mysql>

mysql> grant all privileges on *.* to 'mha'@'172.16.1.%' identified by 'mha';

Query OK, 0 rows affected, 1 warning (0.08 sec)

mysql> select user,host,authentication_string from mysql.user where user='mha' and host='172.16.1.%';

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

| user | host | authentication_string |

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

| mha | 172.16.1.% | *F4C9AC49A736981AE2739FC2F4A1FD92B4F07929 |

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

1 row in set (0.00 sec)

#在从库(db02、db03、db04)上检查是否有mha用户

mysql> select user,host from mysql.user where user='mha' and host='172.16.1.%';

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

| user | host |

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

| mha | 172.16.1.% |

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

1 row in set (0.00 sec)

4.5 安装mha node包

安装mha node包,所以服务器都需要安装。这里以db01服务器上的安装为例

[[email protected] ~]# yum install perl-DBD-MySQL -y

[[email protected] ~]# cd tools/

[[email protected] tools]# rz

[[email protected] tools]# ll mha4mysql-node-0.56-0.el6.noarch.rpm

-rw-r--r-- 1 root root 36326 Sep 16 2016 mha4mysql-node-0.56-0.el6.noarch.rpm

[[email protected] tools]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm

Preparing... ########################################### [100%]

1:mha4mysql-node ########################################### [100%]

4.6 安装mha manager包

注意:这里是在db04服务器上安装,其它服务器不用安装

[[email protected] ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

[[email protected] ~]# yum install perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y

[[email protected] ~]# rpm -qa perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes

perl-Parallel-ForkManager-1.20-1.el6.noarch

perl-Log-Dispatch-2.27-1.el6.noarch

perl-Time-HiRes-1.9721-144.el6.x86_64

perl-Config-Tiny-2.12-7.1.el6.noarch

epel-release-6-8.noarch

[[email protected] ~]# cd tools/

[[email protected] tools]# rz

[[email protected] tools]# ll mha4mysql-manager-0.56-0.el6.noarch.rpm

-rw-r--r-- 1 root root 87119 Sep 16 2016 mha4mysql-manager-0.56-0.el6.noarch.rpm

[[email protected] tools]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm

Preparing... ########################################### [100%]

1:mha4mysql-manager ########################################### [100%]

4.7 创建mha相关目录

注意:这里是在db04服务器上创建相应的目录

#创建MHA的工作目录

[[email protected] ~]# mkdir /etc/mha/app/app1 -p

[[email protected] ~]# chown -R toor:toor /etc/mha/app/app1/

[[email protected] ~]# ls -ld /etc/mha/app/app1/

drwxr-xr-x 2 toor toor 4096 Nov 16 12:27 /etc/mha/app/app1/

#创建MHA的日志目录

[[email protected] ~]# mkdir /var/log/mha/app/app1 -p

[[email protected] ~]# chown -R toor:toor /var/log/mha/app/app1/

[[email protected] ~]# ls -ld /var/log/mha/app/app1/

drwxr-xr-x 2 toor toor 4096 Nov 16 12:28 /var/log/mha/app/app1/

4.8 配置mha配置文件

[[email protected] ~]# cd /etc/mha/app/app1/

[[email protected] app1]# vim app1.cnf

[server default]

manager_log=/var/log/mha/app/app1/manager.log

manager_workdir=/var/log/mha/app/app1

master_binlog_dir=/data/3306/binlog

master_ip_failover_script=/server/scripts/master_ip_failover

password=mha

ping_interval=2

repl_password=chenliang

repl_user=rep

ssh_user=toor

ssh_port=921

user=mha

[server1]

hostname=172.16.1.11

port=3306

[server2]

hostname=172.16.1.12

port=3306

[server3]

hostname=172.16.1.13

port=3306

[server4]

hostname=172.16.1.14

no_master=1

port=3306

[[email protected] app1]# chown -R toor:toor /etc/mha/app/app1/app1.cnf

[[email protected] app1]# ll /etc/mha/app/app1/app1.cnf

-rw-r--r-- 1 toor toor 468 Nov 16 12:32 /etc/mha/app/app1/app1.cnf

#配置文件说明

[server default]

manager_log=/var/log/mha/app/app1/manager.log #mha的日志保存路径及文件名

manager_workdir=/var/log/mha/app/app1 #mha的工作目录

master_binlog_dir=/data/3306/binlog #所有mysql 的binlog日志存放路径

master_ip_failover_script=/server/scripts/master_ip_failover#VIP飘移脚本的存放路径

password=mha #mha用户的密码

ping_interval=2 #每隔2秒检测一下Master是否正常

repl_password=chenliang #主从复制用户的密码

repl_user=rep #主从复制的用户

ssh_user=toor #ssh通信的用户

ssh_port=921 #ssh的端口

user=mha #mha的用户名mha

[server1] #服务器1

hostname=172.16.1.11 #hostname最好用ip地址来指定

port=3306 #mysql服务的端口

[server2] #服务器2

hostname=172.16.1.12 #hostname最好用ip地址来指定

port=3306 #mysql服务的端口

[server3] #服务器3

hostname=172.16.1.13 #hostname最好用ip地址来指定

port=3306 #mysql服务的端口

[server4] #服务器4

hostname=172.16.1.14 #hostname最好用ip地址来指定

no_master=1 #不让其服务器提升为master

port=3306 #mysql服务的端口

4.9 配置mha vip飘移脚本

注意:这里也只是在db04服务器上编写脚本

[[email protected] ~]# mkdir /server/scripts/ -p

[[email protected] ~]# cd /server/scripts/

[[email protected] scripts]# rz

[[email protected] scripts]# ll master_ip_failover

-rw-r--r-- 1 root root 3049 Nov 8 20:30 master_ip_failover

[[email protected] scripts]# cat 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 $ssh_port = '921' ;

my $vip = '172.16.1.10/24';

my $key = "1";

my $ssh_start_vip = " sudo /sbin/ifconfig eth1:$key $vip";

my $ssh_stop_vip = "sudo /sbin/ifconfig eth1:$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" ) {

# $orig_master_host, $orig_master_ip, $orig_master_port are passed.

# If you manage master ip address at global catalog database,

# invalidate orig_master_ip here.

my $exit_code = 1;

eval {

print "\n\n\n***************************************************************\n";

print "Disabling the VIP - $vip on old master: $orig_master_host\n";

print "***************************************************************\n\n\n\n";

&stop_vip();

$exit_code = 0;

};

if ([email protected]) {

warn "Got Error: [email protected]\n";

exit $exit_code;

}

exit $exit_code;

}

elsif ( $command eq "start" ) {

# all arguments are passed.

# If you manage master ip address at global catalog database,

# activate new_master_ip here.

# You can also grant write access (create user, set read_only=0, etc) here.

my $exit_code = 10;

eval {

print "\n\n\n***************************************************************\n";

print "Enabling the VIP - $vip on new master: $new_master_host \n";

print "***************************************************************\n\n\n\n";

&start_vip();

$exit_code = 0;

};

if ([email protected]) {

warn [email protected];

exit $exit_code;

}

exit $exit_code;

}

elsif ( $command eq "status" ) {

print "Checking the Status of the script.. OK \n";

`ssh -p$ssh_port $ssh_user\@$orig_master_host \" $ssh_start_vip \"`;

exit 0;

}

else {

&usage();

exit 1;

}

}

# A simple system call that enable the VIP on the new master

sub start_vip() {

`ssh -p$ssh_port $ssh_user\@$new_master_host \" $ssh_start_vip \"`;

}

# A simple system call that disable the VIP on the old_master

sub stop_vip() {

`ssh -p$ssh_port $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=po

rt -new_master_host=host -new_master_ip=i-new_master_port=port\n";

}

[[email protected] scripts]# chmod +x master_ip_failover

[[email protected] scripts]# chown toor:toor master_ip_failover

[[email protected] scripts]# ll master_ip_failover

-rwxr-xr-x 1 toor toor 3049 Nov 16 12:47 master_ip_failover

4.10 配置VIP地址

注意:这里是在Master(db01)服务器上配置vip地址

[[email protected] ~]# ifconfig eth1:1 172.16.1.10/24

[[email protected] ~]# ifconfig eth1:1

eth1:1 Link encap:Ethernet HWaddr 00:0C:29:6B:2E:55

inet addr:172.16.1.10 Bcast:172.16.1.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

[[email protected] ~]# cp -a /etc/sysconfig/network-scripts/ifcfg-eth1 /etc/sysconfig/network-scripts/ifcfg-eth1:1

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1:1

DEVICE=eth1:1

TYPE=Ethernet

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=none

IPADDR=172.16.1.10

NETMASK=255.255.255.0

USERCTL=no

PEERDNS=yes

IPV6INIT=no

1.11 检查mha的配置

注意:这里是在db04服务器上的toor普通用户下进行操作

#检查ssh免密触是否正常

[[email protected] ~]$ masterha_check_ssh --conf=/etc/mha/app/app1/app1.cnf

Fri Nov 16 12:57:21 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Fri Nov 16 12:57:21 2018 - [info] Reading application default configuration from /etc/mha/app/app1/app1.cnf..

Fri Nov 16 12:57:21 2018 - [info] Reading server configuration from /etc/mha/app/app1/app1.cnf..

Fri Nov 16 12:57:21 2018 - [info] Starting SSH connection tests..

Fri Nov 16 12:57:22 2018 - [debug]

Fri Nov 16 12:57:21 2018 - [debug] Connecting via SSH from [email protected](172.16.1.11:921) to [email protected](172.16.1.12:921)..

Fri Nov 16 12:57:22 2018 - [debug] ok.

Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.11:921) to [email protected](172.16.1.13:921)..

Fri Nov 16 12:57:22 2018 - [debug] ok.

Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.11:921) to [email protected](172.16.1.14:921)..

Fri Nov 16 12:57:22 2018 - [debug] ok.

Fri Nov 16 12:57:23 2018 - [debug]

Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.12:921) to [email protected](172.16.1.11:921)..

Fri Nov 16 12:57:22 2018 - [debug] ok.

Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.12:921) to [email protected](172.16.1.13:921)..

Fri Nov 16 12:57:22 2018 - [debug] ok.

Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.12:921) to [email protected](172.16.1.14:921)..

Fri Nov 16 12:57:23 2018 - [debug] ok.

Fri Nov 16 12:57:23 2018 - [debug]

Fri Nov 16 12:57:22 2018 - [debug] Connecting via SSH from [email protected](172.16.1.13:921) to [email protected](172.16.1.11:921)..

Fri Nov 16 12:57:23 2018 - [debug] ok.

Fri Nov 16 12:57:23 2018 - [debug] Connecting via SSH from [email protected](172.16.1.13:921) to [email protected](172.16.1.12:921)..

Fri Nov 16 12:57:23 2018 - [debug] ok.

Fri Nov 16 12:57:23 2018 - [debug] Connecting via SSH from [email protected](172.16.1.13:921) to [email protected](172.16.1.14:921)..

Fri Nov 16 12:57:23 2018 - [debug] ok.

Fri Nov 16 12:57:24 2018 - [debug]

Fri Nov 16 12:57:23 2018 - [debug] Connecting via SSH from [email protected](172.16.1.14:921) to [email protected](172.16.1.11:921)..

Fri Nov 16 12:57:23 2018 - [debug] ok.

Fri Nov 16 12:57:23 2018 - [debug] Connecting via SSH from [email protected](172.16.1.14:921) to [email protected](172.16.1.12:921)..

Fri Nov 16 12:57:24 2018 - [debug] ok.

Fri Nov 16 12:57:24 2018 - [debug] Connecting via SSH from [email protected](172.16.1.14:921) to [email protected](172.16.1.13:921)..

Fri Nov 16 12:57:24 2018 - [debug] ok.

Fri Nov 16 12:57:24 2018 - [info] All SSH connection tests passed successfully.

#检查主从复制是否正常

[[email protected] ~]$ masterha_check_repl --conf=/etc/mha/app/app1/app1.cnf

Fri Nov 16 12:59:03 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Fri Nov 16 12:59:03 2018 - [info] Reading application default configuration from /etc/mha/app/app1/app1.cnf..

Fri Nov 16 12:59:03 2018 - [info] Reading server configuration from /etc/mha/app/app1/app1.cnf..

Fri Nov 16 12:59:03 2018 - [info] MHA::MasterMonitor version 0.56.

Fri Nov 16 12:59:04 2018 - [info] GTID failover mode = 1

Fri Nov 16 12:59:04 2018 - [info] Dead Servers:

Fri Nov 16 12:59:04 2018 - [info] Alive Servers:

Fri Nov 16 12:59:04 2018 - [info] 172.16.1.11(172.16.1.11:3306)

Fri Nov 16 12:59:04 2018 - [info] 172.16.1.12(172.16.1.12:3306)

Fri Nov 16 12:59:04 2018 - [info] 172.16.1.13(172.16.1.13:3306)

Fri Nov 16 12:59:04 2018 - [info] 172.16.1.14(172.16.1.14:3306)

Fri Nov 16 12:59:04 2018 - [info] Alive Slaves:

Fri Nov 16 12:59:04 2018 - [info] 172.16.1.12(172.16.1.12:3306) Version=5.7.19-log (oldest major version between slaves) log-bin:enabled

Fri Nov 16 12:59:04 2018 - [info] GTID ON

Fri Nov 16 12:59:04 2018 - [info] Replicating from 172.16.1.11(172.16.1.11:3306)

Fri Nov 16 12:59:04 2018 - [info] 172.16.1.13(172.16.1.13:3306) Version=5.7.19-log (oldest major version between slaves) log-bin:enabled

Fri Nov 16 12:59:04 2018 - [info] GTID ON

Fri Nov 16 12:59:04 2018 - [info] Replicating from 172.16.1.11(172.16.1.11:3306)

Fri Nov 16 12:59:04 2018 - [info] 172.16.1.14(172.16.1.14:3306) Version=5.7.19-log (oldest major version between slaves) log-bin:enabled

Fri Nov 16 12:59:04 2018 - [info] GTID ON

Fri Nov 16 12:59:04 2018 - [info] Replicating from 172.16.1.11(172.16.1.11:3306)

Fri Nov 16 12:59:04 2018 - [info] Not candidate for the new Master (no_master is set)

Fri Nov 16 12:59:04 2018 - [info] Current Alive Master: 172.16.1.11(172.16.1.11:3306)

Fri Nov 16 12:59:04 2018 - [info] Checking slave configurations..

Fri Nov 16 12:59:04 2018 - [info] read_only=1 is not set on slave 172.16.1.12(172.16.1.12:3306).

Fri Nov 16 12:59:04 2018 - [info] read_only=1 is not set on slave 172.16.1.13(172.16.1.13:3306).

Fri Nov 16 12:59:04 2018 - [info] read_only=1 is not set on slave 172.16.1.14(172.16.1.14:3306).

Fri Nov 16 12:59:04 2018 - [info] Checking replication filtering settings..

Fri Nov 16 12:59:04 2018 - [info] binlog_do_db= , binlog_ignore_db=

Fri Nov 16 12:59:04 2018 - [info] Replication filtering check ok.

Fri Nov 16 12:59:04 2018 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.

Fri Nov 16 12:59:04 2018 - [info] Checking SSH publickey authentication settings on the current master..

Fri Nov 16 12:59:04 2018 - [info] HealthCheck: SSH to 172.16.1.11 is reachable.

Fri Nov 16 12:59:04 2018 - [info]

172.16.1.11(172.16.1.11:3306) (current master)

+--172.16.1.12(172.16.1.12:3306)

+--172.16.1.13(172.16.1.13:3306)

+--172.16.1.14(172.16.1.14:3306)

Fri Nov 16 12:59:04 2018 - [info] Checking replication health on 172.16.1.12..

Fri Nov 16 12:59:04 2018 - [info] ok.

Fri Nov 16 12:59:04 2018 - [info] Checking replication health on 172.16.1.13..

Fri Nov 16 12:59:04 2018 - [info] ok.

Fri Nov 16 12:59:04 2018 - [info] Checking replication health on 172.16.1.14..

Fri Nov 16 12:59:04 2018 - [info] ok.

Fri Nov 16 12:59:04 2018 - [info] Checking master_ip_failover_script status:

Fri Nov 16 12:59:04 2018 - [info] /server/scripts/master_ip_failover --command=status --ssh_user=toor --orig_master_host=172.16.1.11 --orig_master_ip=172.16.1.11 --orig_master_port=3306 --orig_master_ssh_port=921

Unknown option: orig_master_ssh_port

Checking the Status of the script.. OK

Fri Nov 16 12:59:04 2018 - [info] OK.

Fri Nov 16 12:59:04 2018 - [warning] shutdown_script is not defined.

Fri Nov 16 12:59:04 2018 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK

4.12 编写mha的启动命令

注意:这里是在db04服务器上的root用户下进行操作

#把启动命令追加到一个文件中

[[email protected] ~]# echo "nohup masterha_manager --conf=/etc/mha/app/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover </dev/null> /var/log/mha/app/app1/manager.log 2>&1 &" >mha

[[email protected] ~]# cat mha

nohup masterha_manager --conf=/etc/mha/app/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover </dev/null> /var/log/mha/app/app1/manager.log 2>&1 &

[[email protected] ~]# ll mha

-rw-r--r-- 1 root root 159 Nov 16 13:49 mha

[[email protected] ~]# chown toor:toor mha

[[email protected] ~]# chmod 700 mha

[[email protected] ~]# mv mha /usr/bin/

[[email protected] ~]# ll /usr/bin/mha

-rwx------ 1 toor toor 159 Nov 16 13:49 /usr/bin/mha

4.13 启动mha服务

注意:这里是在db04服务器上进行操作

[[email protected] ~]# su - toor

[[email protected] ~]$ mha

[[email protected] ~]$ ps -ef|grep mha

toor 5220 1 8 13:51 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/app/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover

toor 5232 5191 1 13:51 pts/0 00:00:00 grep --color=auto mha

猜你喜欢

转载自blog.51cto.com/11576296/2326998
0条评论
添加一条新回复