MySQL一键部署脚本 centos7.9+MySQL5.7.39

#!/bin/bash


### WARING: 
#1.执行此脚本请确保【/data/mysqldata】目录无其他文件,初始化会清空此目录 
#2.运行此脚本需内存8G以上,小于8G需调整my.cnf配置,否则无法启动mysql服务
#3.部署后mysql初始root密码为123456,部署完毕后可登录mysql自行修改
###


#####
#MySQL5.7.39数据库自动安装脚本
# Version:      1.0
# Author:       zzs
# Date:         2022-10-09
#####

#sed -i 's/\r//' install_mysql.sh

#安装包 脚本存放路径
# mkdir -p /data/package
installPackage=/data/package

#mysql 安装路径
installPath=/data/mysqldata
 
 
#my.cnf配置文件
mysqlcnf=/etc/my.cnf
 
 
#mysql serverid需要设置唯一的id,比如 ip+3位数字
mysqlServerid=100001
 
  
#mysql 端口
mysqlPort=3306


version=5.7.39




# 校验是否为ROOT用户
CheckRoot()
{
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install"
    exit 1
fi
clear
}
 
#优化文件最大打开数
DependFile()
{
 
 
if [ $( cat /etc/security/limits.conf  | grep "mysql" | wc -l )  -lt 1 ] ;then
cat >>/etc/security/limits.conf << EOF
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
mysql soft nproc 65536
mysql hard nproc 65536
mysql soft nofile 65536
mysql hard nofile 65536
EOF
 
 
fi
 
 
if [ -e /etc/security/limits.d/20-nproc.conf ];then
if [ $( cat /etc/security/limits.d/20-nproc.conf  | grep "mysql" | wc -l )  -lt 1 ] ;then
cat >>/etc/security/limits.d/20-nproc.conf<>/etc/security/limits.d/90-nproc.conf< libs --> clients --> server

rpm -ivh mysql-community-common-$version-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-$version-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-$version-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-$version-1.el7.x86_64.rpm 
rpm -ivh mysql-community-server-$version-1.el7.x86_64.rpm

echo -e "\e[31m #4.mysql安装完成 \e[0m"
}


#创建my.cnf
MakeMyCnf()
{
 
 
if  [ -e ${mysqlcnf} ] ;then
    mv  ${mysqlcnf}  ${mysqlcnf}"`date +%Y%m%d%H%M`"
    #rm ${mysqlcnf}
fi
 

cat >${mysqlcnf}</data/mysql_backup/mysql_xtrabackup.sh<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \${CURRENT_DATETIME} Begin Backup <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" >> \${BAK_LOG}

}

function full_backup() {

    if [[ ! -d \${BAK_FULL_DIR} ]]; then
        mkdir -p \${BAK_FULL_DIR}
    fi

    echo "*** FULL BACKUP Date : \${CURRENT_DATETIME}" >> \${BAK_FULL_DIR}/full_backup.date

    /usr/bin/innobackupex --defaults-file=\${MYSQL_CNF} --no-timestamp --user=\${MYSQL_USERNAME} --password=\${MYSQL_PASSWORD} --no-lock   \${BAK_FULL_DIR}    2>&1 >> \${BAK_LOG}


}


#每周周一到周六进行增量备份
function incr_backup() {

    CURRENT_INCR_DIR="\${BAK_WEEK_DIR}/INCR_\${CURRENT_DAY_OF_WEEK}"
    PREV_DAY_OF_WEEK=\$((\${CURRENT_DAY_OF_WEEK} - 1))
    BASE_DIR="\${BAK_WEEK_DIR}/INCR_\${PREV_DAY_OF_WEEK}"

    #如果不存在之前的增量,则使用全量路径作为增量的BASE
    #比如周一的时候
    if [[ ! -d \${BASE_DIR} ]];then
        BASE_DIR=\${BAK_FULL_DIR}
    fi

    #如果在此函数中,还没有BASE,则认为可能是在项目第一周执行
    #进行一次全量备份
    if [[  ! -d \${BASE_DIR} ]];then
        echo "*** \${BASE_DIR} as BASE_DIR is not exists!" >> \${BAK_LOG}
        echo "***  So Backup Processor into FULL BACKUP " >> \${BAK_LOG}
        full_backup
        exit \$?
    fi

    #如果存放增量数据的目录已经存在,这里进行添加时间戳处理(一天备份多次)
    if [[ -d \${CURRENT_INCR_DIR} ]];then
        CURRENT_INCR_DIR="\${CURRENT_INCR_DIR}_\${CURRENT_DATETIME}"
    fi

    #如果BASE_DIR 存在,则进行增量备份
    if [[ ! -d \${CURRENT_INCR_DIR} ]];then
        mkdir -p \${CURRENT_INCR_DIR}
    fi

    echo "*** INCR BACKUP Date : \${CURRENT_DATETIME}" >> \${CURRENT_INCR_DIR}/incr_backup.date

    /usr/bin/innobackupex --defaults-file=\${MYSQL_CNF}  --no-timestamp --user=\${MYSQL_USERNAME} --password=\${MYSQL_PASSWORD} --no-lock --incremental --incremental-basedir=\${BASE_DIR}   \${CURRENT_INCR_DIR}   2>&1 >> \${BAK_LOG}

}
#################main #################

write_start_log

clean_backup

#如果指定的全备时间 == 当前的时间,则执行全备
if [[ \${FULL_BAK_DAY_OF_WEEK} -eq \${CURRENT_DAY_OF_WEEK} ]];then
    full_backup
    exit \$?
fi

if [[ \${FULL_BAK_DAY_OF_WEEK} -ne \${CURRENT_DAY_OF_WEEK} ]];then
   incr_backup
   exit \$?
fi



EOF

[ ! -f /data/mysql_backup/backup.log ] && touch /data/mysql_backup/backup.log

echo "35 0 * * 6 /data/mysql_backup/mysql_xtrabackup.sh>>/data/mysql_backup/backup.log 2>&1 &" >> /var/spool/cron/root


echo -e "\e[31m #7. 备份设置完成,每周日全备,每日增备 \e[0m"
}


#接入zabbix监控
install_zabbix()
{

zabbix_agent=`rpm -qa | grep zabbix-agent`
zabbix_conf_dir=`find / -name zabbix_agentd.conf`
if [ -z "${zabbix_agent}" ]; then
    cd  $installPackage
    rpm -ivh zabbix-agent-3.4.15-1.el7.x86_64.rpm
fi

if [ -n "${zabbix_conf_dir}" ]; then
zabbix_key=`grep "UserParameter=mysql.statsvp"  ${zabbix_conf_dir}`
fi


if [ -n "${zabbix_conf_dir}" ]&&[ -z "${zabbix_key}" ] ; then
   sed -i '$a UserParameter=mysql.statsvp[*],/etc/zabbix/scripts/chk_mysql.sh $1' ${zabbix_conf_dir}

fi

mkdir -p /etc/zabbix/scripts
cat >/etc/zabbix/scripts/chk_mysql.sh</dev/null |cut -f2 -d":"|cut -f1 -d"T"\`
echo \$result
;;
Com_update)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null  |grep -w "Com_update"|cut -d"|" -f3\`
echo \$result
;;
Slow_queries)
result=\`\${MYSQL_CONN} status 2>/dev/null |cut -f5 -d":"|cut -f1 -d"O"\`
echo \$result
;;
Com_select)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_select"|cut -d"|" -f3\`
echo \$result
;;
Com_rollback)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_rollback"|cut -d"|" -f3\`
echo \$result
;;
Questions)
result=\`\${MYSQL_CONN} status 2>/dev/null |cut -f4 -d":"|cut -f1 -d"S"\`
echo \$result
;;
Com_insert)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_insert"|cut -d"|" -f3\`
echo \$result
;;
Com_delete)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_delete"|cut -d"|" -f3\`
echo \$result
;;
Com_commit)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_commit"|cut -d"|" -f3\`
echo \$result
;;
Bytes_sent)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Bytes_sent" |cut -d"|" -f3\`
echo \$result
;;
Bytes_received)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null  |grep -w "Bytes_received" |cut -d"|" -f3\`
echo \$result
;;
Com_begin)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null  |grep -w "Com_begin"|cut -d"|" -f3\`
echo \$result
;;
Threads_connected)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null  |grep -w "Threads_connected"|cut -d"|" -f3\`
echo \$result
;;
Threads_running)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null  |grep -w "Threads_running"|cut -d"|" -f3\`
echo \$result
;;
Innodb_row_lock_current_waits)
result=\`\${MYSQL_CONN} extended-status 2>/dev/null  |grep -w "Innodb_row_lock_current_waits"|cut -d"|" -f3\`
echo \$result
;;
Exec_long_time)
result=\`mysql -u\${MYSQL_USER} -p\${MYSQL_PWD} -h\${MYSQL_HOST} -P\${MYSQL_PORT} -e "SELECT count(*) FROM information_schema.processlist WHERE COMMAND<>'Sleep' AND TIME >60 AND info IS NOT NULL AND user NOT IN ('root','event_scheduler','system user ','master','repl','repl1') " 2> /dev/null |sed 1d \`
echo \$result
;;


*)
echo "Usage:\$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin|Threads_connected|Threads_running|Innodb_row_lock_current_waits|Aborted_connects|Exec_long_time)"
;;
esac
   
EOF
   
chmod 777 /etc/zabbix/scripts/chk_mysql.sh
systemctl restart zabbix-agent.service
echo -e "\e[31m #8. MySQL已接入zabbix脚本已创建 \e[0m"	
	
}

install_mysqld_exporter()
{
if [ -e /usr/local/mysqld_exporter/.my.cnf ]; 
then echo "mysqld_exporter已存在"
else  
cd $installPackage
tar -zxvf mysqld_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local/
ln -s /usr/local/mysqld_exporter-0.14.0.linux-amd64 /usr/local/mysqld_exporter
cat >/usr/local/mysqld_exporter/.my.cnf<

离线安装xtrabackup所需依赖包:

https://download.csdn.net/download/u013091109/86746897

你可能感兴趣的:(mysql,mysql,数据库,servlet,1024程序员节)