1.mysql自动安装脚本
ql-5.7.29-linux-glibc2.12-x86_64.tar.gz
GROUP_NAME=mysql #mysq组
USER_NAME=mysql #mysql用户
MYSQL_PORT=3307
MYSQLDB_HOME=/usr/local/mysql #mysql base目录
MYSQLDB_DATA_HOME=/usr/local/mysql/data #mysql data目录
MYSQL_DOWNLOAD_URL="https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz" #mysql 下载url
MYSQL_VERSION="mysql-5.7.29-linux-glibc2.12-x86_64" #mysql版本
ERROR_EXIT=65
CPU_NUMBERS=$(cat /proc/cpuinfo |grep "processor"|wc -l)
MYSQL_ID=`ifconfig enp0s8 | grep "inet " | awk -F. '{print $4}' | awk '{print $1}'`
COMPUTER_MEM=`free -m |grep "Mem"|awk '{print $2}'`
MYSQL_MEM=`expr $COMPUTER_MEM - $COMPUTER_MEM / 4`
echo "$MYSQL_MEM"
echo "====================================================="
echo "setup MySQL 5.7.29 on centos7_64bit "
echo "your computer is $CPU_NUMBERS processes ,mysql Memory is $MYSQL_MEM M"
echo "you will input mysql's root password and mysql's memory"
echo "====================================================="
sleep 1
read -n1 -p "are you sure setup[y/n]?" answer
case $answer in
Y | y)
echo
echo "start setup....";;
N | n)
echo
echo "Cancel setup...."
exit 10 ;;
*)
echo
echo "error input parameter....."
exit 11 ;;
esac
#check if user is root
if [ $(id -u) != "0" ];then
echo "Error: You must be root to run this script!"
exit 1
fi
#addGroup
if [ -z $(cat /etc/group|awk -F: '{print $1}'| grep -w "$GROUP_NAME") ]
then
groupadd $GROUP_NAME
if(( $? == 0 ))
then
echo "group $GROUP_NAME add sucessfully!"
fi
else
echo "$GROUP_NAME is exsits"
fi
#addUser
if [ -z $(cat /etc/passwd|awk -F: '{print $1}'| grep -w "$USER_NAME") ]
then
adduser -g $GROUP_NAME $USER_NAME
if (( $? == 0 ))
then
echo "user $USER_NAME add sucessfully!"
fi
else
echo "$USER_NAME is exsits"
fi
# for i in make gcc-c++ bison-devel ncurses-devel perl perl-devel wget gcc gcc-c++ bzip2 bzip2-devel bzip2-libs python-devel
# do
# yum -y install $i
# done
#downMySQL
# wget ${MYSQL_DOWNLOAD_URL} -P /tmp
# if(( $? == 0 ))
# then
# echo "MySQL DownLoad sucessfully!"
# else
# echo "MySQL DownLoad failed!"
# exit $ERROR_EXIT
# fi
#install mysql
if [ -d $MYSQLDB_HOME ]
then
echo "$MYSQLDB_HOME is exists!"
exit $ERROR_EXIT
fi
if [ -s /etc/my.cnf ]; then
mv /etc/my.cnf /etc/my.cnf.`date +%Y%m%d%H%M%S`.bak
fi
cd /tmp
tar xzvf ${MYSQL_VERSION}.tar.gz
mv ${MYSQL_VERSION} /usr/local/mysql
mkdir -p $MYSQLDB_DATA_HOME
mkdir -p $MYSQLDB_HOME/log
touch $MYSQLDB_HOME/log/mysql-error.log
touch $MYSQLDB_HOME/log/mysql-slow.log
chown -R mysql:mysql $MYSQLDB_HOME
chmod -R 755 $MYSQLDB_HOME/log
chmod -R 755 $MYSQLDB_DATA_HOME
echo "
[mysqld]
datadir=$MYSQLDB_DATA_HOME
basedir=$MYSQLDB_HOME
socket=$MYSQLDB_DATA_HOME/mysql.sock
port=$MYSQL_PORT
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server_id=1
gtid_mode=on
enforce_gtid_consistency=on
log-bin=salve
binlog_format=row
[mysqld_safe]
log-error=$MYSQLDB_HOME/log/mysql-error.log
pid-file=$MYSQLDB_DATA_HOME/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d" >/etc/my.cnf
#初始化数据库
cd $MYSQLDB_HOME/bin
./mysqld --user=mysql --initialize --console
#修改环境变量
cat >> /etc/profile <
2.xtrabackup备份
2020-07-02_16-34-14为全备,2020-07-02_16-35-01为增量备份1
----全备
innobackupex --defaults-file=/etc/my.cnf --socket=/usr/local/mysql/data/mysql.sock --user=root --port=3307 --password=123456 /home/mysql/backup/
----创建增量备份1(基于全备)
innobackupex --defaults-file=/etc/my.cnf --socket=/usr/local/mysql/data/mysql.sock --user=root --port=3307 --password=123456 --incremental-basedir=/home/mysql/backup/2020-07-02_16-34-14/ --incremental /home/mysql/backup/
----创建增量备份2(基于上次的增量备份)
innobackupex --defaults-file=/etc/my.cnf --socket=/usr/local/mysql/data/mysql.sock --user=root --port=3307 --password=123456 --incremental-basedir=/home/mysql/backup/2020-07-02_16-35-01/ --incremental /home/mysql/backup/
3.xtrabackup恢复
----增量备份的恢复大体为3个步骤
(1)恢复完全备份
(2)恢复增量备份到完全备份(开始恢复的增量备份要添加--redo-only参数,到最后一次增量备份去掉--redo-only参数)
(3)对整体的完全备份进行恢复,回滚那些未提交的数据
(4)恢复完全备份(注意这里一定要加--redo-only参数,该参数的意思是只应用xtrabackup日志中已提交的事务数据,不回滚还未提交的数据)
2020-07-02_16-34-14为全备,2020-07-02_16-35-01为增量备份1,2020-07-02_16-37-18为增量备份2
----恢复完全备份
innobackupex --apply-log --redo-only /home/mysql/backup/2020-07-02_16-34-14
----将增量备份1应用到完全备份
innobackupex --apply-log --redo-only /home/mysql/backup/2020-07-02_16-34-14 --incremental-dir=/home/mysql/backup/2020-07-02_16-35-01/
----将增量备份2应用到完全备份(注意恢复最后一个增量备份时需要去掉--redo-only参数,回滚xtrabackup日志中那些还未提交的数据)
innobackupex --apply-log /home/mysql/backup/2020-07-02_16-34-14 --incremental-dir=/home/mysql/backup/2020-07-02_16-37-18/
----把所有合在一起的完全备份整体进行一次apply操作,回滚未提交的数据
innobackupex --apply-log /home/mysql/backup/2020-07-02_16-34-14
----把恢复完的备份复制到数据库目录文件中,赋权,然后启动mysql数据库,检测数据正确性
/etc/init.d/mysql stop
mv /usr/local/mysql /usr/local/mysql_bak
mkdir /usr/local/mysql
innobackupex --defaults-file=/etc/my.cnf --copy-back --rsync /home/mysql/backup/2020-07-02_16-34-14
chown -R mysql:mysql /data/mysql
/etc/init.d/mysql start
4. gtid主备关系配置
4.1 主库创建账号
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl';
4.2 过滤掉已执行过的gtid
cat /xtrabackup_info |grep binlog_pos
cat xtrabackup_info |grep binlog_pos
binlog_pos = filename 'salve.000002', position '2025', GTID of the last change '649eea0c-bc28-11ea-b57b-005056b6352b:1-8'
4.3 查看slave已执行的gtid是否为空,如果不为空,需要执行reset MASTER进行清理,否则无法设置gtid。
mysql> show master status\G;
*************************** 1. row ***************************
File: salve.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 649eea0c-bc28-11ea-b57b-005056b6352b:1-8
1 row in set (0.00 sec)
4.4 执行reset master
4.5 执行GTID_PURGED
SET @MYSQLDUMP_TEMP_LOG_BIN = @@SESSION.SQL_LOG_BIN;
SET @@SESSION.SQL_LOG_BIN= 0;
SET @@GLOBAL.GTID_PURGED='649eea0c-bc28-11ea-b57b-005056b6352b:1-8';
SET @@SESSION.SQL_LOG_BIN = @MYSQLDUMP_TEMP_LOG_BIN;
4.6 change master
change master to
master_host='xxxx',
master_port=3307,
master_user='repl',
master_password='repl',
MASTER_AUTO_POSITION = 1;
4.7 start slave ;
4.8 show slave status\G;
注意事项:从库安装mysql的时候1脚本中的server_id=1需要替换成server_id=2