Linux基础及总结15之MySQL二

实现使用mysqldump、xtraback工具对MySQL备份,MySQL主从架构搭建,MHA实现mysql高可用搭建
1、编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份。
vim mysqlbackup.sh
#!/usr/bin/env bash
mkdir -p /data/{mysqldump,xtrabackup}
read -p "Please choose a mysql backup tool(mysqldump or xtrabackup): " tool
if [ `echo $tool|tr '[A-Z]' '[a-z]'`=='mysqldump'];then
        for db in `/usr/local/mysql/bin/mysql -uroot -p1q2w3e4r -e 'show databases'|grep   -Ev '^(information_schema|performance_schema|Database)$'`;do     /usr/local/mysql/bin/mysqldump -B -F -E -R $db --single-transaction --master-data=1 --    flush-privileges|gzip  > /data/mysqldump/$db.sql.gz;done
        if [ `echo $?` == 0 ];then
                echo "mysqldump backup data successfully"
        fi
  elif [ `echo $tool|tr '[A-Z]' '[a-z]'`='xtrabackup' ];then
        mkdir -p /data/xtrabackup/`date '+%F'`
        /usr/bin/xtrabackup     --backup --target-dir=/data/xtrabackup/`date '+%F'`
        if [ `echo $?` == 0 ];then
                echo "xtrabackup backup data successfully"
        fi
  else
   echo "You entered incorrectly, please enter mysqldump or xtrabackup for backup."
fi 
2、配置Mysql主从同步
1)在3台主机上分别安装mariadb,这里使用的mariadb-10.2.31二进制版本,安装过程不再详述
      192.168.2.239  mariadb-master
      192.168.2.240  mariadb-slave1
      192.168.2.241  mariadb-slave2
2)mysql主服务器配置,编辑mysql配置文件/etc/my.cnf,在mysqld下添加如下选项
    vim /etc/my.cnf
    [mysqld]
    server_id = 1                #主从唯一标识,主从架构中的mysql实例该选项值必须唯一
    log-bin _ mysql-bin       #开启二进制日志文件
3)登录mysql master实例创建主从同步账号
      create user 'repl'@'192.168.2.%' identified by 'repl';
      grant replication slave on *.* to 'repl'@'192.168.2.%';
      flush privileges;
      
       重启mariadb服务
        systemctl restart mysqld
       登录数据库查看二进制文件及位置
        MariaDB [(none)]> show master logs;
          +------------------+-----------+
          | Log_name         | File_size |
          +------------------+-----------+
          | mysql-bin.000001 |       375 |
          | mysql-bin.000002 |     1968 |
          +------------------+-----------+


  
4)mysql从节点配置 (多个从节点使用相同的方法进行配置)
     vim /etc/my.cnf  增加如下选项
     [mysqld]
      server_id = 2
      relay_log = relay-log        #启用中继日志
      read_only                         #mysql的从节点开启只读
    
    重启mysql服务
      systemctl restart mysqld

  使用有复制权限的用户账号连接至主服务器,并启动复制线程
      CHANGE MASTER TO
                MASTER_HOST='192.168.2.239', 
                MASTER_USER='repl',
                MASTER_PASSWORD='repl',
                MASTER_PORT=3306,
                MASTER_LOG_FILE='mysql-bin.000002',
                MASTER_LOG_POS=1968;
    启动slave的sql和IO线程
      MariaDB [(none)]> start slave;
  
  查询slave的sql和IO线程是否正常
    MariaDB [(none)]> show slave status \G

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.239
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1968
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

5)在主节点上导入数据验证主从数据是否同步
    mysql-master上导入hellodb库
    MariaDB [hellodb]> source /root/hellodb_innodb.sql

    mysql-salve从节点检查数据同步正常
    MariaDB [(none)]> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | hellodb            |
      | information_schema |
      | mysql              |
      | performance_schema |
      | test               |
      +--------------------+
3、使用MHA实现Mysql高可用。
192.168.2.238    mha-manager
192.168.2.239    mysql-master
192.168.2.240    mysql-slave1
192.168.2.241    mysql-slave2

1)mysql主服务器配置,编辑mysql配置文件/etc/my.cnf,在mysqld下添加如下选项
    vim /etc/my.cnf
    [mysqld]
    server_id = 1                #主从唯一标识,主从架构中的mysql实例该选项值必须唯一
    log-bin _ mysql-bin       #开启二进制日志文件
    skip_name_resolve=1
2)登录mysql master实例创建主从同步账号,在mysql主从集群所有节点执行此操作
      create user 'repl'@'192.168.2.%' identified by 'repl';
      grant replication slave on *.* to 'repl'@'192.168.2.%';
      flush privileges;
      
       重启mariadb服务
        systemctl restart mysqld
       登录数据库查看二进制文件及位置
        MariaDB [(none)]> show master logs;
          +------------------+-----------+
          | Log_name         | File_size |
          +------------------+-----------+
          | mysql-bin.000001 |       375 |
          | mysql-bin.000002 |     1968 |
          +------------------+-----------+


  
3)mysql从节点配置 (多个从节点使用相同的方法进行配置)
     vim /etc/my.cnf  增加如下选项
     [mysqld]
      server_id = 2
      relay_log = relay-log        #启用中继日志
      read_only                         #mysql的从节点开启只读
      relay_log_purge=0
  skip_name_resolve=1
    
    重启mysql服务
      systemctl restart mysqld

  使用有复制权限的用户账号连接至主服务器,并启动复制线程
      CHANGE MASTER TO
                MASTER_HOST='192.168.2.239', 
                MASTER_USER='repl',
                MASTER_PASSWORD='repl',
                MASTER_PORT=3306,
                MASTER_LOG_FILE='mysql-bin.000002',
                MASTER_LOG_POS=1968;
    启动slave的sql和IO线程
      MariaDB [(none)]> start slave;
  
  查询slave的sql和IO线程是否正常
    MariaDB [(none)]> show slave status \G

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.239
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1968
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

4)在主节点上导入数据验证主从数据是否同步
    mysql-master上导入hellodb库
    MariaDB [hellodb]> source /root/hellodb_innodb.sql

    mysql-salve从节点检查数据同步正常
    MariaDB [(none)]> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | hellodb            |
      | information_schema |
      | mysql              |
      | performance_schema |
      | test               |
      +--------------------+
5)在MHA-Manager管理几点上安装依赖包和mha4mysql-manager  mha4mysql-node包,在mha集群的其他节点上安装mha4mysql-node包。
  yum install -y perl-Mail-Sender perl-Email-Date-Format perl-MIME-Types perl-MIME-Lite perl-Parallel-ForkManager perl-Mail-Sendmail perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-YAML-Tiny perl-PAR-Dist perl-Module-ScanDeps perl-Module-CoreList perl-Module-Build perl-CPAN perl-YAML perl-CPANPLUS perl-File-Remove perl-Module-Install
    yum -y install mha4mysql-manager-0.58-0.el7.centos.noarch.rpm mha4mysql-node-0.58-0.el7.centos.noarch.rpm
    软件包下载地址:
            https://github.com/yoshinorim/mha4mysql-manager
            https://github.com/yoshinorim/mha4mysql-node

6)在集群所有的节点上相互配置ssh基于key验证,这里配置集群所有的主机使用同一个公钥和私钥
  ssh-keygen
  ssh-copy-id 127.0.0.1
  scp -r /root/.ssh  root@remoteIp:/root/
7)在管理节点建立配置文件
      创建存放配置文件的目录 mkdir -p /etc/mastermha
      vim /etc/mastermha/app1.conf
      [server default]
      user=mhauser
      password=mhauser
      manager_workdir=/data/mastermha/app1/
      manager_log=/data/mastermha/app1/manager.log
      remote_workdir=/data/mastermha/app1/
      ssh_user=root
      repl_user=repl
      repl_password=repl
      ping_interval=1
      [server1]
      hostname=192.168.2.239
      candidate_master=1
      [server2]
      hostname=192.168.2.240
      candidate_master=1
      [server3]
      hostname=192.168.2.241
8)登录mysql master创建mha管理账号
      create user 'mhauser'@'192.168.2.%' identified by 'mhauser';
      grant all on *.* to 'mhauser'@'192.168.2.%';
9)使用的是二进制方式安装mysql,需要设置mysql命令的软链接,否则执行检查会提示命令找不到错误。
    ln -s /usr/local/mysql/bin/mysqlbinlog /usr/local/bin/mysqlbinlog
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
10)Mha验证和启动
      验证节点之间ssh key相互登录
      masterha_check_ssh --conf=/etc/mastermha/app1.cnf
      显示如下结果为成功
      Sun Feb 23 16:17:15 2020 - [info] All SSH connection tests passed successfully.

      mha主从复制测试
      masterha_check_repl --conf=/etc/mastermha/app1.cnf
      Sat Feb 29 15:13:16 2020 - [info] Checking replication health on 192.168.2.240..
      Sat Feb 29 15:13:16 2020 - [info]  ok.
    Sat Feb 29 15:13:16 2020 - [info] Checking replication health on 192.168.2.241..
    Sat Feb 29 15:13:16 2020 - [info]  ok.
    Sat Feb 29 15:13:16 2020 - [warning] master_ip_failover_script is not defined.
    Sat Feb 29 15:13:16 2020 - [warning] shutdown_script is not defined.
    Sat Feb 29 15:13:16 2020 - [info] Got exit code 0 (Not master dead).

    MySQL Replication Health is OK.

      启动mha,使用后台运行
      nohup masterha_manager --conf=/etc/mastermha/app1.cnf &> /dev/null &
      ps -aux|grep mha
      root     24329  2.6  0.4 207436 21948 pts/1    S    15:17   0:00 perl /usr/bin/masterha_manager --conf=/etc/mastermha/app1.cnf

11)模拟2.239主服务器故障,集群自动提升其他节点为主节点,停止2.239mysql服务,查看mha日志自动将配置文件中定义的候选节点240替身为mysql master。
Started automated(non-interactive) failover.
The latest slave 192.168.2.240(192.168.2.240:3306) has all relay logs for recovery.
Selected 192.168.2.240(192.168.2.240:3306) as a new master.
192.168.2.240(192.168.2.240:3306): OK: Applying all logs succeeded.
192.168.2.241(192.168.2.241:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
192.168.2.241(192.168.2.241:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.2.240(192.168.2.240:3306)
192.168.2.240(192.168.2.240:3306): Resetting slave info succeeded.
Master failover to 192.168.2.240(192.168.2.240:3306) completed successfully.

你可能感兴趣的:(Linux基础及总结15之MySQL二)