Updated: 22 October 2026 · Applies to: MariaDB 11.8 on Debian 13 and the MySQL 8.4 client, tested; the same options work on Ubuntu, AlmaLinux and Rocky Linux
To run a MySQL or MariaDB query straight from the Linux terminal, without opening the interactive shell, pass it to the client with -e (execute):
mysql -u root -p -e "SHOW DATABASES;"
The client asks for the password, prints the result and exits. On MariaDB servers the client is called mariadb; on Debian 13 mysql is a link to it, so both names work:
mariadb -e "SELECT VERSION();"
Output:
VERSION() 11.8.6-MariaDB-0+deb13u1 from Debian
Logging in: with or without a password
- On Debian and Ubuntu, the database root account is tied to the Linux root user (unix_socket on MariaDB, auth_socket on MySQL 8.4). Run the client as root or with
sudoand leave out-uand-p. - Every other account needs
-u USER -p. The-pon its own makes the client ask for the password. Without it you getERROR 1045 (28000): Access denied for user 'deploy'@'localhost' (using password: NO). - To open the interactive
mysql>shell instead, leave out-e. Connecting to a remote server is covered in How to Connect to a MySQL Database from the Command Line (Local and Remote).
As root on Debian 13:
sudo mariadb -e "SHOW DATABASES;"
As a normal database user, naming the database to use:
mysql -u deploy -p shopdb -e "SELECT COUNT(*) FROM orders;"
Choose the output format
In a terminal, results come as a table. When you pipe or redirect them, the client switches to plain tab-separated columns, which suits other tools. The examples use a small orders table in a database called shopdb:
mysql shopdb -e "SELECT customer, total FROM orders;"
Output in a terminal:
+----------+--------+ | customer | total | +----------+--------+ | alice | 49.90 | | bob | 15.00 | | carol | 120.50 | +----------+--------+
The options that change this:
-t(--table): always print a table, also into a file or pipe.-B(--batch): always print tab-separated columns.-N(--skip-column-names): leave out the header line.-E(--vertical): one line per column, handy for wide rows. Ending the query with\Ginstead of;does the same.-s(--silent): quieter output; with one value it prints just the value.
Data only, no header, ready for a script:
mysql -N -B shopdb -e "SELECT customer, total FROM orders;"
Output (tab-separated):
alice 49.90 bob 15.00 carol 120.50
One record shown vertically:
mysql shopdb -e "SELECT * FROM orders WHERE id = 1\G"
Output:
*************************** 1. row ***************************
id: 1
customer: alice
total: 49.90
created: 2026-10-01
Use query results in shell scripts
Store a single value in a variable:
COUNT=$(mysql -N -B shopdb -e "SELECT COUNT(*) FROM orders;") echo "Orders: $COUNT"
Output:
Orders: 3
Loop over rows (the columns are separated by a tab):
mysql -N -B shopdb -e "SELECT customer, total FROM orders;" | while IFS=$'\t' read -r name total; do echo "$name spent $total" done
Output:
alice spent 49.90 bob spent 15.00 carol spent 120.50
A quick comma-separated export, fine for simple data without commas or tabs inside the values:
mysql -N -B shopdb -e "SELECT * FROM orders;" | tr '\t' ',' > orders.csv
Save the output to a file
Redirect it, or use tee to see it on screen and save it at the same time:
mysql shopdb -e "SELECT * FROM orders;" | tee orders.txt
Add -t if you want the file to keep the table layout.
Run several statements or a .sql file
Separate statements with ; inside one -e string, or use a here document so you can write SQL over several lines without escaping quotes:
mysql shopdb <<'SQL' UPDATE orders SET total = 55.00 WHERE customer = 'alice'; SELECT customer, total FROM orders WHERE customer = 'alice'; SQL
Output:
customer total alice 55.00
For longer queries, save them in a file, for example report.sql:
SELECT customer, total FROM orders WHERE total > 20 ORDER BY total DESC;
Then feed the file to the client:
mysql shopdb < report.sql
By default the client stops at the first error in a file. --force makes it carry on with the next statement. It has no effect with -e, which always stops at the first error.
mysql --force shopdb < report.sql
For whole database dumps, see Import and Export a MySQL or MariaDB Database (Command Line).
Keep the password off the command line
-pPASSWORD (no space after -p) works, but the password ends up in your shell history, and the MySQL manual calls a password on the command line insecure. The MySQL 8.4 client warns about it every time: mysql: [Warning] Using a password on the command line interface can be insecure. For scripts, put the login in an option file that only you can read:
[client] user=deploy password=YOUR_PASSWORD
Save it as ~/.my.cnf (read automatically) or in another file, and lock it down:
chmod 600 ~/.my.cnf
For a file in another place, name it with --defaults-extra-file. It must be the first option: the MySQL 8.4 client refused it later in the line with unknown variable 'defaults-extra-file=...'.
mysql --defaults-extra-file=/root/deploy.cnf -N shopdb -e "SELECT CURRENT_USER();"
Output:
deploy@localhost
Common errors
ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock'(MySQL says "local MySQL server"): the database server is not running on this machine. Start it with systemctl, or add-hand the server's address if the database is elsewhere.ERROR 2003is the same problem over the network.ERROR 1045 (28000): Access denied: wrong user or password, or the user has no rights on that database. See How to grant user access to a database in MySQL command line?ERROR 1064 (42000)near a table name: the name is a reserved word, such asorder. Put it in backticks, and wrap the whole query in single quotes so the shell leaves the backticks alone.
The reserved-word fix as a command:
mysql shopdb -e 'SELECT * FROM `order`;'
On any error the client exits with status 1, so a script can stop with set -e or check $?.
Official documentation: MariaDB command-line client and mysql client options.