一次MHA安装文档整理

根据生产环境搭建的临时测试环境,都是 1c1g的小虚拟机。
部署文档如下:

MHA 官方网址

Manager : https://github.com/yoshinorim/mha4mysql-manager

Node : https://github.com/yoshinorim/mha4mysql-node

MySQL 从官方下载

1 上传文件,3台服务器都传

mysql-5.7.31-linux-glibc2.12-x86_64.tar

mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

mha4mysql-node-0.58-0.el7.centos.noarch.rpm

名称 IP地址 角色

db1 192.168.50.37 主库,HMA node

db2 192.168.50.24 从库,HMA node

db3 192.168.50.34 从库,HMA master,node

2 安装mysql 【3台服务器都安装,注意主从的参数不同】

1)tar -xvf mysql-5.7.31-linux-glibc2.12-x86_64.tar

2)tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

3)mv mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql

4)mkdir -p /data/mysql

5)mkdir -p /data/logs

6)groupadd mysql

7)useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql

8)chown -R mysql.mysql /usr/local/mysql

9)chown -R mysql.mysql /data

10)/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

11)记录初始密码[10步骤的日志最后一串字符串]

12)cd /usr/local/mysql/support-files

13)cp mysql.server /etc/init.d/mysql

14)vi /etc/init.d/mysql

basedir=/usr/local/mysql

datadir=/data/mysql

15)ln -s /usr/local/mysql/bin/mysql /usr/bin

16)vi /etc/my.cnf [配置mysql配置文件],请更加实际情况修改参数,注意主从的不同]

[mysqld]

innodb

port=3306

server_id=3

read_only=1 #主库去掉

relay_log_purge=0 #主库去掉

transaction_isolation = READ-COMMITTED

performance_schema = OFF

max_allowed_packet = 1024M

innodb_sync_spin_loops = 100

innodb_spin_wait_delay = 30

innodb_open_files = 3000

innodb_flush_method = O_DIRECT

innodb_buffer_pool_instances = 1

group_concat_max_len = 102400000

innodb_buffer_pool_size=256M

autocommit=1

datadir=/data/mysql

socket=/data/mysql/mysql.sock

innodb index

innodb_stats_method = nulls_ignored

table use

lower_case_table_names = 1

table_open_cache = 3000

gtid

gtid-mode=on

enforce_gtid_consistency=on

log

log_timestamps = SYSTEM

slow_query_log = ON

long_query_time = 1

binlog_cache_size = 2048KB

binlog_format=row

binlog_row_image=minimal

max_binlog_size=1G

log_bin=/data/logs/mysqlbin

log_bin_index=/data/logs/mysqlbin.index

slow_query_log=on

slow_query_log_file=/data/logs/mysql-slow.log

long_query_time=1

char_set

character_set_server = utf8mb4

session

max_connections = 1000

innodb_thread_concurrency = 32

cache_buffer

tmp_table_size = 67108864

max_heap_table_size = 67108864

join_buffer_size = 432KB

thread_cache_size = 300

sort_buffer_size = 4096KB

query_cache_type = 0

query_cache_size = 0

read_rnd_buffer_size=1024KB

log_timestamps = SYSTEM

optimize

innodb_checksums=on

innodb_checksum_algorithm=strict_crc32

default_change

explicit_defaults_for_timestamp=on

[client]

port=3306

socket=/data/mysql/mysql.sock

17)/etc/init.d/mysql start 【启动MySQL】

18)mysql -uroot -p [输入11步骤记下的密码]

19)set password=password('Testsql123.'); 修改初始化密码

20)grant all privileges on . to 'root'@'%' identified by 'Testsql123.';

21)flush privileges;

至此,数据库安装完毕

3 配置root用户的ssh互信

ssh-keygen -t rsa 【一直回车】

将 3台服务器的 id_rsa.pub 内容都复制到 authorized_keys。

使3台服务器的authorized_keys都一样,且都有3个id_rsa.pub内容。

3台机子都执行下面3个命令,输入yes之后,3台机子第二次ssh就可以免密码。

ssh 192.168.50.37 date

ssh 192.168.50.24 date

ssh 192.168.50.34 date

4 配置1主2从

主库【192.168.50.37】执行:

grant replication slave on . to rep@'192.168.50.%' identified by 'Rep123456';

flush privileges;

show master status\G;

从库【2台都一样】执行:

change master to master_host='192.168.50.37',master_port=3306, master_user='rep',master_password='Rep123456', master_auto_position=1;

start slave;

show slave status;

至此主从配置完毕

5 MHA配置

3台都执行

1)yum install perl-DBD-MySQL -y

2)rpm -ivh /root/mha4mysql-node-0.58-0.el7.centos.noarch.rpm

3)ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog

db1主库中创建MHA专用监控用户

grant all privileges on . to mhamonitor@'192.168.50.%' identified by 'Mha123456';

flush privileges;

db3 安装 HMA manager 软件

yum install -y epel-release

yum install -y perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes

rpm -ivh /root/mha4mysql-manager-0.58-0.el7.centos.noarch.rpm

mkdir -p /etc/mha

mkdir -p /var/log/mha/app1

vi /etc/mha/app1.cnf

[server default]

manager_log=/var/log/mha/app1/manager

manager_workdir=/var/log/mha/app1

master_binlog_dir=/data/logs ##如果binlog的位置不一样,在下面的[server]标签填写

user=mhamonitor

password=Mha123456

ping_interval=2

repl_user=rep

repl_password=Rep123456

ssh_user=root

[server1]

hostname=192.168.50.37

port=3306

[server2]

hostname=192.168.50.24

port=3306

[server3]

hostname=192.168.50.34

port=3306

验证SSH通信[db3]

masterha_check_ssh --conf=/etc/mha/app1.cnf

验证 MySQL 主从状态(db3)

masterha_check_repl --conf=/etc/mha/app1.cnf [如果节点2明明同步正常,却报错,重启节点2]

启动 MHA manager (db3):

nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null> /var/log/mha/app1/manager.log 2>&1 &

检查 MHA 工作状态(db3):

masterha_check_status --conf=/etc/mha/app1.cnf

实现 MHA VIP 功能(db3):分配的vip是 192.168.50.43

vi master_ip_failover

!/usr/bin/env perl

use strict;

use warnings FATAL => 'all';

use Getopt::Long;

my (

ssh_user, orig_master_ip,

new_master_host, new_master_port

);

my $vip = '192.168.50.43/24';

my $key = "1";

my key $vip";

my key down";

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" ) {

   # $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 "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" ) {

   # 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 "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";

   `ssh $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 $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";

}

注意:

修改脚本参数,改为自己的VIP,网卡设备

vi /usr/local/bin/master_ip_failover

my $vip = '192.168.50.43/24';

my key $vip";

my key down";

chmod 755 master_ip_failover

cp master_ip_failover /usr/local/bin/

修改 mha 配置

vi /etc/mha/app1.cnf

在 [server default] 下添加

master_ip_failover_script=/usr/local/bin/master_ip_failover

在主库上手工生成第一个vip地址 [db1]

ifconfig eth0:1 192.168.50.43/24

重启 MHA manager 使配置生效(db3)

masterha_stop --conf=/etc/mha/app1.cnf

nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null> /var/log/mha/app1/manager.log 2>&1 &

【部署完成】

MHA管理

验证SSH通信[db3]

masterha_check_ssh --conf=/etc/mha/app1.cnf

验证 MySQL 主从状态(db3)

masterha_check_repl --conf=/etc/mha/app1.cnf

重启 MHA manager 使配置生效(db3)

masterha_stop --conf=/etc/mha/app1.cnf

nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null> /var/log/mha/app1/manager.log 2>&1 &

MHA状态检查

masterha_check_status --conf=/etc/mha/app1.cnf

手工切换

masterha_master_switch --conf=/etc/mha/app1.cnf --master_state=alive --new_master_host=192.168.50.37 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000

[模拟故障,db1关闭]

主节点故障之后,MHA自动切换节点,VIP漂移。

masterha_manager 会自动kill 掉。

此时是2节点的主从环境在使用

故障节点修复好后,需要重新加入主从集群,以从节点的身份。

然后修改 /etc/mha/app1.cnf 配置文件 【这个文件故障后会自动降db1的配置删除】

最后启动 MHA manager 【如果想要切换回主节点的话,可以先手工切换,再启动MHA manager,记得手工切换vip】

ip addr del 192.168.50.43/24 dev eth0 ##删除vip 或者 ifconfig eth0:1 192.168.50.43 down

ifconfig eth0:1 192.168.50.43/24 ##添加vip

你可能感兴趣的:(一次MHA安装文档整理)