一、mysql编译安装
mysql源码包下载
这里用到的是mysql-boost-5.7.31.tar.gz
[root@server1 ~]# tar zxf mysql-boost-5.7.31.tar.gz
cmake升级:(3.5版本以上)
cmake下载这里用到的cmake3.3.6
[root@server1 ~]# yum install cmake3-data-3.6.1-2.el7.noarch.rpm cmake3-3.6.1-2.el7.x86_64.rpm -y
[root@server1 ~]# yum install jsoncpp-0.10.5-2.el7.x86_64.rpm jsoncpp-devel-0.10.5-2.el7.x86_64.rpm -y
[root@server1 mysql-5.7.31]# mkdir build
[root@server1 build]# cmake3 ../ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
> -DMYSQL_DATADIR=/data/mysql \
> -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DSYSCONFDIR=/etc \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_EXTRA_CHARSETS=all \
> -DDEFAULT_CHARSET=utf8mb4 \
> -DDEFAULT_COLLATION=utf8mb4_unicode_ci \
> -DWITH_BOOST=/root/mysql-5.7.31/boost/boost_1_59_0/
升级gcc:(版本要求5.3以上)
[root@server1 build]# yum install -y gcc-c++
[root@server1 build]# yum install ncurses-devel.x86_64 -y
[root@server1 build]# openssl-devel
[root@server1 build]# yum install bison -y
解决依赖性
注意如果编译中途出现问题,在CMakeLists.txt中相应的报错行注释就行
解决依赖问题
[root@server1 build]# make
[root@server1 build]# make install
下载
2.拷贝启动脚本:
[root@server1 mysql-5.7.31]# cd /usr/local/mysql/support-files/
[root@server1 support-files]# cp mysql.server /etc/init.d/mysqld
3.新建用户:
[root@server1 mysql-5.7.31]# useradd -M -d /usr/local/mysql/ -s /sbin/nologin mysql
4.创建数据目录:
[root@server1 mysql-5.7.31]# mkdir -p /data/mysql
[root@server1 mysql-5.7.31]# chown mysql.mysql /data/mysql/
5.添加变量
[root@server1 build]# cd /usr/local/mysql/bin/
[root@server1 bin]# pwd
/usr/local/mysql/bin
[root@server1 bin]# vim ~/.bash_profile
[root@server1 bin]# source ~/.bash_profile
6.修改配置:
[root@server1 ~]# vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
7.初始化数据库:
[root@server1 ~]# mysqld --initialize --user=mysql ##获得密钥
[root@server1 ~]# /etc/init.d/mysqld start ##开启mysql服务
[root@server1 ~]# mysql_secure_installation ##初始化
初始化
接下来的初始化内容输入y即可,即完成了初始化内容
# mysql -p
7.图形化管理数据库
# unzip phpMyAdmin-5.0.2-all-languages.zip
##这个包可以从网上直接下载
# mv phpMyAdmin-5.0.2-all-languages /usr/local/nginx/html/phpmyadmin
172.25.1.1/phpmyadmin/index.php
登陆失败,因为php配置文件问题,只要把相关文件的路径加上即可
# vim /usr/local/lnmp/php/etc/php.ini
# systemctl reload php-fpm.service
然后我们再次登陆,成功登陆
二、mysql用户与授权
mysql> CREATE USER 'suns'@'localhost' IDENTIFIED BY 'password';
##本地登陆建立
mysql> CREATE USER 'suns'@'%' IDENTIFIED BY 'password';
##远程登录建立
mysql> GRANT insert, delete,update,select ON westos.* TO 'suns'@'localhost';
##本地登陆授权
mysql> GRANT select ON *.* TO 'suns'@'%';
##远程登陆授权
三、mysql加密连接
mysql> CREATE USER 'westos'@'%' IDENTIFIED BY 'password' REQUIRE SSL;//强制要求用户加密连接
四、mysql 的主从复制
1.先给server2配置mysql,可以直接把文件传给server2
[root@server1 local]# scp -r mysql/ server2:/usr/local/
然后在server2配置,初始化等操作和server1相同
1.master设置server-id:
[root@server1 ~]# vim /etc/my.cnf
server-id=1 //在线生效
log-bin=binlog ##二进制日志
2.master创建复制用户:
mysql> grant replication slave on *.* to repl@'%' identified by 'westos';
mysql> show master status;
3.slave设置server-id
[root@server2 ~]# vim /etc/my.cnf
server-id=2 //在线生效
log-bin=binlog ##二进制日志(可以不写)
mysql> change master to master_host='172.25.1.1', master_user='repl', master_password='westos', master_log_file='binlog.000001', master_log_pos=154;
mysql> start slave;
mysql> show slave status\G;
可以在图形化管理界面进行库和表的创建
测试:
从上图看主从复制已经实现;可以实现数据同步。
4.GTID复制:
[root@server1 ~]# vim /etc/my.cnf
gtid_mode=ON
enforce-gtid-consistency=ON
[root@server2 ~]# vim /etc/my.cnf
gtid_mode=ON
enforce-gtid-consistency=ON
slave.server2
mysql>stop slave;
mysql> change master to master_host='172.25.1.1', master_user='repl', master_password='westos', MASTER_AUTO_POSITION = 1;
mysql> start slave;
master.server1
mysql> insert into westos.users values ('user2','123');
5.半同步复制:
master:
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
SET GLOBAL rpl_semi_sync_master_enabled =1;
show status like 'Rpl_semi%';
slave:
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
SET GLOBAL rpl_semi_sync_slave_enabled = 1;
STOP SLAVE IO_THREAD;
START SLAVE IO_THREAD;
状态查询
测试
Slave
mysql> set global rpl_semi_sync_slave_enabled = 0; mysql> stop slave io_thread; mysql> start slave io_thread;
关闭半同步复制,重启io接口
master
mysql> insert into westos.users values ('user4','123');
插入第一个数据时花了10.01秒,而我们前面设置的超时时间是10秒,而插入第二个数据花了0.01秒,由此得出结论是超时转换为异步传送。可以在Master上查看半同步相关的参数值Rpl_semi_sync_master_clients和Rpl_semi_sync_master_status是否正常。
mysql> set global rpl_semi_sync_master_timeout = 99999999999999;
5.7版本之前的半同步模式
5.7版本以后的
再配置一个server3方法和server2之前一样
安县
注意因为之前数据库里已经有数据了,所以在这里需要将数据库备份然后给server3倒入数据。
[root@server1 mysql]# mysqldump -p --set-gtid-purged=OFF westos > db.sql
[root@server1 mysql]# scp db.sql server3:/data/mysql/
[root@server3 mysql]# mysqladmin create westos -p
[root@server3 mysql]# mysql -p westos < db.sql
让2也成为master
slave.server2
grant replication slave on *.* to repl@'%' identified by 'westos';
reset slave;
reset master;
slave.serer3
change master to master_host='172.25.1.2', master_user='repl', master_password='westos', MASTER_AUTO_POSITION = 1;
reset master;
reset slave;
测试
master.server1
mysql> delete from westos.users where user='user2';
6. 并行复制
给三台都加上以下参数
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
7.MySQL组复制
多主模式
[root@server3 ~]# vim /etc/my.cnf ##server1-3配置大体相同
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
server_id=3
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address="server3:33061"
group_replication_group_seeds="server1:33061,server2:33061,server3:33061"
group_replication_bootstrap_group=off
group_replication_ip_whitelist="172.25.1.0/24,127.0.0.1/8"
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=ON
server1
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'password';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='password' FOR CHANNEL 'group_replication_recovery';
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
server2 server3 相同
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'password';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='password' FOR CHANNEL 'group_replication_recovery';
mysql> START GROUP_REPLICATION;
mysql> SELECT * FROM performance_schema.replication_group_members; ##查询组状态
测试
建立一个数据库看信息是否同步
mysql> create database test;
mysql> USE test;
mysql> create table t1 (c1 INT PRIMARY KEY, C2 TEXT NOT NULL); ##只支持带有主键的表
mysql> INSERT INTO t1 VALUES (1, 'Luis');
mysql> SELECT * FROM t1;
组复制相较与主从复制而言,实现了在每台组成员上都可以实现读写的功能,对于主从复制中,只有主机可以写,其他从机只能读,不能写。
延迟复制:
STOP SLAVE SQL_THREAD;
CHANGE MASTER TO MASTER_DELAY=60;
START SLAVE SQL_THREAD;
show slave status\G;
慢查询:
set global slow_query_log=ON;
show variables like "long%";
set long_query_time=5;
show status like '%slow%';
五、MySQL路由器
1.MySQL路由器配置
在server4执行
[root@server4 ~]# rpm -ivh mysql-router-community-8.0.21-1.el7.x86_64.rpm
[root@server4 ~]# vim /etc/mysqlrouter/mysqlrouter.conf
[routing:ro]
bind_address = 0.0.0.0
bind_port = 7001
destinations = 172.25.1.1:3306,172.25.1.2:3306,172.25.1.3:3306
routing_strategy = round-robin
[routing:rw]
bind_address = 0.0.0.0
bind_port = 7002
destinations = 172.25.1.1:3306,172.25.1.2:3306,172.25.1.3:3306
routing_strategy = first-available
[root@server4 ~]# systemctl start mysqlrouter.service
mysql> grant all on test.* to sun@'%' identified by 'westos'; ##创建一个远程登录的用户
测试:
[root@Sun_s Desktop]# mysql -h 172.25.1.4 -P 7001 -u sun -p
在真机测试连接路由ip 登陆mysql
路由只能实现调度,负载均衡,不能实现读写分离。
[root@server1 ~]# lsof -i :3306
查看日志:cat /var/log/mysqlrouter/mysqlrouter.log
当连接server1 down掉之后会自动跳转到server2上
由于mysql路由器的7001端口采用的使轮询的调度算法,所以每次连接到的后端真实的数据库都是一次变化的
六、MHA 高可用
MHA全称Master High Availability,也就是主节点的高可用,是目前比较成熟的MySQL高可用解决方案。它的主要功能主要是通过一个manager节点来监控主节点和从节点的状况,并会在主节点发生故障的时候,自动将一个数据最贴近Master的从节点转化成主节点。实现自动的故障转移。
MHA的变成语言是Perl,需要安装一些软件包来进行编译操作,但是总体的编译过程十分地简单。
1.实验环境搭建
还原环境,保证实验环境的纯净,三台机子操作相同
[root@server1 mysql]# /etc/init.d/mysqld stop
[root@server1 mysql]# rm -fr /data/mysql/*
[root@server1 mysql]# mysqld --initialize --user=mysql
[root@server1 mysql]# /etc/init.d/mysqld start
[root@server1 mysql]# mysql -p
输入刚才初始化得到的密码
进入之后修改登陆密码
mysql> alter user root@localhost identified by 'westos';
创造新的MHA高可用环境
1.首先需要配成一主两从
vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
server_id=1 #每个数据库都有一个不一样ID
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
master:server1:
mysql> grant replication slave on *.* to repl@'%' identified by 'westos'; ##设置复制用户
mysql> grant all on *.* to root@'%' identified by 'westos'; ## 更改root登陆权限
slave:server2,server3
mysql> grant replication slave on *.* to repl@'%' identified by 'westos';
mysql> change master to master_host='172.25.1.1',master_user='repl',master_password='westos',MASTER_AUTO_POSITION = 1;
mysql> start slave;
server4 #MHA Manager(管理节点)
[root@server4 ~]# cd MHA-7/
[root@server4 MHA-7]# yum install /root/MHA-7/*.rpm -y
[root@server4 MHA-7]# ssh-keygen ##给server1 2 3 都做免密登陆操作
[root@server4 MHA-7]# ssh-copy-id server1
[root@server4 MHA-7]# ssh-copy-id server2
[root@server4 MHA-7]# ssh-copy-id server3
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server1:
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server2:
[root@server4 MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server3:
[root@server4 MHA-7]# tar xzf mha4mysql-manager-0.58.tar.gz
[root@server4 MHA-7]# cd /root/MHA-7/mha4mysql-manager-0.58/samples/conf
[root@server4 conf]# ls
app1.cnf masterha_default.cnf
[root@server4 conf]# mkdir /etc/masterha
[root@server4 conf]# cp app1.cnf /etc/masterha/
这里还需要将 masterha_default.cnf 里的内容复制到 /etc/masterha/app1.cnf 中
[root@server4 masterha]# mkdir /etc/masterha/app1 ##配置文件中日志存放的默认路径
[root@server1 ~]# yum install mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
server2 server3相同
# vim /etc/masterha/app.conf
[server default]
user=root #mysql主从节点的管理员用户密码,确保可以从远程登陆
password=westos
ssh_user=root #ssh用户名
master_binlog_dir= /data/mysql
remote_workdir=/tmp #远端mysql在发生切换时binlog的保存位置
secondary_check_script= masterha_secondary_check -s 172.25.1.1 -s 172.25.1.2
ping_interval=3 #发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行failover
# master_ip_failover_script= /script/masterha/master_ip_failover #failover自动切换脚本
# shutdown_script= /script/masterha/power_manager #故障发生后关闭故障主机脚本,防止脑裂
# report_script= /script/masterha/send_report #发生切换后发送报警的脚本
# master_ip_online_change_script= /script/masterha/master_ip_online_change #手动切换脚本
manager_workdir=/etc/masterha/app1 #manager工作目录
manager_log=/etc/masterha/app1/manager.log #manager日志文件
repl_user=repl #主从复制用户密码
repl_password=westos
[server1]
hostname=172.25.1.1
[server2]
hostname=172.25.1.2
candidate_master=1 #指定failover时此slave会接管master,即使数据不是最新的
check_repl_delay=0 #默认情况下如果一个slave落后master 100M的relay logs的话,MHA将不会选择该slave作为一个新的master,因为对于这个slave的恢复需要花费很长时间,通过设置check_repl_delay=0,MHA触发切换在选择一个新的master的时候将会忽略复制延时,这个参数对于设置了candidate_master=1的主机非常有用,因为这个候选主在切换的过程中一定是新的master
[server3]
hostname=172.25.1.3
no_master=1 #始终是slave
server4 为了让123之间也彼此免密
[root@server4 ~]# scp -r .ssh/ server1:
[root@server4 ~]# scp -r .ssh/ server2:
[root@server4 ~]# scp -r .ssh/ server3:
Manager工具包主要包括以下几个工具:
masterha_check_ssh//检查MHA的SSH配置状况masterha_check_repl//检查MySQL复制状况
masterha_manger启动MHA
masterha_check_status//检测当前MHA运行状态
masterha_master_monitor/检测master是否宕机
masterha_master_switch//控制故障转移(自动或者手动)
masterha_conf_host//添加或删除配置的server信息
save_binary_logs/保存和复制master的二进制日志
apply_diff_relay_logs//识别差异的中继日志事件并将其差异的事件应用于其他的slave
filter_mysqlbinlog//去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
purge_relay_logs//清除中继日志(不会阻塞SQL线程)
[root@server4 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf ##检测免密
[root@server4 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf ## 检测复制用户
2.测试高可用
(1)手动切换:
当master在线时 切换master,从1-2
# masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=172.25.1.2 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
查看效果
从server1里查看效果,server1已经变成slave,serve2 变成了master
MHA在线切换的大概过程:
1.检测复制设置和确定当前主服务器
2.确定新的主服务器
3.阻塞写入到当前主服务器
4.等待所有从服务器赶上复制
5.授予写入到新的主服务器
6.重新设置从服务器
为了保证数据完全一致性,在最快的时间内完成切换,MHA的在线切换必须满足以下条件才会切换成功,否则会切换失败。
1.所有slave的IO线程都在运行
2.所有slave的SQL线程都在运行
3.所有的show slave status的输出中Seconds_Behind_Master参数小于或者等于running_updates_limit秒,如果在切换过程中不指定running_updates_limit,那么默认情况下running_updates_limit为1秒。
4.在master端,通过show processlist输出,没有一个更新花费的时间大于running_updates_limit秒。
当master down 掉时,master从server2变成server1
[root@server2 ~]# /etc/init.d/mysqld stop
[root@server4 ~]# masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=172.25.1.2 --dead_master_port=3306 --new_master_host=172.25.1.1 --new_master_port=3306 --ignore_last_failover
查看效果
mysql> CHANGE MASTER TO MASTER_HOST='172.25.1.1', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl', MASTER_PASSWORD='westos'; mysql> start slave;
master 已经成功由2-1
(2)自动切换
[root@server1 ~]# /etc/init.d/mysqld stop
[root@server4 masterha]# ls
app1 app1.cnf
[root@server4 masterha]# cd app1/
[root@server4 app1]# ls
app1.failover.complete manager.log
[root@server4 app1]# rm -fr app1.failover.complete
删除刚才生成的日志,不然实现不了
[root@server4 masterha]# masterha_manager --conf=/etc/masterha/app1.cnf &
查看效果
[root@server1 ~]# /etc/init.d/mysqld start
[root@server1 ~]# mysql -p
mysql> CHANGE MASTER TO MASTER_HOST=‘172.25.1.2’, MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER=‘repl’, MASTER_PASSWORD=‘westos’;
mysql> start slave;
master 由1变成2
MHA的故障切换过程,共包括以下的步骤:
1.配置文件检查阶段,这个阶段会检查整个集群配置文件配置
2.宕机的master处理,这个阶段包括虚拟ip摘除操作,主机关机操作
3.复制dead maste和最新slave相差的relay log,并保存到MHA Manger具体的目录下
4.识别含有最新更新的slave
5.应用从master保存的二进制日志事件(binlog events)
6.提升一个slave为新的master进行复制
7.使其他的slave连接新的master进行复制
3.加入VIP
[root@server4 masterha]# vim app1.cnf
[root@server4 masterha]# vim 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 = '172.25.1.100/24';
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";
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";
}
[root@server4 masterha]# vim master_ip_online_change
#!/usr/bin/env perl
use strict;
use warnings FATAL =>'all';
use Getopt::Long;
my $vip = '172.25.1.100/24'; # Virtual IP
my $ssh_start_vip = "/sbin/ip addr add $vip dev eth0";
my $ssh_stop_vip = "/sbin/ip addr del $vip dev eth0";
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" ) {
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=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
测试
[root@server4 masterha]# chmod +x master_ip_failover master_ip_online_change
[root@server2 ~]# ip addr add 172.25.1.100/24 dev eth0
[root@server2 ~]# /etc/init.d/mysqld stop
[root@server4 masterha]# masterha_manager --conf=/etc/masterha/app1.cnf &