The MySQL CLI: Four Ways to Feed Inline SQL
Four ways to feed SQL to mysql without an interactive session: a single -e statement, semicolon-separated statements, a quoted heredoc, or a .sql file; plus --batch for piping.
Four ways to feed SQL to mysql without an interactive session:
1. Single inline statement (-e):
mysql … -e "SELECT * FROM asset WHERE uploaded_package_id = 100"
2. Multiple statements: semicolon-separate inside one -e:
mysql … -e "USE app_production; SELECT COUNT(*) FROM asset; SELECT NOW();"
3. Heredoc: cleanest for multi-line; quote the delimiter ('SQL') so the shell won’t expand $ / backticks:
mysql … -D app_production <<'SQL'
SELECT a.asset_id, a.`size`
FROM asset AS a
WHERE uploaded_package_id = 100;
SQL
4. From a .sql file:
mysql … app_production < query.sql # shell redirect
mysql … -e "source query.sql" # MySQL's own source command
- For machine-parseable output: add
--batch(tab-separated) +--skip-column-names(drop the header), then pipe tojq/ Python.
From this session: piped --batch --skip-column-names output into Python to build a per-asset_id JSON config from the prod query result.
One statement →
-e; multi-line → heredoc (quote the delimiter); piping → add--batch --skip-column-names.
References
- MySQL command-line options: https://dev.mysql.com/doc/refman/8.0/en/command-line-options.html
- Connect to MySQL from the CLI: https://oneuptime.com/blog/post/2026-03-31-mysql-connect-cli-client/view
Related: back to the remote DB overview; pairs with one-shot vs REPL and the MySQL connection flags.