create mysql database with one line in bash

@Nitrodist's answer will work, but it's over-engineering the problem. MySQL's command-line client supports the very handy -e switch, like so:

mysql -uroot -e "create database 'foo'"

You can of course substitute any valid SQL into there.


According to MySQL's documentation, the mysql can take commands from stdin

shell> mysql -uroot < script.sql > output.tab

You can use Process Substitution to accomplish this:

shell> mysql -uroot <(echo "create database mydatabase;") 

or you can just pipe it to the stdin of mysql normally:

shell> echo "create database mydatabase;" | mysql -uroot

Can't test this personally, but I think this should work.

Edit: Another option is to use the -e option, specified here, like so:

shell> mysql -uroot -e "create database mydatabase;"