Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate mysql_secure_installation with echo command via a shell script

Tags:

linux

shell

mysql

I am trying to automate mysql_secure_installation script with automated response. My code is as follows :

echo "& y y abc abc y y y y" | ./usr/bin/mysql_secure_installation 

The actual questions which i am automating are as follows:

Enter current password for root (enter for none): <enter> Set root password? [Y/n] y New password: abc Re-enter new password: abc Remove anonymous users? [Y/n] y Disallow root login remotely? [Y/n] y Remove test database and access to it? [Y/n] y Reload privilege tables now? [Y/n] y 

But it gives me an error "Sorry you cannot use an empty password here" but in the screen i used to press the return key for the first question.

like image 430
cucucool Avatar asked Jun 17 '14 18:06

cucucool


1 Answers

I stumbled upon this question but decided to run the queries manually through a Bash script:

#!/bin/bash  # Make sure that NOBODY can access the server without a password mysql -e "UPDATE mysql.user SET Password = PASSWORD('CHANGEME') WHERE User = 'root'" # Kill the anonymous users mysql -e "DROP USER ''@'localhost'" # Because our hostname varies we'll use some Bash magic here. mysql -e "DROP USER ''@'$(hostname)'" # Kill off the demo database mysql -e "DROP DATABASE test" # Make our changes take effect mysql -e "FLUSH PRIVILEGES" # Any subsequent tries to run queries this way will get access denied because lack of usr/pwd param 
like image 195
Sleavely Avatar answered Nov 16 '22 00:11

Sleavely