mysql: Usage, Options & Common CI Errors
mysql runs SQL against a MySQL or MariaDB server from the shell.
mysql seeds databases and runs migrations against a MySQL service container in CI. The two recurring failures are access-denied from a misjoined password flag and connecting before the server is ready.
What it does
mysql connects to a MySQL/MariaDB server and runs SQL interactively, from -e, or from a piped/redirected file. In CI it targets a service container, so host, port, user, and password are supplied on the command line or via environment variables.
Common usage
mysql -h 127.0.0.1 -u root -psecret -e 'SELECT 1' # no space after -p
MYSQL_PWD=secret mysql -h 127.0.0.1 -u root app < schema.sql
mysql -h 127.0.0.1 -P 3306 -u root -psecret app
mysql --protocol=TCP -h 127.0.0.1 -u root -psecret -e 'SHOW DATABASES'Options
| Flag | What it does |
|---|---|
| -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, no space) |
| -e "SQL" | Run one statement and exit |
| --protocol=TCP | Force TCP instead of a local socket |
Common errors in CI
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES/NO) - wrong credentials, or the password had a space after -p (the value must be attached: -psecret, or use MYSQL_PWD). "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'" means it tried the Unix socket; pass -h 127.0.0.1 (not localhost) to force TCP to the service container. "ERROR 2003 ... Can't connect ... (111)" is connection refused - the server is not up yet; wait with mysqladmin ping.