I am trying to automate mysql secure installation using linux shell scriping. I have below code got from https://gist.github.com/Mins/4602864.
#!/bin/bash
MYSQL=$(grep 'temporary password' /var/log/mysqld.log | awk '{print $11}')
MYSQL_ROOT_PASSWORD="test@123"
SECURE_MYSQL=$(expect -c "
set timeout 10
spawn mysql_secure_installation
expect "Enter password for user root:"
send "$MYSQL\r"
expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "New password:"
send "$MYSQL_ROOT_PASSWORD\r"
expect "Re-enter new password:"
send "$MYSQL_ROOT_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"
But i am getting error
./sql.sh: command substitution: line 48: syntax error near unexpected token `('
./sql.sh: command substitution: line 48: ` expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"'
I have tried to figure out the error but no success.
Try this as it is, seems like there is a some issue with
(
and pipe|
so I had to escape them.Source Code
Debug executionAnd I have verified the login with new password
test@123
.Below script worked for me.