Hey,
Automation MySQL secure installation by a shell script.
Platform: AWS Cloud
Instance Type: t2.micro
MySQL Edition: MySQL Community Edition 5.8
What work this far beautifully is:
Code:
sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm -y
sudo amazon-linux-extras install epel -y
sudo yum -y install mysql-community-server
sudo systemctl enable mysqld
sudo systemctl start mysqld
sudo systemctl status mysqld
export CURRENT_MYSQL_PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log| awk '{print $NF}')
echo ${CURRENT_MYSQL_PASSWORD}
export NEW_MYSQL_PASSWORD='NS!rQdzhF44^y2VEiI'
Now comes the step of MySQL secure installation which is where I am issue.
Script:
Code:
#!/bin/bash
set -x
if [ -n "${1}" -a -z "${2}" ]; then
# Setup root password
CURRENT_MYSQL_PASSWORD=''
NEW_MYSQL_PASSWORD="${1}"
elif [ -n "${1}" -a -n "${2}" ]; then
# Change existens root password
CURRENT_MYSQL_PASSWORD="${1}"
NEW_MYSQL_PASSWORD="${2}"
else
echo "Usage:"
echo " Setup mysql root password: ${0} 'your_new_root_password'"
echo " Change mysql root password: ${0} 'your_old_root_password' 'your_new_root_password'"
exit 1
fi
SECURE_MYSQL=$(expect -c "
set timeout 3
spawn mysql_secure_installation
expect \Enter password:\"
send \"${CURRENT_MYSQL_PASSWORD}\r\"
expect \New password:\"
send \"${NEW_MYSQL_PASSWORD}\r\"
expect \Re-enter new password:\"
send \"${NEW_MYSQL_PASSWORD}\r\"
expect \"Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :\"
send \"y\r\"
expect \"Remove anonymous users? (Press y|Y for Yes, any other key for No) :\"
send \"y\r\"
expect \"Disallow root login remotely? (Press y|Y for Yes, any other key for No) :\"
send \"y\r\"
expect \"Remove test database and access to it? (Press y|Y for Yes, any other key for No) :\"
send \"y\r\"
expect \"Reload privilege tables now? (Press y|Y for Yes, any other key for No) :\"
send \"y\r\"
expect eof
")
echo "${SECURE_MYSQL}"
exit 0
However, when I start executing, I get an error:
Code:
[ec2-user@ip-172-31-40-83 ~]$ ./dbsetup.sh ${CURRENT_MYSQL_PASSWORD} ${NEW_MYSQL_PASSWORD}
+ PURGE_EXPECT_WHEN_DONE=0
+ '[' -n '5Wg9&WLGzAe6' -a -z 'NS!rQdzhF44^y2VEiI' ']'
+ '[' -n '5Wg9&WLGzAe6' -a -n 'NS!rQdzhF44^y2VEiI' ']'
+ CURRENT_MYSQL_PASSWORD='5Wg9&WLGzAe6'
+ NEW_MYSQL_PASSWORD='NS!rQdzhF44^y2VEiI'
++ expect -c '
set timeout 3
spawn mysql_secure_installation
expect \Enter password:"
send "5Wg9&WLGzAe6\r"
expect \New password:"
send "NS!rQdzhF44^y2VEiI\r"
expect \Re-enter new password:"
send "NS!rQdzhF44^y2VEiI\r"
expect "Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect eof
'
invalid command name "password:""
while executing
"password:""
invoked from within
"expect \Enter password:""
+ SECURE_MYSQL='spawn mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: '
+ echo 'spawn mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: '
spawn mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
+ '[' 0 -eq 1 ']'
+ exit 0
[ec2-user@ip-172-31-40-83 ~]$ mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)