第十五周作业(mysql备份,主从同步,高可用)

1、编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份。

[root@centos7 ~]# cat backup_mysql.sh 
#!/bin/bash

BACKUP_DIR=/data/backup
USER=root
PASSWD=root

[ -d $BACKUP_DIR ] || mkdir $BACKUP_DIR

while true;do

cat < $BACKUP_DIR/fullbak_$(date +%F_%T).sql
    ;;
   2)
    innobackupex --user=$USER --password=$PASSWD $BACKUP_DIR
    ;;
   3)
    echo "Bye!" && exit 2
    ;;
   *)
    echo "Choose Error !" && exit 3
    ;;
esac

done

2、配置Mysql主从同步

主机:两台,一台当主服务器master(192.168.27.27),一台当从服务器(192.168.27.37)

(1)安装启动数据库,并设置密码

yum -y install mysql mysql-server mysql-devel
service mysqld restart   #启动数据库
mysqladmin -u root password '123456'   #设置root用户密码

(2)修改主库my.cnf主要设置个不一样的ID,以及同步的数据库的名字,我一般用vim 来完成,vim /etc/my.cnf 在[mysqld]中加入内容如下

#主数据库端ID号
server_id = 1           
 #开启二进制日志                  
log-bin = mysql-bin    
#需要复制的数据库名,如果复制多个数据库,重复设置这个选项即可                  
binlog-do-db = db        
#将从服务器从主服务器收到的更新记入到从服务器自己的二进制日志文件中                 
log-slave-updates                        
#控制binlog的写入频率。每执行多少次事务写入一次(这个参数性能消耗很大,但可减小MySQL崩溃造成的损失) 
sync_binlog = 1                    
#这个参数一般用在主主同步中,用来错开自增值, 防止键值冲突
auto_increment_offset = 1           
#这个参数一般用在主主同步中,用来错开自增值, 防止键值冲突
auto_increment_increment = 1            
#二进制日志自动删除的天数,默认值为0,表示“没有自动删除”,启动时和二进制日志循环时可能删除  
expire_logs_days = 7                    
#将函数复制到slave  
log_bin_trust_function_creators = 1       

(3)登录主库赋予从库权限账号,允许用户在主库上读取日志(用户名:admin,密码:123456)

mysql -uroot -p123456
#创建slave账号admin,密码123456
grant replication slave on *.* to 'admin'@'192.168.100.20' identified by '123456';
#更新数据库权限
mysql>flush privileges;

(4)锁主库表,显示主库信息

flush tables with read lock;
显示主库信息
show master status;
mysql> show master status; 
+---------------+----------+--------------+------------------+ 
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | 
+---------------+----------+--------------+------------------+ 
| binlog.000002 |      106 | haocheng   |                  | 
+---------------+----------+--------------+------------------+ 
1 row in set (0.00 sec) 
(记录file、position,从库设置将会用到)

(5)修改从库my.cnf ,如下:

server_id = 2
log-bin = mysql-bin
log-slave-updates
sync_binlog = 0
#log buffer将每秒一次地写入log file中,并且log file的flush(刷到磁盘)操作同时进行。该模式下在事务提交的时候,不会主动触发写入磁盘的操作
innodb_flush_log_at_trx_commit = 0        
#指定slave要复制哪个库
replicate-do-db = db         
#MySQL主从复制的时候,当Master和Slave之间的网络中断,但是Master和Slave无法察觉的情况下(比如防火墙或者路由问题)。Slave会等待slave_net_timeout设置的秒数后,才能认为网络出现故障,然后才会重连并且追赶这段时间主库的数据
slave-net-timeout = 60                    
log_bin_trust_function_creators = 1
#master-host=192.168.100.10
#master-user=admin
#master-password=123456
(注释内容为了方便以后管理)

(6)在从库上设置同步

#执行同步命令,设置主服务器ip,同步账号密码,同步位置
mysql -u root -p123456
mysql>slave stop;
mysql>change master to master_host='10.10.20.111',master_user='account',master_password='123456',master_log_file='mysql-bin.000033',master_log_pos=337523;
#开启同步功能
mysql>start slave;

(7)查看从服务器状态

mysql>show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.10.20.111
                  Master_User: account
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000033
          Read_Master_Log_Pos: 337523
               Relay_Log_File: db2-relay-bin.000002
                Relay_Log_Pos: 337686
        Relay_Master_Log_File: mysql-bin.000033
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
          ...

Slave_IO_Running及Slave_SQL_Running进程必须正常运行,即Yes状态,否则说明同步失败
若失败查看mysql错误日志中具体报错详情来进行问题定位
最后可以去主服务器上的数据库中创建表或者更新表数据来测试同步

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

模拟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.

你可能感兴趣的:(第十五周作业(mysql备份,主从同步,高可用))