马哥Linux第十五周
2020-05-29 本文已影响0人
Liang_JC
Q1、编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份。
vim mysql_full_backup.sh
#!/bin/bash
echo "Please choose database backup mode"
echo "1) mysqldump"
echo "2) xtrabackup"
echo
read -p "which your choose: " MODE
read -p "Please input backup directory:" DIR
if [ "$MODE" == "1" ];then
[ -d $DIR ] || mkdir $DIR
rpm -q mariadb &> /dev/null || yum install mariadb -y > /dev/null
BACKUP_TIME=`date +%Y-%m-%d_%H%M%S`
mysqldump -uroot -A -F -E -R --single-transaction --master-data=1 --flush-privileges --triggers --default-character-set=utf8 --hex-blob > $DIR/fullbak_${BACKUP_TIME}.sql
[ `echo $?` ] && echo;echo "Backup path: $DIR/fullbak_${BACKUP_TIME}.sql";echo "Backup Complete"!!!
elif [ "$MODE" == "2" ];then
[ -d $DIR ] || mkdir $DIR
rpm -q percona-xtrabackup &> /dev/null || yum install percona-xtrabackup -y > /dev/null
xtrabackup --backup --target-dir=$DIR &> /dev/null
[ `echo $?` ] && echo;echo "Backup path: $DIR";echo "Backup Complete"!!!
else
echo
echo "choose mode error"
exit 5
fi
Q2、配置Mysql主从同步
#master
[root@rs1 ~]# yum install mariadb-server -y
[root@rs1 ~]# vim /etc/my.cnf
[mysqld]
server_id=27
log_bin
skip_name_resolve
binlog_format=row
[root@rs1 ~]# systemctl start mariadb
[root@rs1 ~]# mysql -e "grant replication slave on *.* to repluser@'192.168.37.%' identified by 'centos'"
[root@rs1 ~]# mysql -e "flush privileges"
[root@rs1 ~]# mysqldump -A -F -E -R --single-transaction --master-data=1 --flush-privileges --triggers --hex-blob > /data/mysql/master_all.sql
scp /data/mysql/master_all.sql root@192.168.37.37:/data
[root@rs1 ~]# mysql
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name | File_size |
+--------------------+-----------+
| mariadb-bin.000001 | 520 |
| mariadb-bin.000002 | 245 |
+--------------------+-----------+
2 rows in set (0.00 sec)
#slave
[root@rs2 ~]# yum install mariadb-server -y
[root@rs2 ~]# vim /etc/my.cnf
[mysqld]
server-id=37
log-bin
read-only
skip_name_resolve
binlog_format=row
[root@rs2 ~]# vim /data/master_all.sql
CHANGE MASTER TO
MASTER_HOST='192.168.37.27',
MASTER_USER='repluser',
MASTER_PASSWORD='centos',
MASTER_PORT=3306,
MASTER_LOG_FILE='mariadb-bin.000002', MASTER_LOG_POS=245;
[root@rs2 ~]# systemctl start mariadb
[root@rs2 ~]# mysql
MariaDB [none]> stop slave;
MariaDB [none]> source /data/master_all.sql
MariaDB [none]> start slave;
MariaDB [none]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.37.27
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000002
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 531
Relay_Master_Log_File: mariadb-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 245
Relay_Log_Space: 827
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 27
1 row in set (0.00 sec)
#测试主从同步
#master上创建数据库
MariaDB [(none)]> create database magedu;
Query OK, 1 row affected (0.00 sec)
#slave上查看
ariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| magedu |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
Q3、使用MHA实现Mysql高可用
#环境:4台机器
#mha-master,mysql-master,mysql-slave1,mysql-slave2
首先:四台机器都装上mysql
yum install mariadb-server -y
#mysql-master
[root@mysql-master ~]# vim /etc/my.cnf
[mysqld]
server_id=17
log_bin
binlog_format=row
skip_name_resolve
[root@mysql-master ~]# systemctl start mariadb
[root@mysql-master ~]# mysql -e "grant replication slave on *.* to repluser@'192.168.37.%' identified by 'magedu'"
[root@mysql-master ~]# mysql -e "grant all on *.* to mhauser@'192.168.37.%' identified by 'magedu'"
[root@mysql-master ~]# mysql -e "flush privileges"
[root@mysql-master ~]# mysql -e "show master status"
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000001 | 475 | | |
+--------------------+----------+--------------+------------------+
[root@mysql-master ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y
#slave1
[root@rs1 ~]# vim /etc/my.cnf
[mysqld]
skip_name_resolve
server_id=27
log_bin
binlog_format=row
read-only
relay_log_purge=0
[root@rs1 ~]# systemctl start mariadb
[root@rs1 ~]# mysql
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.37.17',
-> MASTER_USER='repluser', MASTER_PASSWORD='magedu',
-> MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
[root@rs1 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y
#slave2
[root@rs2 ~]# vim /etc/my.cnf
[mysqld]
skip_name_resolve
server_id=37
log_bin
binlog_format=row
read-only
relay_log_purge=0
[root@rs2 ~]# systemctl start mariadb
[root@rs2 ~]# mysql
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.37.17',
-> MASTER_USER='repluser', MASTER_PASSWORD='magedu',
-> MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
[root@rs2 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm -y
#mha-master
[root@mha ~]# yum install mha4mysql-manager-0.56-0.el6.noarch.rpm mha4mysql-node-0.56-0.el6.noarch.rpm -y #需要epel源
[root@mha ~]# ssh-keygen
[root@mha ~]# ssh-copy-id 192.168.37.7
[root@mha ~]# scp -r .ssh/ 192.168.37.17:/root/
[root@mha ~]# scp -r .ssh/ 192.168.37.27:/root/
[root@mha ~]# scp -r .ssh/ 192.168.37.37:/root/
[root@mha ~]# mkdir /etc/mha
[root@mha ~]# vim /etc/mha/app1.cnf
[root@mha-master ~]# vim /etc/mha/app1.cnf
[server default]
user=mhauser
password=magedu
manager_workdir=/data/mastermha/app1/
manager_log=/data/mastermha/app1/manager.log
remote_workdir=/data/mastermha/app1/
ssh_user=root
repl_user=repluser
repl_password=magedu
ping_interval=1
[server1]
hostname=192.168.37.17
candidate_master=1
[server2]
hostname=192.168.37.27
candidate_master=1
[server3]
hostname=192.168.37.37
candidate_master=1
[root@mha ~]# masterha_check_ssh --conf=/etc/mha/app1.cnf #检查ssh连接
[root@mha ~]# masterha_check_repl --conf=/etc/mha/app1.cnf #检查同步状况
#在前台启动,一次性的,发生故障后把从变主后任务就结束了
[root@mha ~]# masterha_manager --conf=/etc/mha/app1.cnf #启动服务
image.png
此时断电mysql-master,可以看到任务已停止
image.png查看日志可以看到27变为主了
[root@mha ~]# tail /data/mastermha/app1/manager.log