How to create a database from shell command in MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Creating a MySQL database from the shell is a standard admin task in local development, CI pipelines, and production provisioning. The core operation is simple, but reliable automation requires safe authentication handling, idempotent SQL, and explicit charset and collation choices. If you skip these details, you can end up with inconsistent environments or scripts that fail unpredictably on reruns. This guide shows practical command-line patterns for creating databases, verifies outcomes, and highlights security-friendly ways to run the same logic in scripts.
Core Sections
Basic one-liner with mysql -e
You can execute SQL directly from the shell:
After entering your password, MySQL runs the statement and exits.
To avoid failures when the database may already exist:
This is better for repeatable setup scripts.
Set charset and collation explicitly
Default server settings differ across environments. Set encoding intentionally for consistent text behavior.
utf8mb4 is the usual safe default for modern applications that need full Unicode support.
Run from non-interactive scripts
For automation, avoid exposing passwords in shell history. Prefer one of these:
mysql_config_editorlogin paths- environment variables provided by secret managers
- CI secret injection
Example using login path:
This keeps credentials out of command history and plain process arguments.
Verify database creation and permissions
Creation can succeed while app user permissions remain missing. Validate both existence and grants.
In stricter environments, grant only required privileges instead of ALL PRIVILEGES.
Integrate into provisioning workflows
For predictable environments, keep SQL provisioning scripts under version control and run them via deployment tooling. Example:
This approach scales better than ad hoc terminal commands and improves auditability.
Common Pitfalls
- Running
CREATE DATABASEwithoutIF NOT EXISTS, causing scripts to fail on reruns. - Relying on server defaults for charset and collation and getting inconsistent text behavior across environments.
- Passing passwords directly on the command line, exposing secrets in shell history and process lists.
- Creating the database but forgetting to grant required privileges to application users.
- Hardcoding root credentials in CI scripts instead of using managed secrets.
Production Readiness Check
Before closing the task, run a short validation loop on representative inputs and one intentional failure case. Confirm that your code path behaves correctly for normal data, empty data, and malformed data. Capture at least one measurable signal such as runtime, memory use, or error rate, then compare it to your baseline so regressions are visible. Keep this check lightweight so it can run in local development and CI without slowing feedback too much. A simple checklist plus one executable smoke test prevents most regressions after refactors and library upgrades.
Summary
Creating a MySQL database from shell command is straightforward, but production-safe usage needs a few disciplined steps. Use mysql -e with idempotent SQL, specify utf8mb4 settings, and manage credentials securely through login paths or secret systems. Always verify both database existence and user grants. When you treat database creation as versioned automation instead of one-off commands, your setups become repeatable, secure, and easier to troubleshoot.
Related reading
- How to create a DB for MongoDB container on start up?
- How to create a LINQ to SQL Transaction?
- How to create a multi-tenant database with shared table structures?
- How to create a MySQL hierarchical recursive query?
- How to Create a nested index in MongoDB?
- How to create an Index in Amazon Redshift
- How to Create and Use Enum in Mongoose
- How to create arguments for a Dapper query dynamically

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.