使用Shell脚本安装Mysql
以下脚本程序将自动安装Mysql,内容见代码。可用于安装大量Mysql的场景下。
#!/bin/bash
# author: li
# data: 2020-05-24
# descript: install mysql
set -u
cd
mysql_file=/root/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
[ ! -e $mysql_file ] && wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
if [ ! -e $mysql_file ];then
echo "not found $mysql_file!"
exit
fi
basedir=/data/mysql
if [ ! -d $basedir ];then
mkdir -p $basedir
fi
tar -zxf $mysql_file -C $basedir
cd $basedir
mv mysql-5.6.46-linux-glibc2.12-x86_64 mysql
grep mysql /etc/passwd
if [ $? -eq 1 ];then
useradd -M -s /sbin/nologin mysql
fi
datadir=$basedir/data
mkdir $datadir
chown -R mysql:mysql $basedir $datadir
cd $basedir/mysql
./scripts/mysql_install_db --user=mysql --basedir=$basedir/mysql --datadir=$datadir
mysqld=/etc/rc.d/init.d/mysqld
cp ./support-files/mysql.server $mysqld
if [ -e $mysqld ];then
sed -i "s#basedir=#basedir=$basedir/mysql#" $mysqld
sed -i "s#datadir=#datadir=$datadir/mysql#" $mysqld
fi
# cd /data/mysql/mysql
# cp ./support-files/my-default.cnf /etc/my.cnf
# 请手动配置与优化你的/etc/my.cnf文件,并注意其basedir与datadir路径与本脚本一致
# 启动: service mysqld start
echo "Done"