I need to execute a mysql query in one line using bash.
It should be something like this:
mysql database --user=\'root\' --password=\'my-password\' <
Writing your password in a command is generally a bad idea (people can read it over your shoulder, it probably gets stored in your shell history, etc.), but you can put your credentials in a file. Giving the file a name starting with .
makes it hidden, which is also more secure.
# .db.conf
[client]
database=myDatabase
user=myUserName
password=myPassWord
Make sure only you can read the file:
chmod 600 .db.conf
Then call MySQL like so:
mysql --defaults-extra-file=.db.conf -e "UPDATE database SET field1 = '1' WHERE id = 1111;"
or:
echo "UPDATE database SET field1 = '1' WHERE id = 1111;" | mysql --defaults-extra-file=.db.conf
Note that --defaults-extra-file
needs to be the first option supplied to mysql
otherwise it freaks out.