Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you run a single query through mysql from the command line?

People also ask

How do I run a single query in MySQL workbench?

To do that, first select the desired database from the left column menu by double-clicking it. Then type in the MySQL query you want to run in the text field in the middle of the program window and use the yellow lightning button above that text field to run the query.


mysql -u <user> -p -e 'select * from schema.table'

(Note the use of single quotes rather than double quotes, to avoid the shell expanding the * into filenames)


mysql -uroot -p -hslavedb.mydomain.com mydb_production -e "select * from users;"

From the usage printout:

-e, --execute=name
Execute command and quit. (Disables --force and history file)


here's how you can do it with a cool shell trick:

mysql -uroot -p -hslavedb.mydomain.com mydb_production <<< 'select * from users'

'<<<' instructs the shell to take whatever follows it as stdin, similar to piping from echo.

use the -t flag to enable table-format output


If it's a query you run often, you can store it in a file. Then any time you want to run it:

mysql < thefile

(with all the login and database flags of course)


echo "select * from users;" | mysql -uroot -p -hslavedb.mydomain.com mydb_production