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?

  • Title mentions "two arguments". Password is one, the other? – techraf Feb 25 '16 at 7:21
  • The other is the sql statement CREATE DATABASE database_name – The amateur programmer Feb 25 '16 at 7:31
  • But it is not provided "after startup". Anyway what's the problem here? Why can't you provide password as an argument? – techraf Feb 25 '16 at 7:35
  • The server asks for password after the mysql -u root -p command. As this command is executed from vagrant provision script there is no way to provide the password manually. I don't know though if the password could be provided int the mysql command arguments though. – The amateur programmer Feb 25 '16 at 7:37
  • 2
    Sorry I didn't read the options for mysql command thoroughtly rnought it seems that I can provide the password int he command like so: mysql -u root --password=mypassword_here – The amateur programmer Feb 25 '16 at 7:40

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.


@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.

  • 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 8:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.