1. 创建目录存放脚本
[root@localhost ~]# mkdir /script
2. 下载MySQL的二进制包,并创建目录用来存放包
[root@localhost packages]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
3. 编写脚本
[root@localhost ~]# tree /script/
/script/
├── mysql.sh
└── packages
└── mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
1 directory, 2 files
//脚本内容
[root@localhost ~]# cat /script/mysql.sh
#!/bin/bash
route=/usr/local
data=/opt/data
yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel openssl openssl-devel ncurses-compat-libs perl ncurses-devel cmake
id mysql &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
fi
if [ ! -d $route/mysql-5.7.34-linux-glibc2.12-x86_64 ];then
tar xf packages/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C $route
cd $route
ln -s mysql-5.7.34-linux-glibc2.12-x86_64 mysql
chown -R mysql.mysql mysql
echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
fi
if [ ! -d $data ];then
mkdir -p $data
chown -R mysql.mysql $data
fi
echo "初始化数据库"
command=$(ls $data | wc -l)
if [ $command -eq 0 ];then
$route/mysql/bin/mysqld --initialize-insecure --user mysql --datadir $data
cat > /etc/my.cnf << EOF
[mysqld]
basedir = $route/mysql
datadir = $data
socket = /tmp/mysql.sock
port = 3306
pid-file = $data/mysql.pid
user = mysql
skip-name-resolve
EOF
sed -ri "s#^(basedir=).*#\1$route/mysql#g" /usr/local/mysql/support-files/mysql.server
sed -ri 's#^(datadir=).*#\1$data#g' /usr/local/mysql/support-files/mysql.server
cat > /usr/lib/systemd/system/mysql.service << EOF
[Unit]
Description=mysql server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now mysql
fi
4. 效果检测
[root@localhost ~]# systemctl status mysql.service
● mysql.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disab>
Active: active (running) since Sun 2021-11-07 12:02:03 CST; 4min 7s ago
Process: 10075 ExecStart=/usr/local/mysql/support-files/mysql.server start (code=exit>
Main PID: 10088 (mysqld_safe)
Tasks: 28 (limit: 23790)
Memory: 183.7M
CGroup: /system.slice/mysql.service
[root@localhost ~]# ss -anlt | grep 3306
LISTEN 0 80 *:3306 *:*
//脚本执行过程。
[root@localhost script]# bash -x mysql.sh
+ route=/usr/local
+ data=/opt/data
+ yum -y install gcc gcc-c++ make zlib zlib-devel pcre pcre-devel openssl openssl-devel ncurses-compat-libs perl ncurses-devel cmake
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repository AppStream is listed more than once in the configuration
上次元数据过期检查:0:01:13 前,执行于 2021年11月07日 星期日 12时06分03秒。
软件包 gcc-8.4.1-1.el8.x86_64 已安装。
软件包 gcc-c++-8.4.1-1.el8.x86_64 已安装。
软件包 make-1:4.2.1-10.el8.x86_64 已安装。
软件包 zlib-1.2.11-17.el8.x86_64 已安装。
软件包 zlib-devel-1.2.11-17.el8.x86_64 已安装。
软件包 pcre-8.42-4.el8.x86_64 已安装。
软件包 pcre-devel-8.42-4.el8.x86_64 已安装。
软件包 openssl-1:1.1.1g-15.el8_3.x86_64 已安装。
软件包 openssl-devel-1:1.1.1g-15.el8_3.x86_64 已安装。
软件包 ncurses-compat-libs-6.1-7.20180224.el8.x86_64 已安装。
软件包 perl-4:5.26.3-419.el8_4.1.x86_64 已安装。
软件包 ncurses-devel-6.1-7.20180224.el8.x86_64 已安装。
软件包 cmake-3.18.2-11.el8_4.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
+ id mysql
+ '[' 0 -ne 0 ']'
+ '[' '!' -d /usr/local/mysql-5.7.34-linux-glibc2.12-x86_64 ']'
+ '[' '!' -d /opt/data ']'
+ echo 初始化数据库
初始化数据库
++ wc -l
++ ls /opt/data
+ command=19
+ '[' 19 -eq 0 ']'