1、安装编译依赖包:
yum -y install ncurses-devel gcc-c++ perl-Module-Install
2、安装 gmake 编译软件(直接解压,然后./configure,gmake && gmake install 就可以了)
3、安装 Mysql 数据库:
解压;
编译:cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DSYSCONFDIR=/etc
安装:make && make install
4、创建mysql用户,并授权:
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql
chown -R mysql.mysql /usr/local/mysql
5、设置环境变量:
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile //使环境变量生效
---配置主服务器:
vim /etc/my.cnf
[mysql]
server-id = 1
log_bin = master-bin
log-slave-updates = true
---配置从服务器1:
vim /etc/my.cnf
[mysql]
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
---配置从服务器2:
vim /etc/my.cnf
[mysql]
server-id = 3
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/
systemctl stop firewalld.service
setenforce 0
/usr/local/mysql/bin/mysqld_safe --user=mysql & //开启服务
[root@s01 mysql-5.6.36]# netstat -natp | grep 3306 //查看端口是否正常开发
tcp6 0 0 :::3306 :::* LISTEN 40105/mysqld
mysql -uroot -p //进入数据库
mysql> grant replication slave on *.* to 'myslave'@'192.168.220.%' identified by '123';
mysql> grant all privileges on *.* to 'mha'@'192.168.220.%' identified by 'manager';
mysql> flush privileges; //刷新数据库
下面三条视情况添加(理论上不需要),以主机名授权(MHA检查时是通过主机名的形式):
mysql> grant all privileges on *.* to 'mha'@'master' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'slave1' identified by 'manager';
mysql> grant all privileges on *.* to 'mha'@'slave2' identified by 'manager';
show master status;
成都网络公司-成都网站建设公司创新互联十载经验成就非凡,专业从事网站建设、网站设计,成都网页设计,成都网页制作,软文营销,一元广告等。十载来已成功提供全面的成都网站建设方案,打造行业特色的成都网站建设案例,建站热线:13518219792,我们期待您的来电!slave1:
change master to master_host='192.168.220.131',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1215;
slave2:
change master to master_host='192.168.220.131',master_user='myslave',master_password='123',master_log_file='master-bin.000001',master_log_pos=1215;
mysql> start slave;
mysql> show slave status\G;
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
1、安装node组件(四台服务器都需要):
tar zvxf mha4mysql-node-0.57.tar.gz -C /opt/
cd /opt/mha4mysql-node-0.57/
perl Makefile.PL
make && make install
2、manager服务器上安装 manager组件(这个只有manager服务器才需要):
tar zvxf mha4mysql-manager-0.57.tar.gz -C /opt/
cd /opt/mha4mysql-manager-0.57/
perl Makefile.PL
make && make install
1、在 manager 上配置到所有数据库节点的无密码认证:
ssh-keygen -t rsa //因为是无密码登录,所以这步一直回车即可(会出现密钥)
ssh-copy-id 192.168.220.131
ssh-copy-id 192.168.220.140
ssh-copy-id 192.168.220.136
输入“yes”;再输入密码即可
2、在 master 上配置到数据库节点slave1 和slave2 的无密码认证:
ssh-keygen -t rsa
ssh-copy-id 192.168.220.140
ssh-copy-id 192.168.220.136
输入“yes”;再输入密码即可
3、在 slave1 上配置到数据库节点master 和slave2 的无密码认证:
ssh-keygen -t rsa
ssh-copy-id 192.168.220.131
ssh-copy-id 192.168.220.136
输入“yes”;再输入密码即可
4、在 slave2 上配置到数据库节点master 和slave1 的无密码认证:
ssh-keygen -t rsa
ssh-copy-id 192.168.220.131
ssh-copy-id 192.168.220.140
输入“yes”;再输入密码即可
cp -ra /opt/mha4mysql-manager-0.57/samples/scripts/ /usr/local/bin/
ls scripts/
master_ip_failover:自动切换时 VIP 管理的脚本;
master_ip_online_change:在线切换时 VIP 的管理;
power_manager:故障发生后关闭主机的脚本;
send_report:因故障切换后发送报警的脚本;
将自动切换时 VIP 管理的脚本复制到 /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.220.100';
my $brdc = '192.168.220.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 /opt/mha4mysql-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
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.220.140 -s 192.168.220.136
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.220.131
port=3306
[server2]
candidate_master=1
hostname=192.168.220.140
check_repl_delay=0
port=3306
[server3]
hostname=192.168.220.136
port=3306
masterha_check_ssh -conf=/etc/masterha/app1.cnf
masterha_check_repl -conf=/etc/masterha/app1.cnf //检查健康状态
/sbin/ifconfig ens33:1 192.168.220.100/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
tailf /var/log/masterha/app1/manager.log
pkill -9 mysql
mysql -h 192.168.220.100 -u root -p