1.MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,MHA是由日本人开发,是一套优秀的MySQL故障切换和主从复制的高可用软件
2.在MySQL故障切换的过程中,MHA能做到0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能够最大程度上保证数据库的一致性,以达到真正意义上的高可用
3.MHA由perl语言编写,该工具仅仅使用于MySQLReplication环境,目的在于维持Master主库的高可用性
4.MHA是基于标准的MySQL复制(异步/半同步)
5.MHA由两部分组成管理节点(MHA manager)和数据节点(MHA Node)两部分组成
6.MHA manager 可以单独部署在一台服务器上,可以部署在一台slave上面
7.主从复制架构:主从复制(一主多从);MHA架构(多主多从);MMM架构(双主多从)
yum -y install ncurses-devel gcc-c++ perl-Module-Install
tar zxvf cmake-2.8.6.tar.gz
cd cmake-2.8.6
./configure
gmake && gmake install
tar -zxvf mysql-5.6.36.tar.gz
cd mysql-5.6.36
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DSYSCONFDIR=/etc
[root@Mysql1 mysql-5.6.36]# make && make install
[root@Mysql1 mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
[root@Mysql1 mysql-5.6.36]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@Mysql1 ~]# chmod +x /etc/rc.d/init.d/mysqld
[root@Mysql1 ~]# chkconfig --add mysqld
[root@Mysql1 ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@Mysql1 ~]# source /etc/profile
[root@Mysql1 ~]# groupadd mysql
[root@Mysql1 ~]# useradd -M -s /sbin/nologin mysql -g mysql
[root@Mysql1 ~]# chown -R mysql.mysql /usr/local/mysql
[root@Mysql1 ~]# mkdir -p /data/mysql
/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--user=mysql
vim /etc/my.cnf
[mysqld]
server-id = 1
log_bin = master-bin
log-slave-updates = true
在/etc/my.cnf 中修改或者增加下面内容。
vim /etc/my.cnf
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
ln -s /usr/local/mysql/bin/mysql /usr/sbin/
ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
/usr/local/mysql/bin/mysqld_safe --user=mysql &
systemctl stop firewalld
setenforce 0
mysql> grant replication slave on *.* to 'myslave'@'192.168.88.%' identified by '123';
mysql> grant all privileges on *.* to 'mha'@'192.168.88.%' identified by 'manager';
mysql> flush privileges;
mysql> grant all privileges on *.* to 'mha'@'Mysql1' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'Mysql2' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'Mysql3' identified by 'manager';
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 1314 | | | |
+-------------------+----------+--------------+------------------+-------------------+
mysql> change master to master_host='192.168.195.129',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1314;
mysql> start slave;
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql> set global read_only=1;
yum install epel-release --nogpgcheck -y
yum install -y perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
tar zxvf mha4mysql-node-0.57.tar.gz
cd mha4mysql-node-0.57
perl Makefile.PL
make
make install
tar zxvf mha4MHA-manager-0.57.tar.gz
cd mha4MHA-manager-0.57
perl Makefile.PL
make
make install
ssh-keygen -t rsa //一路按回车键
ssh-copy-id 192.168.88.131
ssh-copy-id 192.168.88.133
ssh-copy-id 192.168.88.135
ssh-keygen -t rsa
ssh-copy-id 192.168.88.133
ssh-copy-id 192.168.88.135
ssh-keygen -t rsa
ssh-copy-id 192.168.88.131
ssh-copy-id 192.168.88.135
ssh-keygen -t rsa
ssh-copy-id 192.168.88.131
ssh-copy-id 192.168.88.133
cp -ra /root/mha4MHA-manager-0.57/samples/scripts /usr/local/bin
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
vim /usr/local/bin/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.88.200';
my $brdc = '192.168.88.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $exit_code = 0;
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
##################################################################################
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";
}
mkdir /etc/masterha
cp /root/mha4MHA-manager-0.57/samples/conf/app1.cnf /etc/masterha
vim /etc/masterha/app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=manager
ping_interval=1
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.88.133 -s 192.168.88.135
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.88.131
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.88.133
port=3306
[server3]
hostname=192.168.88.135
port=3306
masterha_check_ssh -conf=/etc/masterha/app1.cnf
masterha_check_repl -conf=/etc/masterha/app1.cnf
/sbin/ifconfig ens33:1 192.168.88.200/24
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 &
masterha_check_status --conf=/etc/masterha/app1.cnf
cat /var/log/masterha/app1/manager.log10. 查看
[root@Mysql1 ~]# ifconfig
yum insatll -y mariadb
mysql -h 192.168.43.200 -u root -p //先给权限
grant all on *.* 'root'@'' indentified by 'abc123'
pkill -9 mysql
ifconfig
ens33:1:flags=4163 mtu 1500
inet 192.168.218.100 netmask 255.255.255.0 broadcast 192.168.88.200
ether 00:0c:29:…… txqueuelen 1000 (Ethernet)