[Docker]:将 PHPMyAdmin 连接到 MySQL 不起作用

2022-01-05 00:00:00 docker mysql phpmyadmin

我正在尝试将 PHPMyAdmin-Container 连接到 MySQL-Container 以查看数据库.

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

我已经通过 $ docker run --name databaseContainer -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

和 PHPMyAdmin-Container 通过 $ docker run --name myadmin -d --link databaseContainer:mysql -p 8080:8080 phpmyadmin/phpmyadmin

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

尝试登录 PHPMyAdmin 时,我得到:mysqli_real_connect(): php_network_getaddresses: getaddrinfo 失败: 名称无法解析

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

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

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

顺便说一句,我还启动了一个 wordpress 容器并将其链接到 mysql,它可以工作...

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

推荐答案

不要一一开始,而是使用 docker-compose.

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

创建一个 docker-compose.yml 文件

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"

然后在 docker-compose.yml 文件所在的同一文件夹中使用 docker-compose up 启动它.使用浏览器访问 PHPmyadmin 并使用db"作为数据库的主机名,因为这是 docker-compose.yml 文件中的服务名称,因此可以使用 dockers 内部 DNS 服务进行解析到 docker-container 的实际 ip.所有链接都是自动为您设置的.

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.

这要简单得多 - docker run 使事情变得过于复杂并且对这些事情不实用 - 从不.

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

提示:如果您的机器上未安装 docker-compose,请使用此官方文档 https 进行安装://docs.docker.com/compose/install/(超出范围)

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)

相关文章