MySQL脚本安装

#!/bin/bash

# 下载安装包
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
echo "下载成功!"
# 解压缩安装包
tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

# 移动文件夹至安装目录
mv mysql-8.0.20-linux-glibc2.12-x86_64 /usr/local/mysql
echo "安装目录创建成功!"

# 创建MySQL用户和用户组
useradd -s /bin/nologin mysql
echo "创建MySQL用户成功!"

# 更改安装目录权限
mkdir -p /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql
echo "chown -R mysql:mysql /usr/local/mysql"

# 设置MySQL环境变量
echo 'export PATH="/usr/local/mysql/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
echo "设置MySQL环境变量成功!"

# 配置/etc/my.cnf
tee /etc/my.cnf > /dev/null <<EOF
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
socket = /tmp/mysql.sock
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default-storage-engine=InnoDB
default-authentication-plugin=mysql_native_password
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
EOF

echo "配置/etc/my.cnf成功"

# 安装依赖
yum install libaio libaio-devel ncurses ncurses-devel openssl openssl-devel -y
# 初始化MySQL数据目录
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
echo "MySQL初始化成功"

# 启动MySQL服务
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
/etc/init.d/mysqld restart
systemctl enable mysqld
echo "启动MySQL"

# 获取初始密码
initial_password=$(grep 'temporary password' /usr/local/mysql/data/error.log | awk '{print $NF}')
echo "获取初始密码为:$initial_password" 

# 修改root用户密码
/usr/local/mysql/bin/mysql -uroot -p"$initial_password" --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'root123';"
echo "修改MySQL初始密码为:root123"

# 重启MySQL服务
pkill -9 mysqld
service mysql start
echo "安装完成,请您放心使用,登录密码为:root123"

# 可选:防火墙设置(如果启用了防火墙,确保3306端口开放)
# firewall-cmd --zone=public --add-port=3306/tcp --permanent
# firewall-cmd --reload
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值