zoukankan      html  css  js  c++  java
  • 11. 未完

    一. 搭建主从复制 - 一主一从 - 单机

    1. 主库开启二进制日志

    # 创建二进制储存目录并授权
    [root@cs ~]# mkdir -p /data/mysql/3307/logs/binlog
    [root@cs ~]# chown -R mysql:mysql /data/mysql/3307/logs/*
    
    # 配置二进制日志
    [root@cs ~]# vim /data/mysql/3307/my.cnf
    log_bin=/data/mysql/3307/logs/binlog/mysql-bin
    binlog_format=row
    
    # 启动主库服务
    [root@cs ~]# systemctl start mysqld3307.service
    

    2. 主库创建专用的复制用户

    [root@cs ~]# mysql -uroot -p123 -S /data/mysql/3307/mysql.sock
    -- 创建专用的复制用户
    grant replication slave on *.* to rs@'%' identified by '123';
    -- 查看所有用户
    select user,host from mysql.user;
    

    3. 主库模拟一些数据变更,然后进行全备

    [root@cs ~]# mysql -uroot -p123 -S /data/mysql/3307/mysql.sock
    create database r1 charset utf8;
    use r1
    create table a1(id int);
    insert into a1 values(1),(2),(3);
    
    # 全备
    [root@cs ~]# mysqldump -uroot -p123 -S /data/mysql/3307/mysql.sock -A -E -R --triggers --master-data=2 --single-transaction >/data/mysql/3307/backup/full.sql
    [root@cs ~]# ll /data/mysql/3307/backup/
    

    4. 在从库中,恢复主库的全备数据

    # 启动从库服务
    [root@cs ~]# systemctl start mysqld3308.service
    [root@cs ~]# mysql -uroot -p123 -S /data/mysql/3308/mysql.sock
    
    # 恢复主库数据到从库
    set sql_log_bin=0;
    source /data/mysql/3307/backup/full.sql
    set sql_log_bin=1;
    

    5. 在从库中操作,建立主从关系

    change master to在从库中通过帮助信息,找到我们需要的几个字段,然后根据full.sql中保存的position号和当前使用的binlog文件进行字段值的修改:

    # 1.查找日志文件和pos号
    [root@cs ~]# head -n 40 /data/mysql/3307/backup/full.sql
    CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=1018;
    
    # 2.建立主从关系
    [root@cs ~]# mysql -uroot -p123 -S /data/mysql/3308/mysql.sock
    mysql> help change master to     # 下面的参数可以用help查看
    
    CHANGE MASTER TO
      MASTER_HOST='192.168.189.137',				
      MASTER_USER='rs',
      MASTER_PASSWORD='123',
      MASTER_PORT=3307,
      MASTER_LOG_FILE='mysql-bin.000001',
      MASTER_LOG_POS=1018,
      MASTER_CONNECT_RETRY=10;
      
    # 3.开启专用的复制线程
    mysql> start slave;
    
    # 4.监控主从复制状态
    mysql> show slave status G
    

    CHANGE MASTER TO 参数

    • MASTER_HOST:主库的IP。
    • MASTER_USER:主库中专门为复制创建的用户名。
    • MASTER_PASSWORD:主库中专门为复制创建的用户名的密码,密码为空则保留空字符串即可。
    • MASTER_PORT:主库监听的端口号。
    • MASTER_LOG_FILE:主库备份时的binlog文件。
    • MASTER_LOG_POS:主库备份文件保存的position号。
    • MASTER_CONNECT_RETRY:如果主从库之间网络环境较差,导致主从失联,那么从库将每隔一段时间尝试重新连接主库,一共尝试10次,该参数可以省略不写。

    6.模拟数据变更,看从库是否随之改变

    create database db11
    

    二. 部署MHA高可用架构

    1. 基于GTID的主从复制环境搭建

    环境准备:

    • 根据虚拟机新克隆4台虚拟机,保证MySQL是安装好的,且最好的是干净的环境
    • 在4台虚拟机分别执行,起名,好识别
    hostnamectl set-hostname db01
    hostnamectl set-hostname db02
    hostnamectl set-hostname db03
    hostnamectl set-hostname db04
    
    # 在分别执行bash
    # 保证通信正常 ping www.baidu.com
    

    1.1 配置4台虚拟机的MySQL的my.cnf文件

    db01 - 主库

    # 修改配置文件,添加错误日志和binlog日志,开启GTID,各数据库server_id要不同
    cat  > /etc/my.cnf <<EOF
    [mysqld]
    user=mysql
    basedir=/opt/software/mysql
    datadir=/data/mysql/3306/data
    server_id=204
    port=3306
    socket=/tmp/mysql.sock
    log_error=/data/mysql/3306/logs/mysql_error.log
    secure-file-priv=/tmp
    log_bin=/data/mysql/3306/logs/binlog/mysql-bin
    binlog_format=row
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    [mysql]
    socket=/tmp/mysql.sock
    prompt=db01 [\d]>
    user=root
    password=123
    EOF
    
    # 清空mysql数据库中的UUID,否则配置主从库会出错
    > /data/mysql/3306/data/auto.cnf
    
    # 创建存放binlog日志目录,授权,启动mysql服务,查看
    mkdir -p /data/mysql/3306/logs/binlog
    chown -R mysql:mysql /data/mysql/3306/logs/*
    systemctl start mysqld
    mysql -uroot -p123 -e "select @@server_id"
    

    db02 - 从库1

    cat  > /etc/my.cnf <<EOF
    [mysqld]
    user=mysql
    basedir=/opt/software/mysql
    datadir=/data/mysql/3306/data
    server_id=205
    port=3306
    socket=/tmp/mysql.sock
    log_error=/data/mysql/3306/logs/mysql_error.log
    secure-file-priv=/tmp
    log_bin=/data/mysql/3306/logs/binlog/mysql-bin
    binlog_format=row
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    [mysql]
    socket=/tmp/mysql.sock
    prompt=db02 [\d]>
    user=root
    password=123
    EOF
    
    
    > /data/mysql/3306/data/auto.cnf
    mkdir -p /data/mysql/3306/logs/binlog
    chown -R mysql:mysql /data/mysql/3306/logs/*
    systemctl start mysqld
    mysql -uroot -p123 -e "select @@server_id"
    
    

    db03 - 从库2

    cat  > /etc/my.cnf <<EOF
    [mysqld]
    user=mysql
    basedir=/opt/software/mysql
    datadir=/data/mysql/3306/data
    server_id=206
    port=3306
    socket=/tmp/mysql.sock
    log_error=/data/mysql/3306/logs/mysql_error.log
    secure-file-priv=/tmp
    log_bin=/data/mysql/3306/logs/binlog/mysql-bin
    binlog_format=row
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    [mysql]
    socket=/tmp/mysql.sock
    prompt=db03 [\d]>
    user=root
    password=123
    EOF
    
    > /data/mysql/3306/data/auto.cnf
    mkdir -p /data/mysql/3306/logs/binlog
    chown -R mysql:mysql /data/mysql/3306/logs/*
    systemctl start mysqld
    mysql -uroot -p123 -e "select @@server_id"
    
    

    db04 - 从库3

    cat  > /etc/my.cnf <<EOF
    [mysqld]
    user=mysql
    basedir=/opt/software/mysql
    datadir=/data/mysql/3306/data
    server_id=207
    port=3306
    socket=/tmp/mysql.sock
    log_error=/data/mysql/3306/logs/mysql_error.log
    secure-file-priv=/tmp
    log_bin=/data/mysql/3306/logs/binlog/mysql-bin
    binlog_format=row
    gtid-mode=on
    enforce-gtid-consistency=true
    log-slave-updates=1
    [mysql]
    socket=/tmp/mysql.sock
    prompt=db04 [\d]>
    user=root
    password=123
    EOF
    
    > /data/mysql/3306/data/auto.cnf
    mkdir -p /data/mysql/3306/logs/binlog
    chown -R mysql:mysql /data/mysql/3306/logs/*
    systemctl start mysqld
    mysql -uroot -p123 -e "select @@server_id"
    
    

    1.2 主库创建专用的复制用户

    # db01虚拟机服务器
    [root@db04 ~]# mysql
    grant replication slave on *.* to rs@'%' identified by '123';
    
    

    1.3 从库连接主库

    -- 1.分别在db02、db03、db04的MySQL中执行下面的代码:
    -- 连接
    CHANGE MASTER TO
      MASTER_HOST='192.168.189.138',
      MASTER_USER='rs',
      MASTER_PASSWORD='123',
      MASTER_PORT=3306,
      MASTER_AUTO_POSITION=1;
    -- 开启线程
    start slave;
    -- 查看状态
    show slave statusG
    
    -- 2.确认主从关系构建是否成功,在主库db01的MySQL执行
    show slave hosts;
    
    -- # 3. 我们现在只需要3台服务器,这里把db04服务器可以关闭,先断开和主库的主从复制关系,在db04中执行
    stop slave;
    reset salve all;
    
    
    

    2. 准备MHA高可用的环境

    1. 创建软连接

    MHA程序运行时,没有调用/etc/profile中的环境变量信息,而是调的/usr/bin中的相关软连接,所以,我们上来先把软件配置好。

    在所有的节点上建立软连接:

    ln -s /opt/software/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlog
    ln -s /opt/software/mysql/bin/mysql /usr/bin/mysql
    
    

    2. 各节点进行互信配置

    所谓互信就是通过配置密钥,让多个节点之间无密码访问。

    首先在各节点都要执行删除原来的.ssh

    rm -rf /root/.ssh
    
    

    然后,下面的操作在db01中进行操作:

    ssh-keygen   # 生成秘钥,一路回车
    cd /root/.ssh
    mv id_rsa.pub authorized_keys
    
    # 远程复制
    scp -r /root/.ssh 192.168.189.139:/root
    scp -r /root/.ssh 192.168.189.140:/root
    scp -r /root/.ssh 192.168.189.141:/root
    
    

    配置完成后,四个节点都要进行验证,在每个节点输入下面命令,都能返回对应节点的hostname就行了:

    ssh 192.168.189.138 hostname
    ssh 192.168.189.139 hostname
    ssh 192.168.189.140 hostname
    ssh 192.168.189.141 hostname
    
    # 上面执行验证时,根据提示输入yes或者回车。
    
    

    3.安装 MHA Node

    接下来,我们需要在各个节点上都安装上MHA Node软件。

    # 1.下载依赖包,各节点都要下载
    yum install -y perl-DBD-MySQL
    
    # 2.安装MHA Node软件
    rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm
    
    # 3.到了这一步,四台从节点都配置好了。我们先把db04节点停掉,后续择机使用,先使用一主二从的架构
    systemctl stop mysqld.service
    
    

    4.安装 MHA Manager

    我们只需要将MHA Manager软件包安装到主节点上,也就是db01节点。

    # 1.下载依赖包:
    yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
    
    # 2.安装MHA Manager软件包,我这里选择装在主库上,也就是db01上:
    rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm
    
    # 3.在主库上创建mha专用监控管理用户,只在主库创建即可, 它会自动复制到从库:
    -- 在主库上创建专用用户
    grant all privileges on *.* to mha@'%' identified by '123';
    
    -- 然后可以分别在从库中进行确认
    select user,host from mysql.user;
    
    # 4.在主库上创建mha manger的配置文件:
    # 4.1 创建配置文件和日志目录
    mkdir -p /etc/mha
    mkdir -p /var/log/mha/node1
    
    # 创建配置文件node1.cnf:
    cat  > /etc/mha/node1.cnf <<EOF
    [server default]
    manager_log=/var/log/mha/node1/manager        
    manager_workdir=/var/log/mha/node1            
    master_binlog_dir=/data/mysql/3306/logs/binlog 
    repl_user=rs
    repl_password=123
    user=mha                                   
    password=123                               
    ping_interval=2
    ssh_user=root                               
    [server1]                                   
    hostname=192.168.189.138
    port=3306                                  
    [server2]            
    hostname=192.168.189.139
    port=3306
    [server3]
    hostname=192.168.189.140
    port=3306
    EOF
    
    # 5.在主库中,使用MHA Manager提供的脚本验证各节点间的SSH互信:
    [root@db01 tmp]# masterha_check_ssh --conf=/etc/mha/node1.cnf
    Tue May 11 21:24:47 2021 - [info] All SSH connection tests passed successfully.
    -- 看到successfully就表示成功了
    
    

    5.启动manager

    # 1.验证主从状态,主库执行:is OK 才能启动manager
    [root@db01 tmp]# masterha_check_repl --conf=/etc/mha/node1.cnf
    ...
    MySQL Replication Health is OK.
    
    # 2.启动MHA Manager:
    nohup masterha_manager --conf=/etc/mha/node1.cnf --remove_dead_master_conf --ignore_last_failover  < /dev/null> /var/log/mha/node1/manager.log 2>&1 &
    
    # 3.检查MHA的工作状态,主库执行:
    masterha_check_status --conf=/etc/mha/node1.cnf
    
    # 4.关闭Manager进程
    masterha_stop --conf=/etc/mha/node1.cnf
    
    
    masterha_check_ssh --conf=/etc/mha/node1.cnf
    masterha_check_repl --conf=/etc/mha/node1.cnf
    nohup masterha_manager --conf=/etc/mha/node1.cnf --remove_dead_master_conf --ignore_last_failover  < /dev/null> /var/log/mha/node1/manager.log 2>&1 &
    masterha_check_status --conf=/etc/mha/node1.cnf
    
    masterha_stop --conf=/etc/mha/node1.cnf
    
    
    
    CHANGE MASTER TO MASTER_HOST='192.168.189.138', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rs', MASTER_PASSWORD='123';
    start slave;
    show slave status G
    
    stop slave;
    reset slave all;
    
    wmqnnsqlsggnfbij
    
    
    
    邮箱
    #!/usr/bin/env perl
    
    use strict;
    use warnings FATAL => 'all';
    use Email::Simple;
    use Email::Sender::Simple qw(sendmail);
    use Email::Sender::Transport::SMTP::TLS;
    use Getopt::Long;
    
    #new_master_host and new_slave_hosts are set only when recovering master succeeded
    my ( $dead_master_host, $new_master_host, $new_slave_hosts, $subject, $body );
    my $smtp='smtp.qq.com';
    my $mail_from='1921608594@qq.com';		
    my $mail_user='1921608594@qq.com';
    my $mail_pass='wmqnnsqlsggnfbij';
    my $mail_to='1921608594@qq.com';
    
    GetOptions(
      'orig_master_host=s' => $dead_master_host,
      'new_master_host=s'  => $new_master_host,
      'new_slave_hosts=s'  => $new_slave_hosts,
      'subject=s'          => $subject,
      'body=s'             => $body,
    );
    
    mailToContacts($smtp,$mail_from,$mail_user,$mail_pass,$mail_to,$subject,$body);
    mailToContacts();
    sub mailToContacts {
            my ( $smtp, $mail_from, $user, $passwd, $mail_to, $subject, $msg ) = @_;
                    open my $DEBUG, "> /var/log/mha/node1/mail.log"
            or die "Can't open the debug      file:$!
    ";
            my $transport = Email::Sender::Transport::SMTP::TLS->new(
                    host     => 'smtp.qq.com',		
                    port     => 25,			
                    username => '1921608594@qq.com',	
                    password => 'wmqnnsqlsggnfbij',		
                    );
    
            my $message = Email::Simple->create(
        header => [
            From           => '1921608594@qq.com',	
            To             => '1921608594@qq.com',	
            Subject        => 'MHA-manager(192.168.189.100) ERROR'
            ],
            body           => $body,				
    );
    sendmail( $message, {transport => $transport} );
        return 1;
    }
    # Do whatever you want here
    exit 0;
    
    
    
    备份节点
    [binlog1]
    # 声明10.0.0.207即db04作为备份节点
    hostname=192.168.189.141
    # 日志存储到一个新的目录中,注意,这个目录不要和原来的binlog目录重合。
    master_binlog_dir=/data/mysql/3306/logs/repl_binlog
    # 取消db04的选举权,只作为备份来用
    no_master=1
    
    [binlog1]
    hostname=192.168.189.141
    master_binlog_dir=/data/mysql/3306/logs/repl_binlog
    no_master=1
    
    
    # 必须手动cd到创建得二进制目录中去,再执行拉取命令
    cd /data/mysql/3306/logs/repl_binlog/
    mysqlbinlog  -R --host=192.168.189.138 --user=mha --password=123 --raw  --stop-never mysql-bin.000006 &
    
    
    
    
    读写分离:
    
    cat  > /usr/local/mysql-proxy/conf/test.cnf <<EOF
    [mysql-proxy]
    admin-username = user			
    admin-password = pwd
    proxy-backend-addresses = 192.168.189.138:3306
    proxy-read-only-backend-addresses = 192.168.189.139:3306,192.168.189.140:3306
    pwds = rs:3yb5jEku5h4=,mha:3yb5jEku5h4=,root:3yb5jEku5h4=
    daemon = true
    keepalive = true
    event-threads = 8
    log-level = message
    log-path = /usr/local/mysql-proxy/log
    sql-log=ON
    proxy-address = 0.0.0.0:33060
    admin-address = 0.0.0.0:2345
    charset=utf8
    EOF
    
    
    # 加密密码,这里正好我们将后续用到的两个密码"123"和"mah"加密
    [root@db01 ~]# /usr/local/mysql-proxy/bin/encrypt 123
    3yb5jEku5h4=
    [root@db01 ~]# /usr/local/mysql-proxy/bin/encrypt mha
    O2jBXONX098=
    
    # 根据test配置文件管理Atlas
    [root@db01 ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test start
    [root@db01 ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test restart
    [root@db01 ~]# /usr/local/mysql-proxy/bin/mysql-proxyd test stop
    
    
    mysql -uroot -p123 -h 192.168.189.138 -P 33060
    
    mysql -uuser -ppwd -h 192.168.189.138 -P 2345
    
    
  • 相关阅读:
    MySQL全文索引--转载
    提升接口tps
    数据库连接池了解和常用连接池对比
    SpringBoot跨域配置,解决跨域上传文件
    oss上传
    MySQL高级 之 explain
    spring cloud集群负载均衡
    Xmind日常操作
    产品经理应该懂点经济学
    初谈产品
  • 原文地址:https://www.cnblogs.com/jia-shu/p/14805555.html
Copyright © 2011-2022 走看看