在 CentOS 上,如何使用 shell 脚本将 MySQL 的 root 用户密码设置为 'Linux0224',并同时创建一个名为 'wordpress' 的数据库?
时间: 2024-12-22 07:17:27 浏览: 9
在 CentOS 上,你可以使用以下步骤编写一个 shell 脚本来完成任务:
首先,你需要通过 `mysql` 客户端登录到 MySQL 并设置 root 密码。然后,你可以手动创建数据库。这里有一个基本的 bash 脚本示例:
```bash
#!/bin/bash
# 检查 mysqld 是否正在运行
if ! pgrep mysqld > /dev/null; then
echo "MySQL is not running. Starting it now..."
sudo systemctl start mysqld
while ! pgrep mysqld > /dev/null; do
sleep 1
echo "Waiting for MySQL to start..."
done
fi
# 设置 root 密码
echo "Changing MySQL root password..."
echo "Enter current password for root (press Enter if blank):" # 默认为空
echo ""
read -s NEW_ROOT_PASSWORD # 读取用户输入的新密码,不会显示
echo ""
mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD('$NEW_ROOT_PASSWORD') WHERE User='root';"
mysql -u root -e "FLUSH PRIVILEGES;"
# 创建 'wordpress' 数据库
echo "Creating WordPress database..."
mysql -u root -p$NEW_ROOT_PASSWORD << EOF
CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost' IDENTIFIED BY 'your_wordpress_password';
EXIT;
EOF
echo "WordPress database created successfully."
# 关闭提示符
unset NEW_ROOT_PASSWORD
echo "Please restart MySQL for changes to take effect:"
sudo systemctl restart mysqld
echo "Remember to secure your WordPress installation and change the default credentials!"
#
阅读全文