[Docker]: Connecting PHPMyAdmin to MySQL doesnt wo

2020-02-20 01:27发布

I'm trying to connect a PHPMyAdmin-Container to a MySQL-Container to view the databases.

I have started the MySQL container via $ docker run --name databaseContainer -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

and the PHPMyAdmin-Container via $ docker run --name myadmin -d --link databaseContainer:mysql -p 8080:8080 phpmyadmin/phpmyadmin

When trying to login on PHPMyAdmin, I get: mysqli_real_connect(): php_network_getaddresses: getaddrinfo failed: Name does not resolve

and

mysqli_real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name does not resolve

By the way, I have also started a wordpress container and also linked it to mysql, there it works...

6条回答
Fickle 薄情
2楼-- · 2020-02-20 02:01

I've had the same error installing docker using laradock, then running docker-compose up. Out of the box, default config throws this error when you attempt to log into phpMyAdmin (using current images as of Oct 2018). The error was not present using docker run.

For docker-compose.yml (version 3), one cause is services running on different networks by default. For anyone else having this issue, here is a config that works for mysql and phpmyadmin.

Inside "docker-compose.yml" file, under "services":

### db ###################################################
    db:
      image: mysql:5.7
      container_name: db
      environment:
        - MYSQL_ROOT_PASSWORD=mypass
      networks:
       - backend

### MySQL ################################################
    mysql:
      build:
        context: ./mysql
        args:
          - MYSQL_VERSION=${MYSQL_VERSION}
      environment:
        - MYSQL_DATABASE=${MYSQL_DATABASE}
        - MYSQL_USER=${MYSQL_USER}
        - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
        - TZ=${WORKSPACE_TIMEZONE}
      volumes:
        - ${DATA_PATH_HOST}/mysql:/var/lib/mysql
        - ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
      ports:
        - "${MYSQL_PORT}:3306"
      networks:
        - backend

## phpMyAdmin ###########################################
    phpmyadmin:
      build: ./phpmyadmin
      environment:
        - PMA_ARBITRARY=1
        - PMA_HOST=db
        - MYSQL_USER=${PMA_USER}
        - MYSQL_PASSWORD=${PMA_PASSWORD}
        - MYSQL_ROOT_PASSWORD=${PMA_ROOT_PASSWORD}
      ports:
        - "${PMA_PORT}:80"
      depends_on:
        - db
        - mysql
      networks:
        - frontend
        - backend

Edit the ".env" file as follows:

### MYSQL #################################################

MYSQL_VERSION=5.7
MYSQL_DATABASE=db
MYSQL_USER=root
MYSQL_PASSWORD=mypass
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=mypass
MYSQL_ENTRYPOINT_INITDB=./mysql/docker-entrypoint-initdb.d

### PHP MY ADMIN ##########################################

# Accepted values: mariadb - mysql

PMA_DB_ENGINE=mysql

# Credentials/Port:

PMA_USER=default
PMA_PASSWORD=secret
PMA_ROOT_PASSWORD=secret
PMA_PORT=8080

Add the following line to the "/etc/hosts" file:

127.0.0.1   localhost

Assuming you're also using nginx, and that config is elsewhere in your "docker-compose.yml", you can build and start these services with:

docker-compose up -d mysql nginx db phpmyadmin

Then navigate to localhost:8080 in your browser and login with username "root" and password "mypass" (leave server field blank).

查看更多
Fickle 薄情
3楼-- · 2020-02-20 02:07
docker run --name db -e MYSQL_ROOT_PASSWORD=mypass -d mysql:5.7

docker run -p 80:80 --link db:mysql phpmyadmin/phpmyadmin

Try executing the above command it will work, as phpmyadmin may have hardcoded the in their implementation that the mysql server conatiner name to be db.

If you still want to give your db server a unique name, add PMA_HOST environment variable:

docker run --name myadmin -d --link mysqlserver:mysql -p 8080:80 -e PMA_HOST=mysqlserver phpmyadmin/phpmyadmin

mysqlserver is the name give to your mysql db server.

查看更多
Emotional °昔
4楼-- · 2020-02-20 02:09

In my case, I was using MySQL 8.0 and it turned out that mysql has introduced a new password identification which is not supported by phpmyadmin. See this answer.

查看更多
劫难
5楼-- · 2020-02-20 02:10

Instead of starting them one by one, use docker-compose.

Create a docker-compose.yml file

version: '2'
services:
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: my-secret-pw
    ports:
      # just if you also want to access it directly from you host
      # node neede for phpmyadmin
      - "3306:3306"
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    depends_on:
      - db
    ports:
      - "8080:8080"

Then start it using docker-compose up in the same folder your docker-compose.yml file is located. Access PHPmyadmin using the browser and use 'db' as the hostname of your database, since that is the name of the service in the docker-compose.yml file and therefore can be resolved using dockers internal DNS service to the actual ip of the docker-container. All the links are setup for you automatically.

That's much simpler - docker run overcomplicates things and is not practical for those things - never.

Hint: if docker-compose is not installed on your machine, install it using this official docs https://docs.docker.com/compose/install/ (out of scope)

查看更多
叛逆
6楼-- · 2020-02-20 02:13

The configuration file of phpmyadmin/phpmyadmin (/www/config.inc.php) say by default the host name of database server if 'db' :

$hosts = array('db');

So if your database name container is not 'db', you should add the environment variable PMA_HOST= (or PMA_HOSTS if multi db servers) with the right name (databaseContainer in your case)

查看更多
劳资没心,怎么记你
7楼-- · 2020-02-20 02:26

GIVEN you started the MySQL container as:

$ docker run --name databaseContainer -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

THEN you should actually create phpmyadmin as follows:

$ docker run --name myadmin -d --link mysql:db -p 8080:8080 phpmyadmin/phpmyadmin

It is actually advisable that you include a network. So you would have:

$ docker run --name databaseContainer --network myDockerNetwork -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

AND

$ docker run --name myadmin -d --link mysql:db --network myDockerNetwork -p 8080:8080 phpmyadmin/phpmyadmin

Make sure your network is created first:

$ docker network create myDockerNetwork

查看更多
登录 后发表回答