Linux下的mysql自动化安装脚本
脚本说明:
linux平台下的mysql自动化安装脚本,使用该脚本你可以对mysql安装包进行一键安装。
使用方法:
将脚本和mysql安装包都方式在/root目录下。执行bash install.sh即可进行一键安装。
示例图:
代码开源:
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#字体颜色
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Yellow_font_prefix="\033[33m" && Font_color_suffix="\033[0m"
#提示信息
Info="${Green_font_prefix}[信息]${Font_color_suffix}"
Error="${Red_font_prefix}[错误]${Font_color_suffix}"
Tip="${Yellow_font_prefix}[注意]${Font_color_suffix}"
#mysql安装
function mysql_install(){
mysql_url=""
basedir="/usr/local/mysql"
datadir="/usr/local/mysql/data"
mysql_pwd="www.xyzbeta.com"
#用户判断
if [ -z $(grep mysql /etc/group) ]; then
groupadd mysql && echo -e "${Info}创建mysql用户组."
fi
if [ -z $(grep mysql /etc/passwd) ]; then
useradd mysql -g mysql && echo -e "${Info}创建mysql用户."
fi
#mysql安装
#wget ${mysql_url}
tar -zxvf /root/mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz && mv mysql-5.7.27-linux-glibc2.12-x86_64 ${basedir} && echo -e "${Info}解压并放置mysql到${basedir}"
chown -R mysql:mysql ${basedir} && chmod -R 755 ${basedir} && echo -e "${Info}修改mysql文件用户组为mysql"
cd ${basedir} && bin/mysqld --initialize --user=mysql --basedir=${basedir} --datadir=${datadir} 2> /tmp/initialize.log
cat /tmp/initialize.log
if [[ "" != $(grep -E "\[Error\]" /tmp/initialize.log) ]]; then
echo -e "${Error}mysql初始化失败,退出安装!" && exit 1
fi
default_pwd=$(grep -o "localhost:.*" /tmp/initialize.log | sed 's/localhost:\ //') && echo -e "${Info}首次登入密码为:${Green_font_prefix}${default_pwd}${Font_color_suffix}"
#bin/mysql_ssl_rsa_setup --datadir=${datadir}
mv /etc/my.cnf /etc/my.cnf_bak
cp ${basedir}/support-files/mysql.server /etc/init.d/mysqld
sed -i "s#^basedir=#basedir=${basedir}#g" /etc/init.d/mysqld
sed -i "s#^datadir=#datadir=${datadir}#g" /etc/init.d/mysqld
chkconfig --add mysqld && echo -e "${Info}加入mysql开机启动"
ln -s ${basedir}/bin/mysql /usr/bin
#mysql启动与账户初始化
${basedir}/bin/mysqld_safe --user=mysql &
if [[ "" == $(ps -ef | grep mysqld) ]]; then
echo -e "${Error}mysql启动失败..." && exit 1
else
echo -e "${Info}mysql启动成功"
fi
echo -e "${Info}mysql账户初始化"
sleep 5s #执行时存在mysql还未完全起来,导致不存在mysql.sock文件
MYSQL_PWD=${default_pwd} mysql -uroot --connect-expired-password << EOF
set password=password("${mysql_pwd}");
use mysql;
update mysql.user set authentication_string=password("${mysql_pwd}") where user='root';
create user 'root'@'%' identified by "${mysql_pwd}";
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
EOF
if [[ "0" == "$?" ]]; then
service mysqld restart && echo -e "${Info}数据库账户初始化成功.....";
echo && echo "————————————————————————————————————————————"
echo -n "| " && echo -e "${Info}安装路径:${basedir}"
echo -n "| " && echo -e "${Info}数据持久化路径:${datadir}"
echo -n "| " && echo -e "${Info}登入密码:${mysql_pwd}"
echo "————————————————————————————————————————————"
else
echo -e "${Tips}mysql账户初始化失败,请参照首次登入密码,手动重设登入密码。"
fi
}
mysql_install
本文系作者 @Mr.Lee 原创发布在 维简网。未经许可,禁止转载。
