[Docker]:将PHPMyAdmin连接到MySQL不起作用 [英] [Docker]: Connecting PHPMyAdmin to MySQL doesnt work
问题描述
我正在尝试将PHPMyAdmin容器连接到MySQL容器以查看数据库.
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
和通过$ 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失败:名称无法解析
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 up
在docker-compose.yml
文件所在的文件夹中启动它.
使用浏览器访问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)
这篇关于[Docker]:将PHPMyAdmin连接到MySQL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!