1、编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份。
[root@CentOS-7-2 ~]# cat mysql_backup.sh
#!/bin/bash
BACKUP_DIR=/data/backup
USER=root
PASSWD=123456
[ -d $BACKUP_DIR ] || mkdir $BACKUP_DIR
while true;do
cat < Please choose the backup mode you need according to the number: 1.Mysqldump 2.Xtrabackup 3.Exit EOF read -p "Your choose: " choose case "$choose" in 1) mysqldump -u$USER -p$PASSWD -A -F -E -R --single-transaction --master-data=1 --flush-privileges --triggers --default-character-set=utf8 --hex-blob > $BACKUP_DIR/fullbak_$(date +%F_%T).sql echo "The backup to complete!!!" exit 0 ;; 2) innobackupex --user=$USER --password=$PASSWD $BACKUP_DIR echo "The backup to complete!!!" exit 0 ;; 3) echo "Bye!!!" && exit 10 ;; *) echo "You Choose Error !" && exit ;; esac done 2、配置Mysql主从同步 实验主机:两台centos7 主服务器:192.168.10.4 Mysql-Master 从服务器:192.168.10.5 Mysql-Slave 安装软件包: yum install mariadb-server -y 配置主服务器设置: [root@Mysql-Master ~]# vim /etc/my.cnf [mysqld] server-id=1 log-bin=/data/logbin/mysql 启动服务: [root@Mysql-Master ~]# systemctl start mariadb 配置同步账号,授权: MariaDB [(none)]> grant replication slave on *.* to slaveuser@'192.168.10.%' identified by 'slavepasswd'; 查看二进制文件位置: MariaDB [(none)]> show master logs ; 从服务器配置: [root@Mysql-Slave ~]# vim /etc/my.cnf [mysqld] server-id=2 启动服务: [root@Mysql-Slave ~]# systemctl start mariadb 使用有复制权限的用户账号连接至主服务器,并启动复制线程 MariaDB [(none)]> CHANGE MASTER TO -> MASTER_HOST='192.168.10.4', -> MASTER_USER='slaveuser', -> MASTER_PASSWORD='slavepasswd', -> MASTER_PORT=3306, -> MASTER_LOG_FILE='mysql.000001', -> MASTER_LOG_POS=407; MariaDB [(none)]> start slave; 查看主从状态: MariaDB [(none)]> show slave status\G; 测试:主服务器创建的数据库DB1,观察从服务器是否同步完成: 3、使用MHA实现Mysql高可用。 使用四台主机: manager:192.168.10.3 master:192.168.10.4 slave1:192.168.10.5 slave2:192.168.10.6 master,slave节点安装mariadb软件包: yum install -y mariadb-server 修改 master 的数据库配置文件来对其进行初始化配置 [root@Master ~]# vim /etc/my.cnf [mysqld] server-id=2 log-bin=master-log relay-log=relay-log skip_name_resolve 修改两个 slave 的数据库配置文件 [root@Slave1 ~]# vim /etc/my.cnf [mysqld] server-id=3 relay-log=relay-log log-bin=master-log read_only=ON relay_log_purge=0 skip_name_resolve log_slave_updates=1 [root@Slave2 ~]# vim /etc/my.cnf [mysqld] server-id=4 relay-log=relay-log log-bin=master-log read_only=ON relay_log_purge=0 skip_name_resolv log_slave_updates=1 配置一主多从复制架构 master节点配置: 授权同步账号 MariaDB [(none)]> grant replication slave,replication client on *.* to 'slave'@'192.168.10.%' identified by 'linux'; 授权管理账号: MariaDB [(none)]> grant all on *.* to 'mhaadmin'@'192.168.10.%' identified by 'mhapass'; Slave节点: MariaDB [(none)]> change master to master_host='192.168.10.4', -> master_user='slave', -> master_password='linux', -> master_log_file='master-log.000001', -> master_log_pos=245; Query OK, 0 rows affected (0.06 sec) 准备 ssh 是主从之间实现基于秘钥的免验证登录: 在Manager节点生成密钥对儿,将私钥文件及authorized_keys文件复制给余下的所有节点 在所有主机上执行: ssh-keygen -t rsa ssh-copy-id -i .ssh/id_rsa.pub [email protected] 将生成的authorized_keys文件发送至其他主机: scp authorized_keys [email protected]:~/.ssh/ scp authorized_keys [email protected]:~/.ssh/ scp authorized_keys [email protected]:~/.ssh/ 安装 MHA 包 Manager 节点需要安装:mha4mysql-manager-0.56-0.el6.noarch.rpm,mha4mysql-node-0.56-0.el6.noarch.rpm 其余节点安装:mha4mysql-node-0.56-0.el6.noarch.rpm 在管理机器上创建mha配置文件 [root@Manager ~]# mkdir /etc/mha [root@Manager ~]# vim /etc/mha/mha.cnf [server default] user=mhaadmin password=mhapass manager_workdir=/data/mastermha/app1/ manager_log=/data/mastermha/app1/manager.log remote_workdir=/data/mastermha/app1/ ssh_user=root repl_user=slave repl_password=linux ping_interval=1 [server1] hostname=192.168.10.4 candidate_master=1 [server2] hostname=192.168.10.5 candidate_master=1 [server3] hostname=192.168.10.6 Mha验证和启动 检测各节点间 ssh 互信通信配置是否 ok masterha_check_ssh --conf=/etc/mha/mha.cnf 检查管理的MySQL复制集群的连接配置参数是否OK masterha_check_repl --conf=/etc/mha/mha.cnf 启动MHA masterha_manager --conf=/etc/mha/mha.cnf 测试验证。关闭master所在虚拟机。