标签:sql 使用 utils mysqld 架构 tmp tin install password
MHA 简介yum -y install ncurses-devel gcc-c++ perl-Module-Install //安装环境包
mount.cifs //192.144.100.8/shares /mnt/ //挂载软件包目录
cd /mnt //进入挂载目录
tar zxvf mysql-5.6.26.tar.gz -C /opt/ //解压
cd /opt/mysql-5.6.26 //进入解压目录
cmake \ //配置
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc
make && make install //编译安装
groupadd mysql
useradd -M -s /sbin/nologin mysql -g mysql //创建mysql用户
chown -R mysql.mysql /usr/local/mysql
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 //使环境变量生效
//master服务器配置文件修改
vim /etc/my.cnf
[mysql]
server-id = 1
log_bin = master-bin
log-slave-updates = true
//slave1服务器配置文件修改
vim /etc/my.cnf
[mysql]
server-id = 2
log_bin = master-bin
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
//slave2服务器配置文件修改
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 //关闭selinux
/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.144.144.%‘ identified by ‘123‘; //添加授权
mysql> grant all privileges on *.* to ‘mha‘@‘192.144.144.%‘ identified by ‘manager‘; //添加授权
mysql> flush privileges; //刷新数据库
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‘;
mysql> show master status; //在 Mysql 主服务器上查看二进制文件和同步点
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 1215 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
change master to master_host=‘192.168.144.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;
...
raster user: mnysIave
Master_ Port: 3306
Connect_ Retry: 60
Master Log_ File: master-bin .000001
Read Master Log_ Pos: 1215
Relay_ Log_ File: relay- log- bin.000002
Relay_ Log_ Pos: 284
Relay_ Master Log_ File: master- bin .000001
Slave_ IO_ Running: Yes
Slave_ SQL_ Running: Yes
Replicate DO DB:
...
mysql> set global read_only=1; //设置两个从服务器为只读模式
yum install epel-release --nogpgcheck -y //安装epel
yum install -y perl-DBD-MYSQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker perl-CPAN
cd /mnt
tar zvxf mha4mysql-node-0.57.tar.gz -C /opt/ //解压node组件包
cd /opt/mha4mysql-node-0.57/
perl Makefile.PL
make && make install
cd /mnt
tar zvxf mha4mysql-manager-0.57.tar.gz -C /opt/
cd /opt/mha4mysql-manager-0.57/
perl Makefile.PL
make && make install
//在 manager 上配置到所有数据库节点的无密码认证:
ssh-keygen -t rsa //因为是无密码登录,所以这步一直回车即可(会出现密钥)
ssh-copy-id 192.168.144.131
ssh-copy-id 192.168.144.140
ssh-copy-id 192.168.144.136
输入“yes”;再输入密码即可
//在 master 上配置到数据库节点slave1 和slave2 的无密码认证:
ssh-keygen -t rsa
ssh-copy-id 192.168.144.140
ssh-copy-id 192.168.144.136
输入“yes”;再输入密码即可
//在 slave1 上配置到数据库节点master 和slave2 的无密码认证:
ssh-keygen -t rsa
ssh-copy-id 192.168.144.131
ssh-copy-id 192.168.144.136
输入“yes”;再输入密码即可
//在 slave2 上配置到数据库节点master 和slave1 的无密码认证:
ssh-keygen -t rsa
ssh-copy-id 192.168.144.131
ssh-copy-id 192.168.144.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: //因故障切换后发送报警的脚本;
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin/ //将自动切换时 VIP 管理的脚本复制到 /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.114.100‘;
my $brdc = ‘192.168.114.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.114.140 -s 192.168.114.136
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.114.131
port=3306
[server2]
candidate_master=1
hostname=192.168.114.140
check_repl_delay=0
port=3306
[server3]
hostname=192.168.114.136
port=3306
masterha_check_ssh -conf=/etc/masterha/app1.cnf //验证密钥对
Mon Dec 16 10:32:49 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Dec 16 10:32:49 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:32:49 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:32:49 2019 - [info] Starting SSH connection tests..
Mon Dec 16 10:32:50 2019 - [debug]
Mon Dec 16 10:32:49 2019 - [debug] Connecting via SSH from root@192.168.144.131(192.168.144.131:22) to root@192.168.144.136(192.168.144.136:22)..
Mon Dec 16 10:32:49 2019 - [debug] ok.
Mon Dec 16 10:32:49 2019 - [debug] Connecting via SSH from root@192.168.144.131(192.168.144.131:22) to root@192.168.144.140(192.168.144.140:22)..
Mon Dec 16 10:32:50 2019 - [debug] ok.
Mon Dec 16 10:32:51 2019 - [debug]
Mon Dec 16 10:32:50 2019 - [debug] Connecting via SSH from root@192.168.144.140(192.168.144.140:22) to root@192.168.144.131(192.168.144.131:22)..
Mon Dec 16 10:32:50 2019 - [debug] ok.
Mon Dec 16 10:32:50 2019 - [debug] Connecting via SSH from root@192.168.144.140(192.168.144.140:22) to root@192.168.144.136(192.168.144.136:22)..
Mon Dec 16 10:32:51 2019 - [debug] ok.
Mon Dec 16 10:32:51 2019 - [debug]
Mon Dec 16 10:32:49 2019 - [debug] Connecting via SSH from root@192.168.144.136(192.168.144.136:22) to root@192.168.144.131(192.168.144.131:22)..
Mon Dec 16 10:32:50 2019 - [debug] ok.
Mon Dec 16 10:32:50 2019 - [debug] Connecting via SSH from root@192.168.144.136(192.168.144.136:22) to root@192.168.144.140(192.168.144.140:22)..
Mon Dec 16 10:32:50 2019 - [debug] ok.
Mon Dec 16 10:32:51 2019 - [info] All SSH connection tests passed successfully.
masterha_check_repl -conf=/etc/masterha/app1.cnf #验证复制
Mon Dec 16 10:34:36 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Dec 16 10:34:36 2019 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:34:36 2019 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Dec 16 10:34:36 2019 - [info] MHA::MasterMonitor version 0.57.
Creating directory /var/log/masterha/app1.. done.
Mon Dec 16 10:34:37 2019 - [info] GTID failover mode = 0
Mon Dec 16 10:34:37 2019 - [info] Dead Servers:
Mon Dec 16 10:34:37 2019 - [info] Alive Servers:
Mon Dec 16 10:34:37 2019 - [info] 192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info] 192.168.144.140(192.168.144.140:3306)
Mon Dec 16 10:34:37 2019 - [info] 192.168.144.136(192.168.144.136:3306)
Mon Dec 16 10:34:37 2019 - [info] Alive Slaves:
Mon Dec 16 10:34:37 2019 - [info] 192.168.144.140(192.168.144.140:3306) Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Mon Dec 16 10:34:37 2019 - [info] Replicating from 192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info] Primary candidate for the new Master (candidate_master is set)
Mon Dec 16 10:34:37 2019 - [info] 192.168.144.136(192.168.144.136:3306) Version=5.6.26-log (oldest major version between slaves) log-bin:enabled
Mon Dec 16 10:34:37 2019 - [info] Replicating from 192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info] Current Alive Master: 192.168.144.131(192.168.144.131:3306)
Mon Dec 16 10:34:37 2019 - [info] Checking slave configurations..
Mon Dec 16 10:34:37 2019 - [warning] relay_log_purge=0 is not set on slave 192.168.144.140(192.168.144.140:3306).
...
Checking the Status of the script.. OK
Mon Dec 16 10:35:22 2019 - [info] OK.
Mon Dec 16 10:35:22 2019 - [warning] shutdown_script is not defined.
Mon Dec 16 10:35:22 2019 - [info] Got exit code 0 (Not master dead).
/sbin/ifconfig ens33:1 192.168.144.170/24
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_cnf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>&1 & //启动MHA
masterha_check_status --conf=/etc/masterha/app1.cnf //检查master
app1 (pid:3894) is running(0:PING_OK), master:192.168.144.131
cat /var/log/masterha/app1/manager.log //查看节点信息
tailf /var/log/masterha/app1/manager.log //启动监控日志
pkill -9 mysql
标签:sql 使用 utils mysqld 架构 tmp tin install password
原文地址:https://blog.51cto.com/14473285/2461816