mysql --execute: Usage, Options & Common CI Errors
mysql -e runs a single SQL statement and exits, ideal for scripted checks.
mysql --execute is the one-liner form used for migrations and assertions in CI. The output-parsing essentials are -N and -B, which strip the formatting so values are easy to capture.
What it does
mysql -e "SQL" runs a single statement (or several separated by ;) without entering the interactive shell. With batch/skip-column-names flags it produces tab-separated, header-free output suited to shell capture.
Common usage
mysql -h 127.0.0.1 -u root -psecret -e 'SELECT VERSION()'
mysql -h 127.0.0.1 -u root -psecret app -e 'SELECT COUNT(*) FROM users'
mysql -h db -u root -psecret -N -B -e 'SELECT id FROM t' app # raw rows
COUNT=$(mysql -h 127.0.0.1 -u root -psecret -N -e 'SELECT 1' app)Options
| Flag | What it does |
|---|---|
| -e "SQL" | Execute the statement and exit |
| -N / --skip-column-names | Omit the header row |
| -B / --batch | Tab-separated, unaligned output |
| -s / --silent | Less output (combine with -N) |
| <dbname> | Default database (positional, after options) |
| --default-character-set | Set client charset |
Common errors in CI
ERROR 1046 (3D000): No database selected - pass the database name as a positional argument or qualify tables as db.table. Shell quoting bites: wrap the SQL in single quotes so the shell does not expand $ or backticks, and remember -e takes one argument. For capturing a scalar in a variable, combine -N -B so there is no header or box-drawing to strip.