MySQL
database creation
shell commands
SQL
database management

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.

Practice system design

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:

bash
mysql -u root -p -e "CREATE DATABASE app_db;"

After entering your password, MySQL runs the statement and exits.

To avoid failures when the database may already exist:

bash
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS app_db;"

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.

bash
1mysql -u root -p -e "
2CREATE DATABASE IF NOT EXISTS app_db
3  CHARACTER SET utf8mb4
4  COLLATE utf8mb4_unicode_ci;
5"

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_editor login paths
  • environment variables provided by secret managers
  • CI secret injection

Example using login path:

bash
mysql_config_editor set --login-path=localadmin --host=127.0.0.1 --user=root --password
mysql --login-path=localadmin -e "CREATE DATABASE IF NOT EXISTS app_db;"

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.

bash
mysql --login-path=localadmin -e "SHOW DATABASES LIKE 'app_db';"
mysql --login-path=localadmin -e "GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'%'; FLUSH PRIVILEGES;"

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:

bash
mysql --login-path=localadmin < ./sql/001_create_database.sql

This approach scales better than ad hoc terminal commands and improves auditability.

Common Pitfalls

  • Running CREATE DATABASE without IF 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.

text
11. Run happy-path example
22. Run edge-case example
33. Run failure-path example
44. Capture one performance or reliability metric
55. Verify output format and error handling

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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.