自动安装二进制mysql数据库SHELL脚本
首先将下载好的mysql二进制tar包上传到linux系统中,然后编辑好以下脚本再运行便会自动安装,如果配置文件有自定义路径,在下面脚本中修改一下即可
注意:复制以下脚本的时候注意语法的格式!是否和我脚本里的一致!!!
#!/bin/bash
#mysql二进制自动安装脚本
echo -e "\e[32m运行脚本前,请cd 脚本目录 \
并将mysql的tar包放在此目录下,否则会运行出错!\033[0m"
read -p "请输出您要安装的二进制包文件全名:" BIN_PKG
function log_output() {
if [ $? -eq 0 ];then
echo -e "\e[32m[INFO][$(date +"%F %T")] $1 成功! \033[0m"
else
echo -e "\e[31m[ERROR][$(date +"%F %T")] $1 失败! 已退出安装\033[0m"
exit
fi
}
function install_mysql() {
echo -e "\e[5;34m正在安装 mysql Version 5.7.35 ...\033[0m"
echo -e "\e[31m先为您检测mysql服务的依赖环境\033[0m"
# 安装依赖 libaio 用于同步I/O
if [[ `rpm -qa libaio | wc -l` > 0 ]];then
echo -e "\e[32m检测已安装好mysql依赖包libaio,无需再安装!\033[0m"
else
yum -y install libaio &> /dev/null
log_output "安装mysql依赖包libaio"
fi
BASE_DIR=`pwd`
BIN_PKG_DIR=$BASE_DIR/$BIN_PKG
MYSQL_PATH=/usr/local/mysql
# 创建 mysql用户、组
if ! grep -q '^mysql:' /etc/passwd ;then
useradd -s /sbin/nologin -d $MYSQL_PATH mysql
log_output "创建mysql用户"
fi
# 解压tar包到/usr/local/目录下
echo -e "\e[5;34mmysql二进制包正解压到/usr/local/目录下\033[0m"
tar -zxf $BIN_PKG -C ${BASE_DIR}
log_output "mysql二进制包解压"
# 更改目录名为mysql
# 匹配出 /usr/local/tar包名目录/改名为 /usr/local/mysql
mv ${BASE_DIR}/${BIN_PKG%.tar*} $MYSQL_PATH
# 设置mysqld服务的环境变量
result=`cat /etc/profile | grep mysql`
if [ -z "$result" ];then
echo "export PATH=$MYSQL_PATH/bin:$PATH" >> /etc/profile && source /etc/profile
log_output "配置mysqld服务的环境变量"
else
echo -e "\e[31m检测到mysqld服务的环境变量已配置好\e[0m"
fi
# 创建配置文件
if [ -f /etc/my.cnf ]
then
rm -f /etc/my.cnf
fi
cat > /etc/my.cnf <<EOF
[mysql]
default-character-set=utf8mb4
socket=/usr/local/mysql/mysql.sock
[mysqld]
user=mysql
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
log-error=/usr/local/mysql/logs/error.log
pid-file=/usr/local/mysql/pids/mysqld.pid
character-set-server=utf8
max_connections=8000
skip-name-resolve=ON
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
[mysqldump]
socket=/usr/local/mysql/mysql.sock
[mysqladmin]
socket=/usr/local/mysql/mysql.sock
[client]
socket=/usr/local/mysql/mysql.sock
EOF
# 赋予my.cnf的属主和属组为当前登录用户和组
chown -R mysql:mysql /etc/my.cnf
log_output "配置mysql初始化的my.cnf文件"
# 赋予data目录和base目录普通用户组
if [ ! -d $MYSQL_PATH/data ];then
mkdir $MYSQL_PATH/data
fi
#创建存放pid文件的目录
if [ ! -d $MYSQL_PATH/pids ];then
mkdir $MYSQL_PATH/pids
fi
# 创建存放mysqld服务错误日志目录
if [ ! -d $MYSQL_PATH/logs ];then
mkdir $MYSQL_PATH/logs
fi
chown -R mysql.mysql $MYSQL_PATH
log_output "自定义目录赋予权限"
#mysqld服务初始化,设置默认密码为空则将--initialize选项替换为--initialize-insecure选项
$MYSQL_PATH/bin/mysqld --defaults-file=/etc/my.cnf \
--socket=/usr/local/mysql/mysql.sock \
--pid-file=/usr/local/mysql/pids/mysqld.pid \
--log-error=/usr/local/mysql/logs/error.log \
--user=mysql --basedir=$MYSQL_PATH --datadir=$MYSQL_PATH/data \
--initialize-insecure
log_output "mysql初始化(密码为空)"
# 创建mysqld.service文件
if [ ! -f /usr/lib/systemd/system/mysqld.service ];then
touch /usr/lib/systemd/system/mysqld.service
fi
# 编写systemctl启动mysqld服务进程
cat > /usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Community Server
After=network.target syslog.target
[Service]
Type=forking
User=mysql
Group=mysql
PermissionsStartOnly=true
PIDFile=/usr/local/mysql/pids/mysqld.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/usr/local/mysql/support-files/mysql.server restart
ExecStop=/usr/local/mysql/support-files/mysql.server stop
[Install]
WantedBy=multi-user.target
EOF
log_output "配置systemctl管理mysqld服务进程文件"
# 重新加载ststemctl,启动mysqld服务文件并设为开机自启
systemctl daemon-reload
systemctl enable mysqld
systemctl start mysqld
log_output "msyql服务已启动并设置为开机自启"
}
install_mysql
#为什么要安装这个,在博客中https://www.liuzehe.top/archives/2021091501里有写
yum -y install libncurses* > /dev/null
log_output "命令行输入mysql实现登录数据库"
注意:如果是编辑的文本文件再上传到服务器的话,可能会出现格式转换错误的问题。在终端使用vim编辑脚本,输入":set ff",按下Enter键,查看脚本格式,如果格式是"[dos]"的,需要输入":set ff=unix"后按Enter,将格式更改为unix格式
脚本中自定义socket文件路径是/usr/local/mysql/mysql.sock,pid文件路径是/usr/local/mysql/pids/mysqld.pid,数据库默认数据存储位置在/usr/local/mysql/data等
Q.E.D.