MySQL/MariaDB: Run SQL Queries From A Shell Prompt / Command Line

See all MySQL Database Server related FAQ
How do I specify or run MySQL or MariaDB SQL queries on the UNIX or Linux command line?

The mysql command line has option to execute the SQL statement and quit. This is also useful for running sql queries from a shell script or the bash prompt. [donotprint]
Tutorial details
Difficulty level Easy
Root privileges No
Requirements mysql
Est. reading time 1 minutes
[/donotprint]
Advertisement

Syntax

The syntax is as follows for both MariaDB and MySQL client:

mysql -u user -p -e 'Your SQL Query Here' database-name

OR

mysql -u USER -p PASSWORD -h MYSQLSERVERNAME -e 'select * from foo...' database-name

Where,

  • -u : Specify mysql database user name
  • -p : Prompt for password
  • -e : Execute sql query
  • database : Specify database name

Examples

To list all database, enter:
$ mysql -u vivek -p -e 'show databases;'
To list count all rows, enter:
$ mysql -u vivek -p -e 'SELECT COUNT(*) FROM quotes' cbzquotes
Sample output:

Enter password: 
+----------+
| count(*) |
+----------+
|      471 | 
+----------+

Tell mysql to display output a page at a time, by using more or less pager:
$ mysql --pager=/usr/bin/less -u vivek -p -e 'SELECT COUNT(*) FROM quotes' cbzquotes
Redirect out to a file:
$ mysql -u vivek -p 'PassWord' -e 'SELECT COUNT(*) FROM quotes' cbzquotes > sql.output.txt

Say hello to MYSQL configuration file

To avoid password prompt just create ~/.my.cnf file as follows:

[client]
# for local server use localhost
#host=localhost
host=10.0.1.100
user=vivek
password=myPassword
 
[mysql]
pager=/usr/bin/less

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

21 comments… add one
  • Ryan Jun 14, 2012 @ 11:06

    Great post thanks for sharing. Should really be updated to include Andrew McGregor’s correction.

  • arun Aug 1, 2012 @ 7:06

    why “where class” is not working properly? please explain

  • Sean Sep 14, 2012 @ 11:19

    This post saved my butt. I had to edit about 1500 records between 2 databases and this made it happen in a hour instead of about 2 weeks.

  • Ambiorix Rodriguez Mar 10, 2013 @ 4:54

    This could help to avoid password prompt
    mysql –user=dbuser –password=’mypassword’ asterisk -e ‘SELECT * FROM cdr’;

  • T. Jun 6, 2013 @ 7:49

    Very useful, especially with double quote (if you want to pass variables from bash to
    mysql, for example). Thank you very much.

  • udit Feb 2, 2015 @ 10:41

    i used the history command to see the previously executed commands how can i remove all the contents without actually deleting the file or without using rm command

  • trungvan Jun 20, 2015 @ 2:17

    work for me (centos 6):
    mysql -u $rootusr -p$rootpw -e “INSERT INTO db_site (name, domain) VALUES (‘$domain’, ‘$domain’)” db_session;

    note: remove chracter: “`” in sql string

  • Troy Frech Sep 11, 2015 @ 14:13

    This does not work…
    mysql -uUSER -pPASSWORD -e “select * from this-test” dbname

    …because of the “-” in the query. Is there an escape character to allow for dashes in a query? The same query works fine if I log in to mysql and enter it at the mysql prompt but it doesn’t work from a Linus prompt.

    • 🛡️ Vivek Gite (Author and Admin) Vivek Gite Sep 11, 2015 @ 15:14

      Try single quotes and –?
      mysql -uUSER -pPASSWORD -e 'select * from this-test' dbname
      OR
      mysql -uUSER -pPASSWORD -e -- 'select * from this-test' dbname

  • raghu varma Mar 15, 2016 @ 10:08

    can we have the connection idle without closing after the query is executed?

  • sdag Mar 16, 2017 @ 19:53

    If I’m getting the count(*), is there any way to have it written to a variable in my shell script or does it have to go to a file and then be parsed?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.