You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Docker环境下Adminer与phpMyAdmin连接MySQL失败求助

Troubleshooting Your Docker MySQL & Adminer/phpMyAdmin Issues

Hey there! Let's walk through these common Docker/MySQL hiccups one by one—they're totally normal when you're just getting started, so let's get you sorted.

1. Fixing Adminer's SQLSTATE[HY000] [2002] No such file or directory Error

This error pops up because Adminer defaults to using a local Unix socket for connections, but in Docker, your MySQL container runs as a separate service accessible only via TCP. Here's how to fix it:

  • When logging into Adminer: Instead of leaving the "Server" field blank or using localhost, enter the exact name of your MySQL service from your docker-compose.yml (e.g., if your service is named db, type db here).
  • Verify your Docker Compose config: Docker Compose automatically creates a shared network for all services in the file, so as long as you didn't override network settings, Adminer and MySQL should be able to communicate.

Here's a minimal working docker-compose.yml for Adminer + MySQL:

version: '3.8'
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: your_secure_password
      MYSQL_DATABASE: your_default_db
    volumes:
      - mysql_data:/var/lib/mysql # Persist data between container restarts
  adminer:
    image: adminer
    ports:
      - "8080:8080"
    depends_on:
      - db
volumes:
  mysql_data:

2. Resolving phpMyAdmin's "Unknown Authentication Method" Error

This is a classic conflict between MySQL 8.0's default caching_sha2_password authentication plugin and older versions of phpMyAdmin (or underlying PHP) that don't support it. You have two straightforward fixes:

Option 1: Force MySQL to use the legacy authentication plugin

Add a command to your MySQL service in docker-compose.yml to switch to mysql_native_password:

services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: your_secure_password
    command: --default-authentication-plugin=mysql_native_password # Add this line

Option 2: Use the latest phpMyAdmin image

The newest phpmyadmin/phpmyadmin images support caching_sha2_password out of the box. Update your phpMyAdmin service config like this:

phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    ports:
      - "8081:80"
    environment:
      PMA_HOST: db # Match your MySQL service name
    depends_on:
      - db

3. Fixing phpMyAdmin's "Address Resolution Failed" with MySQL 5.7

This almost always comes down to incorrect service naming or timing issues (phpMyAdmin tries to connect before MySQL is fully ready). Here's how to fix it:

  • Explicitly set the PMA_HOST variable: Make sure your phpMyAdmin service points directly to your MySQL service name. For example:
    phpmyadmin:
      image: phpmyadmin/phpmyadmin
      ports:
        - "8081:80"
      environment:
        PMA_HOST: db # Must match your MySQL service name in docker-compose.yml
        PMA_PORT: 3306
      depends_on:
        - db
    
  • Add health checks to wait for MySQL: The depends_on directive only ensures MySQL starts first, not that it's ready to accept connections. Add a health check to your MySQL service to let phpMyAdmin wait until it's fully initialized:
    db:
      image: mysql:5.7
      environment:
        MYSQL_ROOT_PASSWORD: your_secure_password
      volumes:
        - mysql_data:/var/lib/mysql
      healthcheck:
        test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}"]
        interval: 10s
        timeout: 5s
        retries: 5
    
    Then update phpMyAdmin's depends_on to respect the health check:
    phpmyadmin:
      # ... other config ...
      depends_on:
        db:
          condition: service_healthy
    

Quick Final Checks

  • Double-check that all service names in docker-compose.yml match what you're entering in Adminer/phpMyAdmin's server field.
  • Confirm you're using the exact root password set in MYSQL_ROOT_PASSWORD.
  • If you made changes to your config, run docker-compose down -v (to clear old volumes) then docker-compose up -d to start fresh.

内容的提问来源于stack exchange,提问作者user2243747

火山引擎 最新活动