Skip to content
Latchkey

mysql Command Reference: Flags, Usage & CI Examples

mysql runs SQL against a MySQL or MariaDB server from the shell.

mysql connects to a MySQL/MariaDB server and runs SQL interactively, from -e, or from a piped file. In CI it seeds databases and runs migrations against a service container.

Common flags and usage

  • -h <host>: server host (use 127.0.0.1 to force TCP)
  • -P <port>: server port (uppercase; default 3306)
  • -u <user>: connect as this user
  • -p[password]: password attached with no space (or set MYSQL_PWD)
  • -e "SQL": run one statement and exit
  • -N -B: header-free, tab-separated output for capture

Example

shell
MYSQL_PWD=${MYSQL_PWD} mysql -h 127.0.0.1 -P 3306 -u root app < schema.sql
MYSQL_PWD=${MYSQL_PWD} mysql -h 127.0.0.1 -u root -N -B -e 'SELECT 1' app

In CI

Use -h 127.0.0.1 (not localhost) to force TCP to the service container instead of a missing Unix socket. Supply the password via MYSQL_PWD or attached to -p with no space; a space after -p is a common "Access denied" cause.

Key takeaways

  • mysql runs SQL interactively, via -e, or from a piped file.
  • Force TCP with -h 127.0.0.1 to reach a CI service container.
  • Pass the password via MYSQL_PWD or attached to -p, never spaced.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →