MySQL是目前最流行的关系型数据库管理系统之一,属于 Oracle 旗下产品,由于它是开源软件,因此很多企业在 WEB 应用搭建时,常常选择MySQL做为数据存储的软件。
做为运维交付人员,在应用的搭建、迁移时往往需要手动安装部署MySQL软件,对于有一定Linux和MySQL基础的人来说,安装部署仅仅需要瞧瞧指令,但也得花费大量的时间,而对于初级运维人员,甚至是新手小白来说,安装部署MySQL可能会走很多弯路,还可能会遇到中途频频报错。
鉴于此,本人通过编写shell脚本,实现MySQL一键式安装部署,并且支持多版本,多类型,全过程只需3分钟左右,可以帮助运维交付人员实现MySQL的快速部署,提高工作效率。
Tips:目录下允许存放多种类型的MySQL安装包,但是版本只能有一个。
chmod +x mysql_install.sh
./mysql_install.sh
复制脚本内容,粘贴进mysql_install.sh文件。
#!/bin/bash
# -------------------------------------------------------------------------------
# ScriptName: mysql_install.sh
# Author: wenhui.Cheng
# Description: 一键部署MySQL
# Version: v1.3
# Date: 2022-07-24
# -------------------------------------------------------------------------------
echo "
┌──────────────────────────────────────────────────────────────────────┐
│ │
│ ∙ Linux环境下一键部署MySQL v1.3 ∙ │
│ (One-click Mysql installation based on Linux environment) │
│ │
│ ∙ Description : 基于Linux环境,实现MySQL一键式部署 │
│ ∙ Author : wenhui.Cheng │
│ ∙ E-Mail : [email protected] │
│ ∙ Version : v1.3 │
│ │
└──────────────────────────────────────────────────────────────────────┘
"
echo -e "\033[31m
注意事项:
1.目前仅支持rhel或者centos7.0以上版本部署,MySQL安装类型可以是rpm包或者tar包编译安装.
2.此脚本与MySQL的安装包(名称必须是:*rpm-bundle.tar或者*.rpm或者*.tar.gz或者*.tar文件)放在同一个目录下,比如/opt目录下.
3.目录下允许存放多种类型的MySQL安装包,版本只能有一个.
4.赋予脚本可执行权限,运行脚本即可开始部署MySQL.
\033[0m"
curdir=$(cd `dirname $0`; pwd)
# 是否开始
function isStart(){
read -p "是否开始一键部署MySQL?(Y/N):" choose_isStart
if [ "$choose_isStart" == 'Y' ] || [ "$choose_isStart" == 'y' ] ;then
return 0 # 退出函数
else
exit 0 # 退出程序
fi
}
# 检查用户
function use_check(){
curuser=$USER
if [ $curuser != 'root' ];then
echo "当前用户非root,请切换至root后再操作!"
exit 0
else
return 0
fi
}
# ******************************** rpmType ********************************
# 检查mariadb
function mariadb_check(){
mariadb_rpm_list=`rpm -qa |grep mariadb`
if [ -n "$mariadb_rpm_list" ];then
echo "mariadb检查结果:系统已安装mariadb,需先卸载"
read -p "是否卸载mariadb?(Y/N):" choose_Unmariadb
if [ "$choose_Unmariadb" == 'Y' ] || [ "$choose_Unmariadb" == 'y' ] ;then
echo "开始卸载mariadb..."
rpm -qa |grep mariadb |xargs rpm -e --nodeps
sleep 2
echo "mariadb卸载完成!"
else
exit 0
fi
else
echo "mariadb检查结果:系统未安装mariadb."
return 0
fi
}
# 检查MySQL
function mysql_check(){
mysql_rpm_list=`rpm -qa |grep mysql`
if [ -n "$mysql_rpm_list" ]; then
echo "mysql检查结果:系统已安装mysql,需先卸载,已安装的MySQL如下:"
rpm -qa | grep mysql | tee
read -p "是否卸载已安装的MySQL?(Y/N):" choose_UnMySQL
if [ "$choose_UnMySQL" == 'Y' ] || [ "$choose_UnMySQL" == 'y' ];then
echo "开始卸载MySQL..."
rpm -qa |grep mysql | xargs rpm -e --nodeps > /dev/null 2>&1
sleep 2
echo "MySQL卸载完成!"
else
exit 0
fi
else
echo "mysql检查结果:系统未安装MySQL."
fi
}
# 检查可能的MySQL残留文件
function mysqlFile_check(){
mysqlFile=`find / -name mysql`
if [ -n "$mysqlFile" ]; then
echo "检测到服务器可能存在MySQL残留文件,文件列表如下:"
find / -name mysql |xargs du --max-depth=0 -h # 打印可能的MySQL残留文件及大小
read -p "您要对这些文件怎么处理?(0:正常文件,不处理;1:我要手动删除;del:全部删除):" choose_delmysqlFile
if [ "$choose_delmysqlFile" == 0 ] ;then
return 0
elif [ "$choose_delmysqlFile" == 1 ] ;then
exit 0
elif [ "$choose_delmysqlFile" == 'del' ] ;then
echo "开始清理MySQL残留文件..."
#清理冗余文件
find / -name mysql |xargs rm -rf
sleep 3
rm -f /var/log/mysqld.log
echo "MySQL残留文件清理成功!"
else
echo "输入错误,请选择0、1、del."
exit 0
fi
else
rm -f /var/log/mysqld.log
echo "mysql残留文件检查结果:不存在MySQL残留文件"
fi
}
# 安装MySQL
function mysql_install_rpmType(){
echo "关闭SELinux..."
setenforce 0 > /dev/null
echo "关闭防火墙..."
systemctl stop firewalld.service
echo "开始安装MySQL..."
rpm -ivh mysql-community-*.rpm --nodeps --force
sleep 5
echo "MySQL安装成功,开始启动MySQL..."
systemctl start mysqld.service
if [ $? -eq 0 ] ;then
echo "MySQL启动成功!"
else
echo "MySQL启动失败,请检查日志/var/log/mysqld.log"
exit 0
fi
}
# 修改MySQL密码
function change_passwd_rpmType(){
initpasswd=`cat /var/log/mysqld.log |grep root@localhost |awk '{print $NF}'` # 初始密码
echo "MySQL的初始密码是:$initpasswd"
read -p "请输入您要设置MySQL的root用户密码:" passwd
export MYSQL_PWD=$initpasswd #解决mysql>5.6会提示密码安全问题
mysqladmin -uroot password $passwd > /dev/null
echo $?
echo "MySQL密码修改成功!"
}
# 修改MySQL权限
function change_access_rpmType(){
export MYSQL_PWD=$passwd
mysql -uroot <;
update user set Host="%" where User="root";
FLUSH PRIVILEGES;
quit
EOF
echo "MySQL权限修改成功!"
}
# MySQL安装包类型检查(rpm或者rpm-bundle.tar) + 调取mysql_install
function mysql_PackageName_chech_rpmType(){
PackageName_tar=`ls -l | grep rpm-bundle.tar | awk '{print $9}'`
PackageName_rpm=`ls -l | grep mysql-community | awk '{print $9}'`
if [ -n "$PackageName_tar" ];then
echo "解压安装包$PackageName_tar ..."
tar xf $PackageName_tar
echo "解压成功!"
mysql_install_rpmType
return 0
elif [ -n "$PackageName_rpm" ];then
mysql_install_rpmType
return 0
else
echo -e "\033[31m未找到安装包,请检查${curdir}路径下是否存在*rpm-bundle.tar或者*.rpm文件 \033[0m"
exit 0
fi
}
# 迁移目录
function Migration_dir(){
read -p "是否需要迁移MySQL数据目录?(Y/N):" choose_isMigration
if [ "$choose_isMigration" == 'Y' ] || [ "$choose_isMigration" == 'y' ] ;then
read -p "请输入您要迁移至的路径(如:/opt/data):" newPath
echo "开始准备迁移目录..."
systemctl stop mysqld.service
mkdir -p $newPath # 新建目录
cp -r /var/lib/mysql $newPath
chmod -R 755 $newPath
chown -R mysql:mysql $newPath
# 修改my.cnf
# 将迁移的路径进行转义处理,方便使用sed进行替换修改
newPathe=`echo $newPath |sed 's#\/#\\\/#g'`
sed -i "s/\/var\/lib/$newPathe/g" /etc/my.cnf
# 插入配置
sed -i "1i\socket=$newPath/mysql/mysql.sock" /etc/my.cnf
sed -i "1i\[client]" /etc/my.cnf
systemctl start mysqld.service
echo "MySQL目录迁移成功!"
else
return 0
fi
}
# 检查安装结果
function mysql_successful_rpmType(){
netstat -ntl | grep 3306
if [ $? -eq 0 ] ;then
echo "MySQL安装成功,现在可以开始正式使用啦!"
else
echo "MySQL安装或启动失败,请查看日志:/var/log/mysqld.log"
fi
}
# ******************************** tarType ********************************
# MySQL端口设置
function mysql_port_check(){
while true
do
read -p "请输入您要设置的MySQL端口(如:3306):" mysql_port
netstat -ntlp |grep :$mysql_port
if [ $? -eq 0 ] ; then
echo "端口${mysql_port}已被占用,请更换端口!"
else
return 0
fi
done
}
# 检查mysql用户
function mysql_user_check(){
if [ `grep "mysql" /etc/passwd | wc -l` -eq 0 ] ;then
echo "创建MySQL用户和组"
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
else
echo "MySQL用户已经存在!"
return 0
fi
}
# 安装MySQL
function mysql_install_tarType(){
read -p "您要将MySQL的程序和数据文件存放在哪个路径下?(如:/opt/data):" mysql_path
mkdir -p $mysql_path
mv $unzip_dir mysql_tar
mv mysql_tar $mysql_path
# 创建目录
mkdir -p $mysql_path/mysql_tar/data # 存放数据
mkdir -p $mysql_path/mysql_tar/logs # 存放日志
echo "正在创建my.cnf文件..."
cat > $mysql_path/mysql_tar/my.cnf << EOF
[client]
socket=$mysql_path/mysql_tar/data/mysql.sock
[mysql]
default-character-set=utf8
socket=$mysql_path/mysql_tar/data/mysql.sock
[mysqld]
port=$mysql_port
socket=$mysql_path/mysql_tar/data/mysql.sock
# 多实例需要配置mysqlx
mysqlx_port=3${mysql_port}
mysqlx_socket=$mysql_path/mysql_tar/data/mysqlx.sock
basedir=$mysql_path/mysql_tar
datadir=$mysql_path/mysql_tar/data
max_connections=10000
max_connect_errors=10
character-set-server=UTF8MB4
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
log-error=$mysql_path/mysql_tar/logs/mysqld.log
EOF
sleep 3
cat $mysql_path/mysql_tar/my.cnf
echo "开始初始化MySQL(设置表名不区分大小写)..."
$mysql_path/mysql_tar/bin/mysqld --defaults-file=$mysql_path/mysql_tar/my.cnf --initialize --basedir=$mysql_path/mysql_tar --datadir=$mysql_path/mysql_tar/data --lower_case_table_names=1
sleep 5
cat $mysql_path/mysql_tar/logs/mysqld.log
# 赋权限
chown mysql:mysql -R $mysql_path/mysql_tar
chmod 755 -R $mysql_path/mysql_tar/data
chmod 644 $mysql_path/mysql_tar/my.cnf
# 启动MySQL数据库实例
echo "开始启动MySQL..."
nohup $mysql_path/mysql_tar/bin/mysqld --defaults-file=$mysql_path/mysql_tar/my.cnf --user=mysql & > /dev/null 2>&1
sleep 5
echo "MySQL启动成功!"
}
# 检查MySQL安装包类型是tar还是tar.gz
function mysql_PackageName_chech_tarType(){
# 先判断安装包是否为tar.gz
ls -l mysql-[0-9]*tar.gz > /dev/null 2>&1 # 表示stderr标准错误的输出重定向等同于标准输出1
if [ $? -eq 0 ] ;then
mysqlfile_targz=$(ls -l mysql-[0-9]*tar.gz | awk '{print $9}')
echo "开始解压安装包$mysqlfile_targz ..."
tar xf $mysqlfile_targz
# 获取解压后的目录名称
unzip_dir=`ls -l mysql-[0-9]*tar.gz|awk '{print $9}'|cut -d. -f-3`
# 安装MySQL
mysql_install_tarType
elif [ $? -ne 0 ] ;then
ls -l mysql-*.tar > /dev/null 2>&1
if [ $? -eq 0 ] ;then
mysqlfile_tar=$(ls -l mysql-*.tar | awk '{print $9}')
echo "开始解压安装包$mysqlfile_tar ..."
tar xf $mysqlfile_tar
# 获取解压后的tar.gz文件名称,使用正则匹配文件名
mysqlfile_targz=$(ls -l mysql-[0-9]*tar.gz |awk '{print $9}')
echo "开始解压安装包$mysqlfile_targz ..."
tar xf $mysqlfile_targz
# 获取解压后的目录名称
unzip_dir=$(ls -l mysql-[0-9]*tar.gz |awk '{print $9}'|cut -d. -f-3)
# 安装MySQL
mysql_install_tarType
else
echo -e "\033[31m未找到安装包,请检查${curdir}路径下是否存在*.tar.gz或者*.tar文件 \033[0m"
exit 0
fi
else
exit 0
fi
}
# 修改MySQL密码
function change_passwd_tarType(){
initpasswd=`cat $mysql_path/mysql_tar/logs/mysqld.log |grep root@localhost |awk '{print $NF}'` # 初始密码
echo "初始密码是:"$initpasswd
read -p "请输入您要设置MySQL的root用户密码:" passwd
export MYSQL_PWD=$initpasswd
# MySQL多实例登录时需要指定sock路径(-S选项)
$mysql_path/mysql_tar/bin/mysqladmin -uroot -S $mysql_path/mysql_tar/data/mysql.sock password $passwd > /dev/null 2>&1
echo "MySQL密码修改成功!"
}
# 修改MySQL访问权限
function change_access_tarType(){
echo "开始修改MySQL访问权限..."
export MYSQL_PWD=$passwd
$mysql_path/mysql_tar/bin/mysql -uroot -h127.0.0.1 -P$mysql_port -S $mysql_path/mysql_tar/data/mysql.sock <;
update user set Host="%" where User="root";
FLUSH PRIVILEGES;
quit
EOF
echo "MySQL权限修改成功!"
}
# 检查安装结果
function mysql_successful_tarType(){
netstat -ntl | grep $mysql_port
if [ $? -eq 0 ];then
echo "MySQL安装成功,现在可以开始正式使用啦!"
else
echo "MySQL安装或启动失败,请查看日志:$mysql_path/mysql_tar/logs/mysqld.log"
fi
}
# 停止MySQL
function mysqld_stop(){
ps -ef | grep $mysql_path/mysql_tar | grep -v grep |awk '{print $2}' |xargs kill -9 > /dev/null 2>&1
sleep 2
}
# 启动MySQL
function mysqld_start(){
echo "启动MySQL"
nohup $mysql_path/mysql_tar/bin/mysqld --defaults-file=$mysql_path/mysql_tar/my.cnf --user=mysql & > /dev/null 2>&1
sleep 5
}
# 重启MySQL
function mysqld_restart(){
mysqld_stop
mysqld_start
sleep 5
}
# ******************************** 函数调用 ********************************
# rpm类型安装
function rpmType(){
isStart # 是否开始
use_check # 检查用户
mariadb_check # 检查mariadb
mysql_check # 检查MySQL
mysqlFile_check # 检查残留文件
mysql_PackageName_chech_rpmType # 检查安装包名称 + 安装MySQL
change_passwd_rpmType # 修改密码
change_access_rpmType # 修改权限
Migration_dir # 迁移目录
mysql_successful_rpmType # 安装结果检查
}
# tar类型安装
function tarType(){
isStart # 是否开始
use_check # 检查用户
mysql_port_check # 设置端口
mysql_user_check # 检查mysql用户
mysql_PackageName_chech_tarType # 检查安装包名称 + 安装MySQL
change_passwd_tarType # 修改MySQL密码
change_access_tarType # 修改MySQL访问权限
mysqld_restart # 重启MySQL
mysql_successful_tarType # 成功
}
echo "
MySQL安装类型:
(1)rpm类型:rpm包自动安装
(2)tar类型:tar包编译安装
"
read -p "请选择你要安装MySQL的类型(输入1或者2):" chooseType
if [ "$chooseType" == 1 ] ;then
rpmType
elif [ "$chooseType" == 2 ] ;then
tarType
else
echo "输入编号错误!"
fi
以上就是通过shell脚本一键部署MySQL的全部内容,欢迎各位读者给予建议,后续本人还会在该版本的基础上进一步优化,增加新的方法,尽可能的覆盖更多的使用场景。
如果对你有帮助,还请记得收藏点赞加关注,谢谢!