Shell 脚本一键部署 mysql

#!/bin/bash
set -e

ERR_LOG="/usr/local/mysql/data/error.log"

# 验证是否 root 
if [ "$(id -u)" -ne 0 ]; then
    echo "Permission denied! Please execute as the root user!";
    exit 1;
fi

# 解压文件
tar xf mysql-8.0.36-linux-glibc2.28-x86_64.tar.xz

if [ "$?" -ne 0 ]; then
    echo "File not found,or unzip failure.";
    exit 1;
fi

# 移动 mysql 到安装目录
mv mysql-8.0.36-linux-glibc2.28-x86_64 /usr/local/mysql

cd /usr/local/mysql/bin

# 查看依赖是否缺失
echo "检查 mysqld 和 mysql 是否缺依赖..."
ldd mysqld mysql | grep "not found" || echo "依赖检查通过"

# 创建 mysql 用户组和用户
if ! getent group mysql >/dev/null; then
    groupadd mysql
fi

if ! id mysql >/dev/null 2>&1; then
    useradd -r -g mysql -s /bin/false mysql
fi

# 修改 mysql 家目录下的文件所属
chown -R mysql:mysql /usr/local/mysql
mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql

# 设置环境变量
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
source /etc/profile

echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/zsh/zshenv

# 编写配置文件
cat > /etc/my.cnf << EOF
[client]
default_character_set=utf8
socket=/var/lib/mysql/mysql.sock 
[mysqld]
collation_server=utf8mb4_general_ci
character_set_server=utf8mb4
default-time-zone='+08:00'
# 跳过登录验证
#skip-grant-tables
#server_id=101
socket=/var/lib/mysql/mysql.sock
log_error=/usr/local/mysql/data/error.log
log_bin=/usr/local/mysql/data/binlog
binlog-ignore-db=mysql
log-bin=mall-mysql-bin
binlog_cache_size=1M
# binlog_format=mixed    8.0 has deprecated.
# expire_logs_days=7    8.0 has deprecated,use next line instead
binlog_expire_logs_seconds=604800
# slave_skip_errors=1062     8.0 has deprecated,use next line instead
replica_skip_errors=1062

#--innodb-buffer-pool-size=512M
slow_query_log=1
long_query_time=2

##限制mysql的内存占用,内存占用过多时可以使用
#skip-host-cache
#skip-name-resolve
#检测的表对象的最大数目
#performance_schema_max_table_instances=400
#表定义缓存中表的个数
#table_definition_cache=400
#表文件描述符的缓存大小
#table_open_cache=256
#performance_schema=off

EOF

# 初始化 mysql
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data || echo "初始化失败,可删除 /usr/local/mysql/data 重试"
# 获取初始密码
tmp_passwd="$(grep "A temporary password" $ERR_LOG | awk '{print $NF}')"
echo "初始密码为:$tmp_passwd";

# 创建 systemd 服务
cat > /usr/lib/systemd/system/mysqld.service << EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

# Have mysqld write its state to the systemd notify socket
Type=notify

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf $MYSQLD_OPTS 

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 10000

Restart=on-failure

RestartPreventExitStatus=1

# Set environment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1

PrivateTmp=false
EOF

# 重新加载 systemd 并启用服务
systemctl daemon-reload
systemctl enable mysqld --now

echo "MySQL 安装完成,修改 root 密码"
# 如果使用临时密码执行任何修改密码的操作 --connect-expired-password
/usr/local/mysql/bin/mysql -uroot -p"$tmp_passwd" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'mo';" --connect-expired-password

# 允许远程访问
/usr/local/mysql/bin/mysql -uroot -pmo -e"
USE mysql;UPDATE user SET Host='%' WHERE User='root';FLUSH PRIVILEGES;GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;
"