243

I'm looking to be able to run a single query on a remote server in a scripted task.

For example, intuitively, I would imagine it would go something like:

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

7 Answers 7

367
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)

11
  • 3
    C:\Program Files (x86)\MySQL\MySQL Server 5.7\bin>mysql.exe -u root -p -e "my query"---->ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    – Dr.jacky
    Commented Mar 12, 2017 at 11:38
  • 3
    This should be the first result in a google search (for "mysql exec sql from command line") and not the huge mysql site! Commented Apr 4, 2017 at 17:48
  • 5
    Also, if you want to strip the header and table format you can use mysql -u <user> -p -B --disable-column-names -e 'select * from schema.table'
    – dvlcube
    Commented Oct 11, 2017 at 18:40
  • 2
    @tinybyte Assuming Linux: Insinde doublequotes the asterisk * gets expanded to all files in the current directory, while this expansion does not happen in singlequotes, thats the reason why. Doublequotes will work for quesries without the *.
    – NobbZ
    Commented Apr 3, 2018 at 7:39
  • 5
    @Dr.jacky I'm sure you don't really have the need any more, but for the future, if your root user has no password, dont pass -p option as blank, just dont pass it at all i.e. mysql.exe -u root -e "my query"
    – solidau
    Commented Jun 25, 2019 at 16:29
40
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)

24

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

16

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)

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

As by the time of the question containerization wasn't that popular, this is how you pass a single query to a dockerized database cluster with Ansible, following @RC.'s answer:

ansible <host | group > -m shell -a "docker exec -it <container_name | container_id> mysql -u<your_user> -p<your_pass> <your_database> -e 'SELECT COUNT(*) FROM my_table;'"

If not using Ansible, just login to the server and use docker exec -it ... part.

MySQL will issue a warning that passing credentials in plain text may be insecure, so be aware of your risks.

0

From the mysql man page:

   You can execute SQL statements in a script file (batch file) like this:

       shell> mysql db_name < script.sql > output.tab

Put the query in script.sql and run it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.