栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

centos7 安装mysql5.7 MHA

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

centos7 安装mysql5.7 MHA

安装三台虚拟机01,02,03

01 主

02 从

03 从  mha4mysql-manager

 ----------------------------------------------------------------------------------------------------------------------- 

centos 配置环境:

更换yum源

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
mkdir /mysql
cd /mysql
   
同步时间
rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
vim /etc/chrony.conf

server 210.72.145.44 iburst
server ntp.aliyun.com iburst

systemctl restart chronyd.service
chronyc sources -v
timedatectl set-timezone Asia/Shanghai


关闭selinux
   vim /etc/sysconfig/selinux
   SELinux=disabled
   reboot 重启
  防火墙开3306端口
  

firewall-cmd --zone=public --add-port=3306/tcp --permanent
systemctl restart firewalld
安装mysql 依赖包
    yum install libncurses*
--  yum install libtinfo*## 

 ----------------------------------------------------------------------------------------------------------------------- 

mkdir /mysql

所有Node节点安装mha-node软件包及依赖包

wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm 

yum -y install perl-DBD-MySQL ncftp perl-DBI.x86
rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm 

在03安装mha-manager软件及依赖包


yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm 
 

安装完成后移除mariadb-libs,和mysql5.7安装包冲突,但他又是mha-node的依赖


   rpm -e mariadb-libs-1:5.5.68-1.el7.x86_64 --nodeps
   
   

 ----------------------------------------------------------------------------------------------------------------------- 


   mysql 安装
   
   tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
   
   rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm 
   rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
   rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm 
   rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm 
   
   

vim /etc/my.cnf
默认路径
datadir=/var/lib/mysql
socket= /var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysql/mysqld.pid

修改后路径
datadir=/mysql/data
socket= /mysql/data/mysql.sock
log-error=/mysql/log/mysqld.log
pid-file=/mysql/data/mysqld.pid

添加:

[client]
default-character-set=utf8
socket=/mysql/data/mysql.sock

[mysql]
default-character-set=utf8
socket=/mysql/data/mysql.sock

   配置主从


gtid_mode=on
enforce-gtid-consistency=1
log_bin=mysql-bin
log-slave-updates=1
server-id=1   
binlog_format=ROW
sync_binlog=1
expire_logs_days=7
   
--grep 'temporary password' /mysql/log/mysqld.log
   

   

  
   mkdir /mysql/data
   mkdir /mysql/log
   chmod -R 777 /mysql/data 
   chmod -R 777 /mysql/log
  chown mysql:mysql -R  /mysql/data
  
 
 
 
mysqld --initialize-insecure

systemctl start mysqld.service

   chmod -R 777 /mysql/data 
   chmod -R 777 /mysql/log

reboot

 ----------------------------------------------------------------------------------------------------------------------- 

mysql -uroot 

进mysql 设置密码

01 set password=password("密码");
02 set password=password("密码");
03 set password=password("密码");

systemctl restart mysqld.service

create user 'slave'@'%' identified by '密码';
grant replication slave,replication client on *.* to 'slave'@'%';


create user 'zzs'@'%' identified by '密码';
grant all on *.* to 'zzs'@'%';


01主库
 show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 |      617 |              |                  | eb09b02d-c902-11ec-9fee-000c29c42e1a:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)


02、 03 从库
change master to master_host='10.0.0.21', master_port=3306,master_user='slave',master_password='密码',master_auto_position=1;

start slave ;

 ----------------------------------------------------------------------------------------------------------------------- 

测试主从是否生效

01

create database test;
use test;
CREATE TABLE  `test`(
   `id` INT(10) UNSIGNED AUTO_INCREMENT,
   PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into test(id) values(1);
 

02,03查询 

use test;

select id from test;
  ----------------------------------------------------------------------------------------------------------------------- 
 
 配置节点间SSH免密
首先在master节点(10.0.0.21)上执行,一路回车即可

ssh-keygen
执行结果如下

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:HFpSaM7IVW+TjQVUM0m1JBNgrnhH85O3wuur58sev1E root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|       oo.==X+o  |
|      +. + =.* . |
|   . *. o X . .  |
|    o o* = + .   |
|      o S . + . E|
|       . . . o o |
|            + o  |
|           ..= . |
|          .*Ooo. |
+----[SHA256]-----+
把生成的/root/.ssh/id_rsa拷贝到三个节点上(包括自己)

ssh-copy-id -i /root/.ssh/id_rsa root@10.0.0.21
ssh-copy-id -i /root/.ssh/id_rsa root@10.0.0.22
ssh-copy-id -i /root/.ssh/id_rsa root@10.0.0.23
执行完成后,在10.0.0.21使用ssh命令连接到10.0.0.22和10.0.0.23上是不需要密码的

ssh root@10.0.0.22
上述操作需要在10.0.0.22和10.0.0.23上均执行一遍

create user mha@'10.0.0.%'; 
grant all privileges on *.* to mha@'10.0.0.%' identified by '密码';

03执行 
rpm -ivh mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm

 ----------------------------------------------------------------------------------------------------------------------- 

配置mha

mkdir -p /etc/mha            #创建配置文件目录
mkdir -p /var/log/mha/app1   #创建日志目录
vim /etc/mha/app1.cnf        #编辑mha配置文件[server default]
manager_log=/var/log/mha/app1/manager
manager_workdir=/var/log/mha/app1
master_binlog_dir=/mysql/data
user=mha
password=密码
ping_interval=2
repl_password=密码
repl_user=mha
ssh_user=root
[server1]
hostname=10.0.0.21
ssh_port=22
port=3306
[server2]
hostname=10.0.0.22
port=3306
[server3]
hostname=10.0.0.23
port=3306
 ----------------------------------------------------------------------------------------------------------------------- 
  tail -f  /var/log/mha/app1/manager.log
 -----------------------------------------------------------------------------------------------------------------------
  masterha_manager 启动命令 

#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 &

#--remove_dead_master参数:在master 宕机后,会将配置文件中关于master的节点的信息删除掉
#--ignore_last_failover参数:如果上一次failover刚刚才完成(默认8小时),那么MHA Manager这次将不会进行failover,主要是为了避免ping-pong failover 或只是简单的网络问题导致的MHA服务不可用。若是使用了--ignore_last_failover参数,就会忽略这个限制

masterha_manager查看运行状态
masterha_check_status --conf=/etc/mha/app1.cnf

 -----------------------------------------------------------------------------------------------------------------------

创建master_ip_failover 脚本

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 = '10.0.0.100/24';  # Virtual IP 
my $key = "1"; 
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$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 "nnIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===nn"; 

    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=portn";
}

 -----------------------------------------------------------------------------------------------------------------------

chmod +x /usr/local/bin/master_ip_failover 

vim /etc/mha/app1.cnf  
 master_ip_failover_script=/usr/local/bin/master_ip_failover
 

 -----------------------------------------------------------------------------------------------------------------------

手动切换主从

masterha_master_switch --conf=/etc/mha/app1.cnf --master_state=alive --new_master_host=10.0.0.21 --orig_master_is_new_slave --running_updates_limit=10000 --interactive=0

没有做master_online_change脚本
虚拟ip需手动切换


02 删除
ifconfig ens33:1 down
01添加
ifconfig ens33:1  10.0.0.100

ps: centos8 安装mysql mha缺少各种依赖包,尝试了两天 失败告终。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号