准备3台虚拟机(此处用vagrant)
vagrant init envimation/ubuntu-xenial
修改Vagrantfile
配置网络模式和Ip(此处用140,141,142,143)
config.vm.network "public_network", ip:"192.168.1.140"
vagrant ssh
所有节点上执行
sudo apt-get install libperl-dev
wget https://cpan.metacpan.org/authors/id/A/AN/ANDK/CPAN-2.22.tar.gz
tar zxf CPAN-2.22.tar.gz
cd CPAN-2.22
perl Makefile.PL
make
sudo make install
wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58.tar.gz
tar zxf mha4mysql-node-0.58.tar.gz
cd mha4mysql-node-0.58
perl Makefile.PL
make
sudo make install
sudo apt-get install mysql-server
143执行
sudo apt-get install libperl-dev
wget https://cpan.metacpan.org/authors/id/A/AN/ANDK/CPAN-2.22.tar.gz
tar zxf CPAN-2.22.tar.gz
cd CPAN-2.22
perl Makefile.PL
make
sudo make install
wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58.tar.gz
cd mha4mysql-manager-0.58
perl Makefile.PL
make
sudo make install
在所有节点上
ssh-keygen
然后吧~/.ssh/id_rsa.pub中的内容添加到其他节点的~/.ssh/authorized_keys中
修改140-142mysql配置(/etc/mysql/mysql.conf.d/mysqld.cnf)
bind 0.0.0.0
server-id = 1(分别是1,2,3)
log_bin = /var/log/mysql/mysql-bin.log
gtid_mode=ON
enforce-gtid-consistency=true
在140上(假设你设置的密码是test12345)
mysql -uroot -ptest12345
create user 'root'@'%' identified by 'test12345';
grant all privileges on *.* to 'root'@'%';
create user 'repl'@'%' identified by 'test12345';
grant replication slave on *.* to 'repl'@'%';
flush privileges;
在141,142上
mysql -uroot -ptest12345
CHANGE MASTER TO MASTER_HOST='192.168.1.140', MASTER_USER='repl',MASTER_PASSWORD='test12345', MASTER_PORT=3306, master_auto_position=1
set global relay_log_purge=0;
set global read_only=ON;
start slave;
在143上
添加配置/etc/app.cnf
[server default]
user=root
password=test12345
ssh_user=vagrant
#manager_workdir=/var/log/masterha/app1
#remote_workdir=/var/log/masterha/app1
[server1]
hostname=192.168.1.140
[server2]
hostname=192.168.1.141
[server3]
hostname=192.168.1.142
修改/usr/local/share/perl/5.22.1/MHA/NodeUtil.pm
sub parse_mysql_major_version($) {
my $str = shift;
$str =~ /(\d+)\.(\d+)/;
my $strmajor = "$1.$2";
my $result = sprintf( '%03d%03d', $strmajor =~ m/(\d+)/g );
return $result;
}
启动
sudo masterha_manager --conf=/etc/app.cnf
在140上
create database test;
use test;
create table test(id bigint not null auto_increment primary key, name varchar(100) not null default"");
insert into test(name) values("hello"),("world");
在141,142上验证
use test
select * from test;
验证failover
在140上
sudo service mysql stop
可以看到143上masterha_manager的日志
----- Failover Report -----
app1: MySQL Master failover 192.168.1.140(192.168.1.140:3306) to 192.168.1.141(192.168.1.141:3306)
Master 192.168.1.140(192.168.1.140:3306) is down!
Check MHA Manager logs at base-debootstrap for details.
Started automated(non-interactive) failover.
Selected 192.168.1.141(192.168.1.141:3306) as a new master.