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

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


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


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