场景:为了在无网、外网 mysql 安装方便,这里分享一个自己编写得 shell脚本
- 这里以当前最新版
mysql 8.2.0
;centos-7
二进制包下载:
下载地址
mysql_install.sh
#!/bin/bash
# 解压安装包
tar -xf mysql-8.2.0-linux-glibc2.17-x86_64.tar.xz
# 移动解压后的文件夹到指定位置
mv mysql-8.2.0-linux-glibc2.17-x86_64 /usr/local/mysql
# 创建MySQL数据目录
mkdir -p /var/lib/mysql
# 创建MySQL组和用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
# 修改文件夹权限
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /var/lib/mysql
# 创建MySQL配置文件
cat > /usr/local/mysql/my.cnf << EOF
[mysqld]
basedir=/usr/local/mysql
datadir=/var/lib/mysql
server_id=1
port=3306
[mysql]
default-character-set=utf8mb4
[client]
port=3306
socket=/var/lib/mysql/mysqlx.sock
EOF
# 初始化MySQL
cd /usr/local/mysql
./bin/mysqld --defaults-file=/usr/local/mysql/my.cnf --initialize-insecure --user=mysql
# 创建MySQL服务
cat > /etc/systemd/system/mysql.service <<EOF
[Unit]
Description=MySQL8.2.0 Server
After=syslog.target
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf
EOF
# 启动MySQL服务并设置开机启动
systemctl daemon-reload
systemctl start mysql
systemctl enable mysql
mysql_remove.sh
#!/bin/bash
# 停止mysql服务
sudo systemctl stop mysql
# 删除mysql用户组
sudo userdel mysql
sudo groupdel mysql
# 删除mysql服务配置
sudo systemctl disable mysql
sudo rm /etc/systemd/system/mysql.service
# 删除数据文件
sudo rm -rf /var/lib/mysql
# 删除mysql二进制文件
sudo rm -rf /usr/local/mysql
echo "mysql已成功卸载!"