基于Linux环境,实现MySQL一键式安装部署,宝子们有想法的可以评论区指出哦~
话不多说,上脚本
echo "
┌──────────────────────────────────────────────────────────────────────┐
│ │
│ ∙ Linux环境下一键部署MySQL v1.0 ∙ │
│ (One-click Mysql installation based on Linux environment) │
│ │
│ ∙ Description : 基于Linux环境,实现MySQL一键式部署 │
│ ∙ Author : laoliu │
│ ∙ Date : 2023-03-23 │
│ ∙ Version : v1.0 │
│ │
└──────────────────────────────────────────────────────────────────────┘
"
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
}
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
}
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
}
function mysqlFile_check(){
mysqlFile=`find / -name mysql`
if [ -n "$mysqlFile" ]; then
echo "检测到服务器可能存在MySQL残留文件,文件列表如下:"
find / -name mysql |xargs du --max-depth=0 -h
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
}
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
}
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
mysqladmin -uroot password $passwd > /dev/null
echo $?
echo "MySQL密码修改成功!"
}
function change_access_rpmType(){
export MYSQL_PWD=$passwd
mysql -uroot <<EOF
use mysql;
update user set Host="%" where User="root";
FLUSH PRIVILEGES;
quit
EOF
echo "MySQL权限修改成功!"
}
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
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
}
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
}
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
}
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_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
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启动成功!"
}
function mysql_PackageName_chech_tarType(){
ls -l mysql-[0-9]*tar.gz > /dev/null 2>&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_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
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_install_tarType
else
echo -e "\033[31m未找到安装包,请检查${curdir}路径下是否存在*.tar.gz或者*.tar文件 \033[0m"
exit 0
fi
else
exit 0
fi
}
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_path/mysql_tar/bin/mysqladmin -uroot -S $mysql_path/mysql_tar/data/mysql.sock password $passwd > /dev/null 2>&1
echo "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 <<EOF
use mysql;
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
}
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
}
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
}
function mysqld_restart(){
mysqld_stop
mysqld_start
sleep 5
}
function rpmType(){
isStart
use_check
mariadb_check
mysql_check
mysqlFile_check
mysql_PackageName_chech_rpmType
change_passwd_rpmType
change_access_rpmType
Migration_dir
mysql_successful_rpmType
}
function tarType(){
isStart
use_check
mysql_port_check
mysql_user_check
mysql_PackageName_chech_tarType
change_passwd_tarType
change_access_tarType
mysqld_restart
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