MySql一键安装shell脚本

2017-11-06 13:07:17来源:http://bosszhang.blog.51cto.com/2580498/1978995作者:人点击

分享

#!/bin/bash
#data:20171104
#QQ:654996537
#Name:Zhang
#判断是否是root用户
if[$(id-u)!="0"];then
echo"Nottherootuser!TryusingsudoCommand!"
exit1
fi
#安装依赖包
yum-ygroupinstall"DesktopPlatformDevelopment"
yuminstall-ygd-devellibjpeg-devellibpng-devellibxml2-develbzip2-devellibcurl-devellibmcryptlibmcrypt-develzlib-develnet-snmp-developensslcmakencurses-develgitgcclibevent-devel

#自定义源码包名称
Package_Type=".tar.gz"
Package_Version="mysql-5.6.31"
Install_Package=$Package_Version$Package_Type
Setup_path="/root/"
#自定义用户和用户组
Group_Name="mysql"
User_Name="mysql"
#自定义RPM包
RPM="$User_Name"
#自定义MySql数据库保存路径
#####Mydata="/mydata/{data,bin-log}}"
#mkdir/mydata
#mkdir/mydata/data
#mkdir/mydata/bin-log
mkdir/mydata
mkdir/mydata/{data,bin-log}
DataDir="/mydata/data"
#自定义MySql配置文件路径
MySql_Conf="/etc/mysql"
#自定义MySql安装路径
Install_prefix="/usr/local/mysql"
#判断Rpm包是否安装
functionRPM_Install(){
rpm-qa|egrep"$RPM">>/dev/null
if[$?-eq0]
then
echo"$RPMisinstallYes."
else
echo"$RPMisNotinstall."
fi
}
RPM_Install
#判断用户和用户组
Add_Group_User(){
egrep"^$Group_Name"/etc/group>&/dev/null
if[$?-ne0]
then
echo"mysql用户组正在添加."
groupadd$Group_Name
else
echo"The$Group_Nameusergroupalreadyexists."
echo"mysql用户组已经添加."
fi
#判断mysql用户是否存在
egrep"^$User_Name"/etc/passwd>&/dev/null
if[$?-ne0]
then
echo"mysql用户正在添加."
useradd-g$Group_Name$User_Name
else
echo"mysql用户已经添加."
echo"The$User_Nameuseralreadyexists."
fi
}
Add_Group_User
#判断源码包文件是否存在
if[-e$Package_Name]
then
echo"$Package_NameThePackageexists."
else
echo"$Package_NameThepackagedoesnotexist."
fi

#编译安装mysql
cd$Setup_path
tar-zxvf$Install_Package
cd$Package_Version
configure_opts=(
-DCMAKE_INTALL_PREFIX=$Install_prefix
-DSYSCONFDIR=$MySql_Conf
-DMYSQL_DATADIR=$DataDir
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAAGE_ENGINE=1
-DWITH_READLINE=1
-DWITH_OPENSSL=system
-DWITH_ZLIB=system
-DWITH_LIBWRAp=0
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
)
cmake.${configure_opts[@]}
#判断是否编译成功
if[[$?-eq0]]
then
make&&makeinstall
else
echo"编译失败,请重新编译"&&exit1
fi
#修改mysql组和数据目录
cd$Install_prefix
chown:mysql./*-R
chown-Rmysql:mysql$DataDir
chown-Rmysql:mysql/mydata/bin-log
#初始化数据库
$Install_prefix/scripts/mysql_install_db--user=mysql--datadir=$DataDir
#判断是否初始化成功,目录不为空说明成功,目录为空则失败
if["$(ls-A$DataDir)"];then
echo"$DataDirisnotEmpty"
else
echo"$DataDirisEmpty"
fi
#提供服务脚本
cp$Install_prefix/support-files/mysql.server/etc/rc.d/init.d/mysqld
chmod+x/etc/rc.d/init.d/mysqld
chkconfig--addmysqld
#提供配置文件
#mv/etc/my.cnf/etc/my.cnf.bak
#mv/etc/my.cnf.d/etc/my.cnf.d.bak
if[-e/etc/my.cnf]
then
echo"Fileexists."
mv/etc/my.cnf/etc/my.cnf.bak
else
echo"Filedoesnotexist."
fi

#if[-e/etc/my.cnf.d]
#then
#echo"Fileexists."
#mv/etc/my.cnf.d/etc/my.cnf.d.bak
#else
#echo"Filedoesnotexist."
#fi
mkdir$MySql_Conf
cp$Install_prefix/support-files/my-default.cnf/etc/mysql/my.cnf
sed-i'/datadir/a/datadir=/mydata/data'/etc/mysql/my.cnf
sed-i'/log_bin/a/log_bin=/mydata/bin-log/mysql-bin'/etc/mysql/my.cnf
sed-i'$a/innodb_file_per_table=1'/etc/mysql/my.cnf
#修改环境变量
cat>/etc/profile.d/mysql.sh<<EOF
exportPATH=/usr/local/mysql/bin/:$PATH
EOF
source/etc/profile.d/mysql.sh
source/etc/profile
man-M/usr/local/mysql/man/
#启动MySql服务
#Centos6启动方式
#servicemysqldstart
#Centos7启动方式
#systemctlstartmysqld.service
/etc/rc.d/init.d/mysqldstart
#判断MySql是否启动
MySQL_running(){
Startup_file="/etc/rc.d/init.d/mysqld"
#通过查看进程判断是否正常启动
proc=`ps-ef|grepmysql|grep-vgrep|wc-l`
#通过监听3306端口进行判断是否启动
port=`netstat-lntup|grep3306|wc-l`
if[$proc-eq2]&[$port-eq1]
then
echo"MySQLisrunning."
echo"`ss-tnlp|grepmysql`"
else
echo"MySQLisnorunning."
#$Startup_filestart

fi
}
MySQL_running

微信扫一扫

第七城市微信公众平台