在文章上一篇文章中https://blog.51cto.com/8355320/2463218
我使用的是手动源码安装mysql5.7.28,安装过程还是比较耗时,我编写了shell自动安装脚本,请大家参考,脚本我测试没有出现问题,脚本我有二次修改里面的实例名与变量名,可能存在不一致,请自行对比确认下。脚本中有什么可以优化的还请多多指点,谢谢!!以下是脚本中的几点说明:
1、mysql5.7.28软件包(脚本中配置wget下载,如果下载比较慢,可以事先下载好,上传到 /opt 路径下,并注释对应行的wget下载代码,另外源码安装过程比较耗时,安装请耐心等待),软件包名称如下:
#boost_1_59_0.tar.gz
#mysql-boost-5.7.28.tar.gz
2、两个mysql数据库脚本上传路径: /opt 包含如下文件(注意sql脚本中的实例名),sql测试脚本名称如下:
#testone.sql
#testtwo.sql
实例dbtwo有添加lower_case_table_names = 1参数
实例dbone没有添加
4、根据服务器磁盘分配情况,创建对应软链接,脚本我配置的路径是/data,根据需要修改,例如:
#mkdir /home/data
#ln -s /home/data/ /data
5、修改(确保局域网内值唯一)
#mysql3306.cnf 的 server_id = 3306
#mysql3307.cnf 的 server_id = 3307
6、注意修改预设的root密码与apps应用连接密码,根据需要修改使用
#dbone_rootpwd="Dbone.3306"
#dbtwo_rootpwd="Dbtwo.3307"
#dbone_appspwd="dbone3306"
#dbtwo_appspwd="dbtwo3307"
7、修改dbone数据库实例 dbone 用户名 dbone 及对应的密码 dbone3306
8、修改dbone导入数据库时的数据库实例名 dbone
9、修改dbtwo数据库实例 dbtwo 用户名 dbtwo 及对应的密码 dbtwo3307
10、修改dbtwo导入数据库时的数据库实例名 dbtwo
#!/bin/bash
################################################################################################
# Install software -- Install Mysql 5.7.28 on CentOS 7
#
# History: 2020/01/09 Asa release
################################################################################################
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions
###Check if user is root
if [ $UID -ne 0 ]; then
echo "Error: This script must be executed as root."
exit 1
fi
echo "################################################################################################"
echo " 源码自动安装 MySQL 5.7.28 on CentOS7 Linux "
echo "################################################################################################"
#set mysql root password
echo "######################### 预设 Mysql root用户密码 ###############################"
dbone_rootpwd="dbone.3306"
dbtwo_rootpwd="dbtwo.3307"
#set mysql apps password
echo "######################### 预设 Mysql apps用户密码 ###############################"
dbone_appspwd="Dbone3306"
dbtwo_appspwd="Dbtwo3307"
echo "################################ 定义目录路径 #######################################"
##define mysql directory configuration variable
dboneDatadir=/data/mysql/dbone3306/data
dboneBinlogdir=/data/mysql/dbone3306/binlog
dboneLogdir=/data/mysql/dbone3306/logs
dboneSockfile=/data/mysql/dbone3306/mysql3306.sock
dbonepidfile=/data/mysql/dbone3306/mysqld3306.pid
dboneDefaultfile=/data/mysql/dbone3306/my3306.cnf
dbtwoDatadir=/data/mysql/dbtwo3307/data
dbtwoBinlogdir=/data/mysql/dbtwo3307/binlog
dbtwoLogdir=/data/mysql/dbtwo3307/logs
dbtwoSockfile=/data/mysql/dbtwo3307/dbtwo3307.sock
dbtwopidfile=/data/mysql/dbtwo3307/mysqld3307.pid
dbtwoDefaultfile=/data/mysql/dbtwo3307/my3307.cnf
BaseDir=/usr/local/mysql
MYSQL_DATADIR=/usr/local/mysql/data
UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock
BOOSTDIR=/usr/local/boost
Dbdir=/data
Softwaredir=/opt
mkdir -pv /data/mysql/{dbone3306,dbtwo3307}
mkdir -v /data/mysql/dbone3306/{logs,data,binlog}
mkdir -v /data/mysql/dbtwo3307/{logs,data,binlog}
echo "################################################################################################"
echo " 修改系统参数 "
echo "################################################################################################"
###set the ip in hosts
hostsset() {
echo "############################ Ip&Hosts Configuration #######################################"
hostname=`hostname`
ip=`ip a|grep 'inet '|grep -v '127.0.0.1'|awk '{print $2}'|awk -F '/' '{print $1}'`
for i in ${ip}
do
a=`grep "${i}" /etc/hosts`
if [ ! -n "${a}" ];then
echo "${i} ${hostname}" >> /etc/hosts
else
break
fi
done
}
ntp() {
yum -y install ntp
systemctl enable ntpd
echo 'server ntp1.aliyun.com' >> /etc/ntp.conf
echo 'server ntp2.aliyun.com' >> /etc/ntp.conf
systemctl start ntpd
if [ $? != 0 ]; then
errorExit 'ntp 启动未成功'
exit 2
fi
return 0
}
syspro() {
sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config
setenforce 0
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'LANG="en_US.UTF-8"' >> /etc/profile && source /etc/profile
cat >>/etc/security/limits.conf<> /etc/sysctl.conf<> /etc/profile<> /etc/profile
export PATH=\$PATH:${BaseDir}/bin
EOF
source /etc/profile
chown -R mysql:mysql $BaseDir
}
#edit my.cnf
setmycnf() {
cat >>$dboneDefaultfile<>$dbtwoDefaultfile<&1 > /dev/null &
sleep 10
netstat -tnlp|grep 3306
if [ $? -eq 0 ];then
echo -e " \033[32m dbone start success!! \033[0m"
else
echo -e "\e[31;47;5m dbone start failed!! \e[0m"
exit 8
fi
echo "################################ dbone root用户临时密码 #######################################"
dboneTempPass=$(less $dboneLogdir/error.log|grep 'A temporary password is generated for root@localhost:' |awk '{print $NF}')
echo $dboneTempPass
echo "################################ 自动修改 dbone root用户初始密码 ###############################"
$BaseDir/bin/mysqladmin -uroot -p"$dboneTempPass" -S $dboneSockfile password $dbone_rootpwd
if [ $? -eq 0 ];then
echo -e " \033[32m dbone rootpwd changed success!! \033[0m"
else
echo -e "\e[31;47;5m dbone rootpwd changed failed!! \e[0m"
exit 9
fi
echo "################################ dbone 服务关闭 #######################################"
$BaseDir/bin/mysqladmin -uroot -p$dbone_rootpwd -S $dboneSockfile shutdown
netstat -tnlp|grep 3306
if [ $? -ne 0 ];then
echo -e " \033[32m dbone stop success!! \033[0m"
else
echo -e "\e[31;47;5m dbone stop failed!! \e[0m"
exit 10
fi
}
#dbtwo initialize
dbtwoinitialize() {
echo "################################ dbtwo 初始化 #######################################"
$BaseDir/bin/mysqld --defaults-file=$dbtwoDefaultfile --initialize --user=mysql --basedir=$BaseDir --datadir=$dbtwoDatadir
sleep 120
less $dbtwoLogdir/error.log|grep 'A temporary password is generated for root@localhost:'
B=$?
count=0
while [ $count -lt 10 ]
do
if [ $B -eq 0 ];then
echo -e " \033[32m mysql initialize success!! \033[0m"
break
else
echo -e "\e[31;47;5m the result is null,check again!! \e[0m"
count=$[${count}+1]
fi
done
#create ssl_rsa
echo "################################ dbtwo 生成ssl授权 #######################################"
$BaseDir/bin/mysql_ssl_rsa_setup --user=mysql --basedir=$BaseDir --datadir=$dbtwoDatadir
if [ $? -eq 0 ];then
echo -e " \033[32m create ssl_rsa success!! \033[0m"
else
echo -e "\e[31;47;5m create ssl_rsa failed!! \e[0m"
exit 11
fi
echo "################################ dbtwo 服务启动 #######################################"
$BaseDir/bin/mysqld_safe --defaults-file=$dbtwoDefaultfile --user=mysql 2>&1 > /dev/null &
sleep 10
netstat -tnlp|grep 3307
if [ $? -eq 0 ];then
echo -e " \033[32m dbtwo start success!! \033[0m"
else
echo -e "\e[31;47;5m dbtwo start failed!! \e[0m"
exit 12
fi
echo "################################ dbtwo root用户临时密码 #######################################"
dbtwoTempPass=$(less $dbtwoLogdir/error.log|grep 'A temporary password is generated for root@localhost:' |awk '{print $NF}')
echo $dbtwoTempPass
echo "################################ 自动修改 dbtwo root用户初始密码 ###############################"
$BaseDir/bin/mysqladmin -uroot -p"$dbtwoTempPass" -S $dbtwoSockfile password $dbtwo_rootpwd
if [ $? -eq 0 ];then
echo -e " \033[32m dbtwo rootpwd changed success!! \033[0m"
else
echo -e "\e[31;47;5m dbtwo rootpwd changed failed!! \e[0m"
exit 13
fi
echo "################################ dbtwo 服务关闭 #######################################"
$BaseDir/bin/mysqladmin -uroot -p$dbtwo_rootpwd -S $dbtwoSockfile shutdown
netstat -tnlp|grep 3307
if [ $? -ne 0 ];then
echo -e " \033[32m dbtwo stop success!! \033[0m"
else
echo -e "\e[31;47;5m dbtwo stop failed!! \e[0m"
exit 14
fi
}
#set dbonestart script
dbonestart() {
SOCK="${dboneSockfile}"
CNF="${dboneDefaultfile}"
MYSQL_USER="root"
MYSQL_PWD="${dbone_rootpwd}"
CmdPath="${BaseDir}/bin"
cat > /etc/init.d/dbone<&1 > /dev/null &
}
#stop function
function_stop_mysql()
{
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${MYSQL_USER} -p${MYSQL_PWD} -S ${SOCK} shutdown
}
#restart function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case \$1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: dbone {start|stop|restart}\n"
esac
EOF
chmod +x /etc/init.d/dbone
chkconfig --add dbone
chkconfig --list dbone
service dbone start
netstat -tnlp|grep 3306
if [ $? -ne 0 ];then
echo -e " \033[32m dbone start success!! \033[0m"
else
echo -e "\e[31;47;5m dbone start failed!! \e[0m"
exit 15
fi
}
#set dbtwostart script
dbtwostart() {
SOCK="${dbtwoSockfile}"
CNF="${dbtwoDefaultfile}"
MYSQL_USER="root"
MYSQL_PWD="${dbtwo_rootpwd}"
CmdPath="${BaseDir}/bin"
cat > /etc/init.d/dbtwo<&1 > /dev/null &
}
#stop function
function_stop_mysql()
{
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${MYSQL_USER} -p${MYSQL_PWD} -S ${SOCK} shutdown
}
#restart function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case \$1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: dbtwo {start|stop|restart}\n"
esac
EOF
chmod +x /etc/init.d/dbtwo
chkconfig --add dbtwo
chkconfig --list dbtwo
service dbtwo start
netstat -tnlp|grep 3307
if [ $? -ne 0 ];then
echo -e " \033[32m dbtwo start success!! \033[0m"
else
echo -e "\e[31;47;5m dbtwo start failed!! \e[0m"
exit 16
fi
}
dboneimp() {
echo "############################### 再次重启 dbone 数据库确认系统服务正常 ##################################"
service dbone stop
service dbone start
sleep 10
netstat -tnlp|grep 3306
if [ $? -ne 0 ];then
echo -e " \033[32m dbone start success!! \033[0m"
else
echo -e "\e[31;47;5m dbone start failed!! \e[0m"
exit 17
fi
echo "############################### 创建 dbone 库 ##################################"
MYSQL_CMD="$BaseDir/bin/mysql -uroot -p"${dbone_rootpwd}" -S ${dboneSockfile}"
$MYSQL_CMD -e "create database dbone default character set utf8 collate utf8_bin;grant select,insert,update,delete,create,execute on dbone.* to 'dbone'@'%' identified by 'dbone3306';flush privileges;"
$MYSQL_CMD -e "show databases;" | grep dbone
if [ $? -ne 0 ]
then
echo -e "\e[31;47;5m 创建 dbone 数据库失败!\e[0m"
exit 18
fi
echo "############################### dbone 导入数据 ##################################"
cd $Softwaredir
$BaseDir/bin/mysql -uroot -p"${dbone_rootpwd}" -S ${dboneSockfile} --comments dbone < testone.sql
if [ $? -ne 0 ]
then
echo -e "\e[31;47;5m 导入数据失败!\e[0m"
exit 19
fi
}
dbtwoimp() {
echo "############################### 再次重启 dbtwo 数据库确认系统服务正常 ##################################"
service dbtwo stop
service dbtwo start
sleep 10
netstat -tnlp|grep 3307
if [ $? -ne 0 ];then
echo -e " \033[32m dbtwo start success!! \033[0m"
else
echo -e "\e[31;47;5m dbtwo start failed!! \e[0m"
exit 20
fi
echo "############################### 创建 dbtwo 库 ##################################"
MYSQL_CMD="$BaseDir/bin/mysql -uroot -p"${dbtwo_rootpwd}" -S ${dbtwoSockfile}"
$MYSQL_CMD -e "create database dbtwo default character set utf8 collate utf8_bin;grant select,insert,update,delete,create,execute on dbtwo.* to 'dbtwo'@'%' identified by 'dbtwo3307';flush privileges;"
$MYSQL_CMD -e "show databases;" | grep dbtwo
if [ $? -ne 0 ]
then
echo -e "\e[31;47;5m 创建 dbtwo 数据库失败!\e[0m"
exit 21
fi
echo "############################### dbtwo 导入数据 ##################################"
cd $Softwaredir
$BaseDir/bin/mysql -uroot -p"${dbtwo_rootpwd}" -S ${dbtwoSockfile} --comments dbtwo < testtwo.sql
if [ $? -ne 0 ]
then
echo -e "\e[31;47;5m 导入数据失败!\e[0m"
exit 22
fi
}
main() {
hostsset
ntp
syspro
fwport
clean
addusers
dbinstall
setmycnf
dboneinitialize
dbtwoinitialize
dbonestart
dbtwostart
dboneimp
dbtwoimp
}
main
echo "####################### 安装完成 (请记录dbone数据库dbone信息) ##############################"
echo "root密码:"
echo $dbone_rootpwd
echo "数据库实例连接密码:"
echo $dbone_appspwd
echo "####################### 安装完成 (请记录dbtwo数据库dbtwo信息) ##############################"
echo "root密码:"
echo $dbtwo_rootpwd
echo "数据库实例连接密码:"
echo $dbtwo_appspwd