How to pass input from one script into another using bash
Solution 1:
This bug report on the MySQL dev site addresses the issue of mysql_secure_installation
requiring an interactive user session.
There is some good discussion there that has some tips and examples that make use of expect
as a workaround. The best advice that seems to address your desire to run the script non-interactively (i.e.: without direct user interaction) is the answer by Daniël van Eeden which provides the following expect
script:
#!/usr/bin/expect --
spawn /usr/local/mysql/bin/mysql_secure_installation
expect "Enter current password for root (enter for none):"
send "\r"
expect "Set root password?"
send "y\r"
expect "New password:"
send "password\r"
expect "Re-enter new password:"
send "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"
puts "Ended expect script."
Also, it seems like the overarching issue of base MySQL installs having non-secure stuff in place seems to be addressed in MySQL 5.6.8 but only for new installs via RPMs or source code installs that are made with the --random-password
option:
New RPM install operations (not upgrades) invoke mysql_install_db with the --random-passwords option. As a consequence, RPM installs from this version onward will have their root accounts secured, and will have no anonymous-user accounts. (Install operations using RPMs for Unbreakable Linux Network are unaffected because they do not use mysql_install_db.)
For install operations using a binary .tar.gz distribution or a source distribution, you can invoke mysql_install_db with the --random-passwords option manually to make your MySQL installation more secure. This is recommended, particularly for sites with sensitive data.
Solution 2:
You can't do that. If you want to interact with a script you have to use something like expect(1) - there are examples here on SF and on the wider internet of how to do this.
You can emulate much of the mysql_secure_Installation with
/usr/bin/mysql -uroot -e "DELETE FROM mysql.user WHERE User=\'\'; DELETE FROM mysql.user WHERE User=\'root\' AND Host NOT IN (\'localhost\', \'127.0.0.1\', \'::1\'); DROP DATABASE IF EXISTS test; FLUSH PRIVILEGES;"
All you need to do after that is set a root password.