View previous topic :: View next topic |
Author |
Message |
pactoo Guru


Joined: 18 Jul 2004 Posts: 553
|
Posted: Thu Aug 25, 2011 8:06 am Post subject: mysql: execute queries from bash |
|
|
Hello,
I am trying to do a simple query of a mysql database from bash. But I do not want to read that query from a script.
Something like:
mysql -u USER -pPASS -D database "select * from table;"
Instead of:
mysql -u USER -pPASS -D database < file.sql
Currently I am having trouble realising this |
|
Back to top |
|
 |
tomk Bodhisattva


Joined: 23 Sep 2003 Posts: 7221 Location: Sat in front of my computer
|
Posted: Thu Aug 25, 2011 10:07 am Post subject: |
|
|
This is the way I do queries from bash:
Code: | echo "select * from table;" | mysql -u USER -pPASS -D database |
If you don't want the column names displayed as the first row add -s to the mysql options. You can also change the output format: -H for HTML, -t for table, -E for vertical or -X for XML. _________________ Search | Read | Answer | Report | Strip |
|
Back to top |
|
 |
BrummBrumm n00b

Joined: 04 Jan 2008 Posts: 58
|
Posted: Thu Aug 25, 2011 10:21 am Post subject: |
|
|
hi.
try this:
mysql --user="someuser" --password="pw" -D "dbname" -e 'your-sql-command;'
you can also do something like
mysql --user="user" --password="pw" << EOF
command-1;
command-2;
EOF |
|
Back to top |
|
 |
pactoo Guru


Joined: 18 Jul 2004 Posts: 553
|
Posted: Fri Aug 26, 2011 6:48 am Post subject: |
|
|
Thanks very much. For some reason, I have overseen (or misinterpreted) the "-e" option. Multiple times actually. However, the other hints may be helpful, too |
|
Back to top |
|
 |
|