mysql自动安装脚本
cat mysql_auto_install.shmysql
#!/bin/bash
#Part1:整个流程分3步
#1.mysql官网下载二进制版本的mysql5.7.21安装包(mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz)和mysql_auto_install.sh脚本放至/root目录
#2.执行下文所述的mysql_auto_install.sh脚本
#3.输入您设置的密码登陆数据库
###### 二进制自动安装数据库脚本root默认密码:MANAGER, 将脚本和安装包放在/root目录便可.###############
######数据库目录/usr/local/mysql############
######数据目录/data/mysql############
######慢日志目录/data/slowlog############
######端口号默认3306其他参数按需自行修改############
######author:anzhen at 2018/02/27
function check_install_mysql_environment()
{
echo "################检查本机安装mysql的基本条件########################"
echo "Checking user :"
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this script, please use root to install"
exit 1
else
echo "user is root, this is ok!"
fi
echo "checking os version"
if [ `uname -s`="linux" ]; then
echo "os is linux,this is ok!"
else
echo "os isnot linux,this is fail!"
exit 1
fi
if [ -d /data/mysql ]; then
echo "mysql datadir /data/mysql is exist! ,this is fail!"
exit 1
else
echo "mysql datadir /data/mysql is not exist,this is ok!"
fi
os_version=`uname -r|cut -d . -f 4`
if [ ${os_version}="el7" ] || [${os_version}="el6" ]; then
echo "os version is el6 or el7, this is ok!"
else
echo "os version isnot el6 or el7, this is fail!"
exit 1
fi
port=`netstat -ntl| awk '{ print $4}' |grep '3306'|awk -F: '{ print $4}'`
if [[ ${port} = "3306" ]]; then
echo "mysql port 3306 is exist, please uninstall existed mysql or modify script , this is fail!"
exit 1
else
echo "msyql port is not 3306! this is ok!"
fi
}
#Install MySQL
function InstallMySQL()
{
echo -e "\n"
echo "############################# MySQL installing..........########################"
#Set timezone
#rm -rf /etc/localtime
#ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#Delete Old Mysql program
#Disable SeLinux
if [ -s /etc/selinux/config ]; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
fi
setenforce 0
groupadd mysql -g 512
useradd -u 512 -g mysql -s /sbin/nologin -d /home/mysql mysql
#mysql directory configuration
if [ -d /root/mysql-5.7.21-linux-glibc2.12-x86_64 ]; then
rm -rf /root/mysql-5.7.21-linux-glibc2.12-x86_64
fi
echo -e "uncompressioning mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz file..........\nplease wait a few minutes..........."
tar -zxvf /root/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz > /dev/null
if [ -d /usr/local/mysql ]; then
mv /usr/local/mysql /usr/local/mysql_`date +%Y%m%d%H%M%S`
fi
mv /root/mysql-5.7.21-linux-glibc2.12-x86_64 /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
if [ -d /data/mysql ]; then
mv /data/mysql /data/mysql_`date +%Y%m%d%H%M%S`
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
echo "directory /data/mysql created succeed!"
else
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
echo "directory /data/mysql created succeed!"
fi
if [ -d /data/slowlog ]; then
mv /data/slowlog /data/slowlog_`date +%Y%m%d%H%M%S`
mkdir -p /data/slowlog
chown -R mysql:mysql /data/slowlog
echo "directory /data/slowlog created succeed!"
else
mkdir -p /data/slowlog
chown -R mysql:mysql /data/slowlog
echo