-
-
Save coderua/5592d95970038944d099 to your computer and use it in GitHub Desktop.
| #!/bin/bash | |
| aptitude -y install expect | |
| // Not required in actual script | |
| MYSQL_ROOT_PASSWORD=abcd1234 | |
| SECURE_MYSQL=$(expect -c " | |
| set timeout 10 | |
| spawn mysql_secure_installation | |
| expect \"Enter current password for root (enter for none):\" | |
| send \"$MYSQL\r\" | |
| expect \"Change the root password?\" | |
| send \"n\r\" | |
| expect \"Remove anonymous users?\" | |
| send \"y\r\" | |
| expect \"Disallow root login remotely?\" | |
| send \"y\r\" | |
| expect \"Remove test database and access to it?\" | |
| send \"y\r\" | |
| expect \"Reload privilege tables now?\" | |
| send \"y\r\" | |
| expect eof | |
| ") | |
| echo "$SECURE_MYSQL" | |
| aptitude purge expect |
Don't forget to wipe your bash history ;)
For Mysql 5.7, the prompts have changed. Here is an update to the expect component for that environment:
SECURE_MYSQL=$(expect -c "
set timeout 3
spawn mysql_secure_installation
expect "Press y|Y for Yes, any other key for No: "
send "n\r"
expect "New password:"
send "$NEW_MYSQL_PASSWORD\r"
expect "Re-enter new password:"
send "$NEW_MYSQL_PASSWORD\r"
expect "Remove anonymous users?"
send "y\r"
expect "Disallow root login remotely?"
send "y\r"
expect "Remove test database and access to it?"
send "y\r"
expect "Reload privilege tables now?"
send "y\r"
expect eof
")
Only for the sake of my own understanding of this script
when executing mysql_secure_installation
expect "Remove anonymous users?"
send "y\r"
does "send "y\r" simply send the "y" key and then the "return" key?
I'm not a bash expert so I'm thankful for any kind of explanation
Only for the sake of my own understanding of this script
when executing mysql_secure_installation
expect "Remove anonymous users?"
send "y\r"
does "send "y\r" simply send the "y" key and then the "return" key?I'm not a bash expert so I'm thankful for any kind of explanation
Yes, you are right)
Works like a charm, thanks for sharing!
Hi Vladimir,
Thanks you very much for that clean piece of code, just tested with MariaDB 10.1 || centos 7.3, and unless the aptitude commands (replaced by some yum), everything works perfectly.
++