Docker Compose for MySQL and phpMyAdmin on localhost
Run MySQL and the phpMyAdmin web page on your computer with one docker compose command.
This file starts a MySQL database and the phpMyAdmin web page together. phpMyAdmin is already set to talk to the database, so you can open it in the browser and log in. Set your passwords below and run docker compose up.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
services:
db:
image: mysql:8.4
container_name: mysql-db
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: Change-root-456
MYSQL_DATABASE: appdb
MYSQL_USER: app
MYSQL_PASSWORD: Change-me-123
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
restart: unless-stopped
environment:
PMA_HOST: db
ports:
- "8080:80"
depends_on:
- db
volumes:
mysql-data:How this setup works
Step-by-step setup
- Save the file above as docker-compose.yml.
- Start both: docker compose up -d. The first start takes a minute while MySQL sets itself up.
- Open http://localhost:8080.
- Log in with user app and your user password, or root and the root password.
- To stop: docker compose down. Your data stays in the volume. Add -v to delete it too.
What the important lines mean
- PMA_HOST: db tells phpMyAdmin to connect to the service called db, not to localhost.
- MYSQL_DATABASE, MYSQL_USER and MYSQL_PASSWORD create your database and user on the first start only.
- mysql-data is a volume, so the data survives restarts.
- Your apps on this computer connect to MySQL at 127.0.0.1 port 3306.
Common errors and how to fix them
- Access denied for user
- The user and password must match the values above. The passwords are only set on the first start. If you changed them later, run docker compose down -v to start with a new volume.
- phpMyAdmin: #2002 Connection refused
- MySQL is still starting. Wait a minute and reload. If it persists, read docker compose logs db.
- port is already allocated
- Another program uses that port, often an installed MySQL on 3306. Change the left number of the port line.
- Your app cannot connect to the database
- From your computer use 127.0.0.1 and the port above. From another container in the same file use the host db and port 3306.
Frequently asked questions
- Is it safe to leave this running?
- It is meant for development on your own computer. Do not publish these ports to the internet, and use strong passwords if anyone else can reach your machine.
- How do I import a database in phpMyAdmin?
- Open the database, choose Import and pick a .sql file. Large files may exceed the upload limit, so import them with the mysql command instead.
- Where is my data stored?
- In the Docker volume mysql-data. It stays until you remove the volume.
Free tools that help
- Password generatorCreate a random password with the length and characters you choose. It runs in your browser, uses secure randomness and shows how strong it is.
- Common ports listSearch a list of common TCP and UDP port numbers: SSH 22, HTTP 80, HTTPS 443, MySQL 3306, PostgreSQL 5432 and more, with dev server ports.
Fix a problem with this setup
- phpMyAdmin #2002 cannot connectFix phpMyAdmin error #2002, "The server is not responding": start MySQL, use 127.0.0.1 and the right port, or fix the socket and Docker host.
- MySQL access deniedFix error 1045. Default passwords for XAMPP and MAMP, and how to log in on Linux where root uses socket login.
- Docker port already allocatedFix "port is already allocated" in Docker and Docker Compose: find the container or program that holds the port and free it.
- localhost/phpmyadminphpMyAdmin is the web tool for MySQL and MariaDB. How to open it in XAMPP, WAMP and MAMP, and fix 404, #1045 and #2002 errors.
More container configs
- Docker Compose for PostgreSQL and pgAdminRun PostgreSQL and the pgAdmin web page on your computer with one docker compose command.
- docker-compose for MongoDBRun a local MongoDB database with a password that keeps its data.
- docker-compose for PostgreSQLRun a local PostgreSQL database that keeps its data.
- docker-compose for MySQLRun a local MySQL database in Docker with your own user, password and port, and keep the data between restarts.
- docker-compose for RedisRun Redis in Docker with a password, saved data and a memory limit.
- docker-compose for WordPressRun WordPress and its database locally in Docker with one command and keep your site between restarts.