我正在使用 Centos 7 并尝试编写一个脚本来在 vagrant 设置期间安装 mysql 5.7。我知道如何手动更改root密码,但是如何在脚本中编写它?
我已经有了这个:
wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum -y localinstall mysql57-community-release-el7-7.noarch.rpm
yum -y install mysql-community-server
service mysqld start
这是我知道要手动执行的操作:获取临时密码
grep 'temporary' /var/log/mysqld.log
然后我在提示时键入并输入 pass
mysql -u root -p
Enter Passwword:
然后更改pass或运行mysql_secure_installation
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Newhakase-labs123@';
flush privileges;
答案1
我针对 Google Cloud 上的 CentOS 7.5 实例测试了这些命令。脚本执行完成后,您应该能够使用新密码登录数据库服务器。
#!/bin/bash
# Description: Set up MySQL Community Release 5.7
# Get the repo RPM and install it.
wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum -y install ./mysql57-community-release-el7-7.noarch.rpm
# Install the server and start it
yum -y install mysql-community-server
systemctl start mysqld
# Get the temporary password
temp_password=$(grep password /var/log/mysqld.log | awk '{print $NF}')
# Set up a batch file with the SQL commands
echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Newhakase-labs123@'; flush privileges;" > reset_pass.sql
# Log in to the server with the temporary password, and pass the SQL file to it.
mysql -u root --password="$temp_password" --connect-expired-password < reset_pass.sql