mysql_install_shell

#!/bin/bash

if [ ! -d "/opt/software" ]; then
    sudo mkdir -p /opt/software
    echo "已创建目录/opt/software"
fi

cd /opt/software
# 添加MySQL存储库
sudo wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
sudo rpm -ivh mysql57-community-release-el7-11.noarch.rpm
echo "MySQL 5.7 社区版安装完成"
# 安装MySQL服务器
# sudo yum -y install mysql-server
sudo yum -y install mysql mysql-server --nogpgcheck
echo "MySQL服务器安装完成"
# 启动MySQL服务
sudo systemctl start mysqld
# 验证MySQL是否正在运行
status=$(sudo systemctl status mysqld | grep -o 'active (running)')
if [[ $status == "active (running)" ]]; then
    echo "MySQL服务启动成功"
else
    echo "MySQL服务启动失败"
    exit 1
fi

# 设置MySQL服务在开机时自动启动
sudo systemctl enable mysqld
if [ $? -eq 0 ]; then
    echo "设置MySQL服务在开机时自动启动成功"
else
    echo "设置MySQL服务在开机时自动启动失败"
fi

# 修改MySQL密码
mysql --connect-expired-password -uroot -p$(sudo grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}') -e "SET GLOBAL validate_password_policy=LOW; SET GLOBAL validate_password_length=4; ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';"
# 检查密码修改是否成功
if [ $? -eq 0 ]; then
    echo "MySQL密码修改成功!"
    echo "MySQL账号:root,密码:root"
else
    echo "MySQL密码修改失败。"
fi

mysql -uroot -proot -e "use mysql; update user set host='%' WHERE user='root'; flush privileges;"
# 检查上一个命令是否执行成功
if [ $? -eq 0 ]; then
    echo "已经设置MySQL可以远程访问且允许任何人连接"
else
    echo "设置允许任何人连接失败"
fi

# 清理临时文件
sudo rm mysql57-community-release-el7-11.noarch.rpm

  

posted @ 2024-10-30 09:48  hacker-dreamer  阅读(3)  评论(0编辑  收藏  举报