I'm using Centos 7 and trying to write a script that will install mysql 5.7 during a vagrant setup. I know how to change the root password manually, but how do you write this in a script?

I have this already:

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

Here is what I know to do manually: Get the temp password

grep 'temporary' /var/log/mysqld.log

Then I type and enter pass at prompt

mysql -u root -p
Enter Passwword:

Then change the pass or run the mysql_secure_installation

ALTER USER 'root'@'localhost' IDENTIFIED BY 'Newhakase-labs123@';
flush privileges;

1 Answer 1


I tested these commands against a CentOS 7.5 instance on Google Cloud. Once the script finishes executing, you should be able to log in to the database server with the new password.

# 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .