# MySQL + phpMyAdmin

> MySQL database server with phpMyAdmin web interface for visual database management and administration.

- Category: Database Stacks
- Difficulty: beginner
- Components: mysql, phpmyadmin
- Tags: mysql, sql, admin-ui, database, phpmyadmin
- Source: https://docker.recipes/databases/mysql-phpmyadmin

## Overview

MySQL is the world's most widely deployed open-source relational database management system, powering millions of web applications from small WordPress sites to massive enterprise platforms. Originally developed by MySQL AB in 1995, it has become the backbone of the modern web stack, particularly in LAMP (Linux, Apache, MySQL, PHP) environments. Its reputation for speed, reliability, and ease of use has made it the go-to choice for developers who need robust data storage without the complexity of enterprise database systems.

This stack combines MySQL 8.0 with phpMyAdmin, creating a complete database solution with visual management capabilities. phpMyAdmin provides a web-based interface that transforms MySQL's command-line administration into an intuitive graphical experience, allowing users to create databases, manage tables, execute queries, and handle user permissions through a browser. The two containers communicate over a dedicated Docker network, with phpMyAdmin automatically connecting to the MySQL instance without additional configuration.

This combination is perfect for web developers, small to medium businesses, and anyone who needs MySQL database functionality with easy visual management. Whether you're running WordPress sites, developing PHP applications, or need a development database with administrative tools, this stack provides immediate productivity. The setup is particularly valuable for teams where not all members are comfortable with command-line database administration, as phpMyAdmin makes complex database operations accessible through its web interface.

## Features

- MySQL 8.0 with InnoDB storage engine providing ACID compliance and foreign key constraints
- phpMyAdmin web interface with full MySQL administration, SQL editor, and query bookmarks
- JSON data type support with native indexing and path-based queries in MySQL 8.0
- Import/export functionality supporting CSV, SQL, XML, and other database formats
- Visual database designer in phpMyAdmin for creating relationships and ER diagrams
- MySQL Group Replication support for high availability configurations
- Full-text search capabilities with natural language and boolean mode searching
- User privilege management through phpMyAdmin's intuitive interface

## Use cases

- WordPress and PHP application development with visual database management
- Small to medium business applications requiring relational data storage
- Web hosting environments where clients need database access without shell access
- Development and testing environments for MySQL-based applications
- Educational settings for teaching SQL and database administration concepts
- Legacy application migration where phpMyAdmin provides familiar MySQL tooling
- Multi-tenant applications where different teams need database access through web interface

## Prerequisites

- Minimum 512MB RAM available (256MB for MySQL + 128MB for phpMyAdmin)
- Docker and Docker Compose installed on the host system
- Ports 3306 and 8080 available on the host machine
- Basic understanding of SQL and relational database concepts
- Environment file (.env) with MySQL credentials configured

## docker-compose.yml

```yaml
services:
  mysql:
    image: mysql:8.0
    container_name: mysql
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
    ports:
      - "3306:3306"
    networks:
      - mysql-network

  phpmyadmin:
    image: phpmyadmin:latest
    container_name: phpmyadmin
    restart: unless-stopped
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
    ports:
      - "8080:80"
    depends_on:
      - mysql
    networks:
      - mysql-network

volumes:
  mysql_data:

networks:
  mysql-network:
    driver: bridge
```

## .env

```bash
MYSQL_ROOT_PASSWORD=rootpassword
MYSQL_DATABASE=myapp
MYSQL_USER=appuser
MYSQL_PASSWORD=changeme
```

## Usage notes

- Docs: https://dev.mysql.com/doc/ | phpMyAdmin: https://www.phpmyadmin.net/docs/
- Access phpMyAdmin at http://localhost:8080
- Login with root/MYSQL_ROOT_PASSWORD or MYSQL_USER/MYSQL_PASSWORD
- Backup: docker exec mysql mysqldump -u root -p --all-databases > backup.sql
- For production, set MYSQL_ROOT_HOST to restrict root access
- Data persisted in mysql_data volume - backup /var/lib/mysql regularly

## Troubleshooting

- Connection refused on port 3306: Check if MySQL container started successfully and MYSQL_ROOT_PASSWORD is set in environment variables
- phpMyAdmin shows 'mysqli_real_connect(): (HY000/2002)': Ensure both containers are on the same network and MySQL container is fully initialized before phpMyAdmin starts
- Access denied for user 'root'@'%': Verify MYSQL_ROOT_PASSWORD matches between containers, or use MYSQL_USER/MYSQL_PASSWORD for non-root access
- Import file size exceeded in phpMyAdmin: Increase PHP upload limits by mounting custom PHP configuration or use mysql command line for large imports
- MySQL data lost after container restart: Confirm mysql_data volume is properly mounted and not using anonymous volumes

