How to execute a MySQL command from a shell script?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Executing a MySQL command from a shell script is usually just a matter of calling the mysql client with the right credentials and SQL. The hard part is not syntax. It is doing it safely, especially around passwords, quoting, and error handling. This guide shows the standard patterns and the security tradeoffs that matter in real automation.
The Basic One-Line Pattern
The mysql client can run a single SQL statement with the -e flag.
This works, but putting the password directly on the command line is often a bad idea because it can leak through shell history or process inspection.
Prefer an Option File for Credentials
A better pattern is to use a MySQL option file.
Example ~/.my.cnf:
Then your script becomes much cleaner:
This is simpler and safer than embedding secrets in the script itself. The file should have restrictive permissions.
Execute Multiple Statements with a Here-Document
For longer SQL, a here-document is often clearer than cramming everything into one quoted string.
This is easier to read, easier to maintain, and less fragile than deeply nested quoting.
Capture Query Output in the Script
Sometimes you need the result inside a shell variable.
Useful flags here are:
- '
-Nto skip column names' - '
-sfor silent output that is easier to parse'
That makes the result much cleaner for script consumption.
Pass Host and Port Explicitly When Needed
If the database is not local or you want predictable connection behavior, specify host and port directly.
Using 127.0.0.1 rather than localhost can matter because some MySQL clients treat localhost as a Unix socket connection rather than TCP.
Error Handling in Shell Scripts
At minimum, use strict shell settings and fail fast.
If the MySQL command fails, the script exits immediately because of set -e. That is usually better than continuing with partial state.
Using Environment Variables Carefully
If you cannot use an option file, environment variables are another possibility.
This is still not ideal, and the MySQL ecosystem itself discourages long-term reliance on MYSQL_PWD, but it is sometimes used in controlled automation contexts.
Practical Automation Example
A simple backup metadata script might look like this:
If variables are interpolated into SQL, be careful with quoting and escaping. For untrusted input, a shell script is usually the wrong layer for complex query construction.
Common Pitfalls
- Putting the database password directly on the command line in production scripts.
- Forgetting to quote SQL correctly when mixing shell variables and SQL strings.
- Parsing default
mysqloutput without using flags such as-Nand-s. - Assuming
localhostand127.0.0.1behave identically for MySQL connections. - Continuing after a failed query because the script does not enable strict error handling.
Summary
- Use
mysql -efor simple one-line commands from a shell script. - Use a here-document for multi-line SQL.
- Prefer a MySQL option file over inline passwords.
- Use
-Nand-swhen you need script-friendly output. - Treat quoting, credentials, and error handling as part of the real solution, not as minor details.
Related reading
- How to execute a sql script file in a Kubernetes Pod?
- How to execute an SSIS package from .NET?
- How to execute IN SQL queries with Spring's JDBCTemplate effectively?
- How to execute MySQL command from the host to container running MySQL server?
- How to execute raw SQL in Flask-SQLAlchemy app
- How to export a mysql database using Command Prompt?
- How to export an existing dynamo table schema to json?
- How to export an existing dynamo table schema to json?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.