0

I have this following script which works fine on ubuntu for starting and creating a new table for MySQL database:

echo "CREATE DATABASE database_name" | mysql -u root -p

After this command is executed MySQL asks for password.

The only problem is that I want to do this on vagrant provision bash script, so I have to automate the process of entering the password. I basically have to give a single line of input to the MySQL program like this:

Enter password: password_goes_here

How do I combine the process of entering the database creation command and the password as text to the program?

2 Answers2

3

The simplest solution for this specific question is to provide the password in the mysql commands parameters as follows:

echo "CREATE DATABASE database_name" | mysql -u root --password=your_password

This provides the mysql with the password before it even gets to ask for it.

2

@The-amateur-programmer's answer is great, but I just wanted to add one thing to it. If you're worried about your password being visible when someone runs ps auxwww, you can also feed the password to MySQL using an Environment Variable called MYSQL_PWD:

mysql> set password for 'will@localhost' = PASSWORD('test');
Query OK, 0 rows affected, 1 warning (0.01 sec)

$ export MYSQL_PWD=test
$ mysql -uwill@localhost
Welcome to the MySQL monitor.

This, and other options, are explained here in the MySQL documentation.

Will
  • 2,754
  • 1
    Thanks for this information. Though in this situation this is not a problem as the server is only accessed through localhost and is not visible externally so it's okay for the password to be visible. – The amateur programmer Feb 25 '16 at 08:32