MYSQL-MHA搭建
1、创建mysql用户
groupadd mysql
useradd -g mysql mysql
修改/usr/local/mysql权限
chown -R mysql:mysql /usr/local/mysql
初始化
/usr/local/mysql/bin/mysqld
--defaults-file=/etc/my.cnf --initialize-insecure
--basedir=/usr/local/mysql --datadir=/data/mysql/mysqldata --user=mysql
启动
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &
#拷贝启动脚本
cp support-files/mysql.server /etc/init.d/mysql
#添加开机自启 chkconfig mysql on
#启动MySQL service mysql start --启动MySQL
首先创建授权账户
GRANT REPLICATION SLAVE ON *.* TO rep@'192.168.152.%' IDENTIFIED BY 'rep';
flush privileges;
创建用户mha管理的账号,在所有mysql服务器上都需要执行:
GRANT all privileges ON *.* TO manager@'192.168.152.%' IDENTIFIED BY 'manager_1234';
创建主从账号,在所有mysql服务器上都需要执行:
GRANT RELOAD, SUPER, REPLICATION SLAVE ON*.* TO 'repl'@'192.168.52.%' IDENTIFIED BY 'repl_1234';
Master数据库退出MySQL,编辑/etcl/my.cnf
[mysqld] #在mysqld下面添加如下两行
server-id = 2
log-bin=mysql-bin
binlog-ignore-db=mysql #忽略的数据库
binlog-ignore-db=information-schema
重启Master数据库
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000003 | 120 | | information_schema,mysql | |
+------------------+----------+--------------+--------------------------+-------------------+
row in set (0.00 sec)
同样将两台slave服务器也进行如下修改
编辑/etc/my.cnf文件
#slave1
[mysqld]
log-bin=mysql-bin
server-id=136
replicate_ignore_db=mysql #被忽略的数据库
replicate-ignore-db=information-schema
slave-skip-errors=all #跳过所有错误
slave1
change master to master_host='192.168.152.129',master_user='rep',master_password='rep',master_log_file='mysql-bin.000003',master_log_pos=3063;
slave 2
change master to master_host='192.168.152.129',master_user='rep',master_password='rep',master_log_file='mysql-bin.000003',master_log_pos=3063;
配置互信
每个节点执行ssh-keygen
将其他节点的ssh的公钥copy到master节点
slave1:
cd ~/.ssh
scp id_rsa.pub master:~/.ssh/id_rsa.pub1
slave2:
cd ~/.ssh
scp id_rsa.pub master:~/.ssh/id_rsa.pub2
master:
cat id_rsa.pub id_rsa.pub1 id_rsa.pub2 > authorized_keys
chmod 600 authorized_keys
scp authorized_keys slave1:~/.ssh
scp authorized_keys slave2:~/.ssh
各节点测试互信
ssh slave1 date
---------------------------------
安装mha
在各节点安装node节点
安装依赖
yum -y install perl-DBD-MySQL
yum -y install perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
yum -y install perl-devel perl-CPAN
安装mha node节点
tar -xvzf /soft/mha4mysql-node-0.57.tar.gz
mkdir -p /etc/mha/mhanode
mv /soft/mha4mysql-node-0.57/* /etc/mha/mhanode
cd /etc/mha/mhanode
perl Makefile.PL
make
make install
centos
依赖安装
yum -y localinstall perl-Config-Tiny-2.14-7.el7.noarch.rpm
yum -y localinstall perl-Log-Dispatch-2.26-1.el6.rf.noarch.rpm
yum -y localinstall perl-Parallel-ForkManager-1.18-2.el7.noarch.rpm
yum -y localinstall perl-Params-Validate-1.08-4.el7.x86_64.rpm
yum -y install perl-DBD-MySQL
yum -y install perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
yum -y install perl-devel perl-CPAN
yum install -y perl-ExtUtils-Embed
yum install -y perl-Time-HiRes
在slave2上安装manager节点
tar -zxvf /soft/mha4mysql-manager-0.57.tar.gz
mkdir -p /etc/mha/mhamanager
mv /soft/mha4mysql-manager-0.57/* /etc/mha/mhamanager
cd /etc/mha/mhamanager
perl Makefile.PL
make
make install
- DBI ...loaded. (1.627)
- DBD::mysql ...loaded. (4.023)
- Time::HiRes ...loaded. (1.9725)
- Config::Tiny ...loaded. (2.14)
- Log::Dispatch ...loaded. (2.26)
- Parallel::ForkManager ...loaded. (1.18)
- MHA::NodeConst ...loaded. (0.57)
------------------------------------
cd /etc/mha
mkdir -p /etc/mha/app1
vim /etc/mha/app1.cnf
[server default]
# working directory on the manager
manager_workdir=/etc/mha/app1
manager_log=/etc/mha/app1/manager.log
remote_workdir=/etc/mha/app1
# In case of automatic file-over, the following script will be used
master_ip_failover_script=/etc/mha/master_ip_failover
master_ip_online_change_script=/etc/mha/master_ip_online_change
#mha conn mysql
user=root
password=root
#mysql rep
repl_user=rep
repl_password=rep
ssh_user=root
ping_interval=1
secondary_check_script= masterha_secondary_check -s 192.168.2.11 -s 192.168.2.12
[server1]
hostname=192.168.2.10
port=3306
master_binlog_dir="/data/mysql/mysqldata"
candidate_master=1
ssh_port=22
[server2]
hostname=192.168.2.11
port=3306
master_binlog_dir="/home/mysql3306/mysql3306"
candidate_master=1
ssh_port=22
[server3]
hostname=192.168.2.12
port=3306
master_binlog_dir="/home/mysql3306/mysql3306"
no_master=1
ssh_port=22
VIP故障迁移脚本
vim /etc/mha/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.150.201/24';
my $key = '0';
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 \"`;
}
sub stop_vip() {
return 0 unless ($ssh_user);
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
"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";
}
#################################
my $vip = '192.168.150.201/24';
my $key = '0';
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
赋予切换脚本执⾏行行权限
chmod +x /etc/mha/master_ip_failover
c.编辑在线切换脚本
vim /etc/mha/master_ip_online_change
-----------------------------------------------------------------
#!/usr/bin/env perl
use strict;
use warnings FATAL =>'all';
use Getopt::Long;
my $vip = '192.168.150.201/24'; # Virtual IP
my $key = "0";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
my $exit_code = 0;
my (
$command, $orig_master_is_new_slave, $orig_master_host,
$orig_master_ip, $orig_master_port, $orig_master_user,
$orig_master_password, $orig_master_ssh_user, $new_master_host,
$new_master_ip, $new_master_port, $new_master_user,
$new_master_password, $new_master_ssh_user,
);
GetOptions(
'command=s' => \$command,
'orig_master_is_new_slave' => \$orig_master_is_new_slave,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'orig_master_user=s' => \$orig_master_user,
'orig_master_password=s' => \$orig_master_password,
'orig_master_ssh_user=s' => \$orig_master_ssh_user,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
'new_master_user=s' => \$new_master_user,
'new_master_password=s' => \$new_master_password,
'new_master_ssh_user=s' => \$new_master_ssh_user,
);
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 ($@) {
warn "Got Error: $@\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 ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
`ssh $orig_master_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 $new_master_ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $orig_master_ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
"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=ip -new_master_port=port\n";
}
------------------------------------------------------------------------------
保存退出
chmod +x /etc/mha/master_ip_online_change
检测ssh
/etc/mha/mhamanager/bin/masterha_check_ssh --conf=/etc/mha/app1.cnf
检测repl
在各节点创建软连接
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
/etc/mha/mhamanager/bin/masterha_check_repl --conf=/etc/mha/app1.cnf
挂vip
ifconfig ens32:0 192.168.8.235 up
启动mha
nohup
/etc/mha/mhamanager/bin/masterha_manager --conf=/etc/mha/app1.cnf
--ignore_last_failover > /tmp/mha_manager.log < /dev/null
2>&1 &
检测健康状态
/etc/mha/mhamanager/bin/masterha_check_status --conf=/etc/mha/app1.cnf
关闭mha
/etc/mha/mhamanager/bin/masterha_stop --conf=/etc/mha/app1.cnf
failover