MySQL8二进制安装脚本
#!/bin/bash
set -o errexit
set -eo pipefail
#定义变量
URL=https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
TAR=${URL##*/}
MySQL_version=${TAR%-linux*}
MySQL_path=/usr/local/
MySQL_link=${MySQL_path}mysql
DATA=/mysql
PID=${DATA}/pid
MySQL_TMP=${DATA}/tmp
LOG=${DATA}/log
BINLOG=${DATA}/binlog
MySQL_data=${DATA}/data
MySQL_user=mysql
MySQL_group=mysql
PORT=3306
CONF_FILE=/etc/my.cnf
SER_ID=`ip a s eth0|awk -F "[ /.]+" '/inet /{print $7}'`
#检测系统是否有残留mariadb
echo -e "\033[1;31m检测系统是否有残留mariadb\033[0m"
sleep 3
. /etc/os-release
if [[ $ID="centos" || $ID="rocky" ]];then
if rpm -qa "mariadb*" &>/dev/null;then
yum remove mariadb* -y &>/dev/null
rm -f /etc/my.cnf &>/dev/null
echo -e "\033[1;32m检测系统残留mariadb已清理\033[0m"
fi
else
if dpkg -l "mariadb*" &>/dev/null;then
apt –purge remove "mariadb*" &>/dev/null
echo -e "\033[1;32m检测系统残留mariadb已清理\033[0m"
fi
fi
#安装依赖
echo -e "\033[1;32m正在安装依赖\033[0m"
if [[ $ID="centos" || $ID="rocky" ]];then
yum -y install tar libaio-devel gcc-c++ numactl-devel libncurses.so.5 &>/dev/null
ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5 || exit
echo -e "\033[1;33m依赖安装完成\033[0m"
else
apt -y install tar libaio1 libaio-dev libncurses5-dev &>/dev/null
echo -e "\033[1;33m依赖安装完成\033[0m"
fi
#创建Mysql系统用户
echo -e "\033[1;31m正在安装创建MySQL系统用户\033[0m"
sleep 2
if ! id ${MySQL_user} &>/dev/null ;then
useradd -r $MySQL_user -s /sbin/nologin &>/dev/null
echo -e "\033[1;32mMySQL系统用户创建完成\033[0m"
else
echo -e "\033[1;35m用户已存在\033[0m"
fi
#下载并解压MySQL
echo -e "\033[1;31m正在下载MySQL8.0.27,请稍后\033[0m"
if [ ! -f ./$TAR ];then
curl -# -Lo ./${TAR} ${URL}/${TAR}
else
echo -e "\033[1;35m${TAR}已存在\033[0m"
fi
echo -e "\033[1;31m正在解压MySQL8.0.27,请稍后\033[0m"
if [ ! -d ${MySQL_path}${MySQL_version} ];then
mkdir -p ${MySQL_path}${MySQL_version} &>/dev/null
tar -xf ./${TAR} -C ${MySQL_path}${MySQL_version} --strip-components=1 &>/dev/null
else
echo -e "\033[1;35m${MySQL_version}已存在\033[0m"
fi
#创建数据目录并授权
echo -e "\033[1;31m正在创建数据目录并授权,请稍后\033[0m"
sleep 2
ln -snf ${MySQL_path}${MySQL_version} ${MySQL_link}
chown -R ${MySQL_user}.${MySQL_group} ${MySQL_path}${MySQL_version}
if [[ ! -d ${DATA} ]];then
mkdir -p ${PID} ${MySQL_TMP} ${MySQL_data} ${LOG} ${BINLOG}&>/dev/null && touch ${LOG}/log.err && chown -R ${MySQL_user}.${MySQL_group} ${DATA}
else
find ${DATA}/* -name "*" |xargs rm -rf && mkdir -p ${PID} ${MySQL_TMP} ${MySQL_data} ${LOG} ${BINLOG} &>/dev/null \
&& touch ${LOG}/log.err && chown -R ${MySQL_user}.${MySQL_group} ${DATA}
fi
echo -e "\033[1;32m数据目录并授权已完成\033[0m"
#配置环境变量
echo -e "\033[1;32m正在配置环境变量,请稍后\033[0m"
export PATH=${PATH}:${MySQL_link}/bin
echo "export PATH=\$PATH:${MySQL_link}/bin" >> /etc/profile
source /etc/profile
sleep 2
#写入配置文件和初始化数据库
echo -e "\033[1;32m正在写入配置文件和初始化数据库,请稍后\033[0m"
config(){
cat>${CONF_FILE}<<EOF
[mysqld]
server-id=${SER_ID}
port=${PORT}
skip-name-resolve
user=${MySQL_user}
socket=${MySQL_TMP}/mysql.sock
pid-file=${PID}/mysql.pid
datadir=${MySQL_data}
basedir=${MySQL_link}
log-bin=${BINLOG}/bin_log
log-error=${LOG}/log.err
[mysql]
socket=${MySQL_TMP}/mysql.sock
EOF
}
SYSTEM_scripts(){
cat >/etc/systemd/system/mysqld.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=${MySQL_user}
Group=${MySQL_group}
ExecStart=${MySQL_link}/bin/mysqld_safe --defaults-file=${CONF_FILE}
LimitNOFILE = 65536
LimitNPROC = 65536
EOF
}
sleep 1
#初始化数据库
PS3=请输入:
select name in "不生成MySQL登录密码(空密码登录)" "生成MySQL登录随机密码"
do
case $name in
"不生成MySQL登录密码(空密码登录)")
Initialize=initialize-insecure
break
;;
"生成MySQL登录随机密码")
Initialize=initialize
break
;;
*)
echo "输入错误请重新输入"
esac
done
mysqld --${Initialize} --datadir=${MySQL_data} --basedir=${MySQL_path}${MySQL_version} --user=${MySQL_user} &>./initialize.log
rc=$?
sleep 1
if [[ $rc = 0 ]];then
echo -e "\033[1;32m数据库初始化完成,正在写入配置文件\033[0m"
sleep 1
config
SYSTEM_scripts
cp ${MySQL_link}/support-files/mysql.server /etc/init.d/mysqld
sleep 1
systemctl daemon-reload
sleep 3
echo -e "\033[1;34m请使用systemctl命令或者/etc/init.d/mysqld start stop 启动关闭mysqld\033[0m"
echo -e "\033[1;34m如果启动mysqld失败请查看脚本目录下的initialize.log初始化信息文件和${LOG}下的log.err文件\033[0m"
echo -e "\033[1;34m请退出当前shell重新进入或者source /etc/profile后 方可使用mysql客户端命令\033[0m"
echo -e "\033[1;34m如果初始化时选择了生成随机密码,请到当前脚本目录下的initialize.log初始化信息文件中查看\033[0m"
else
echo -e "\033[1;32m数据库初始化失败,请查看脚本目录下的initialize.log初始化信息文件文件和${LOG}下的log.err文件\033[0m"
exit
fi
本文是原创文章,采用 CC BY-NC-SA 许可协议,完整转载请注明来自 半城小栈
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果