Docker mysql 无法连接到容器
Docker mysql cant connect to container
我有 docker-compose 文件用于创建 mysql 图像并将端口暴露给 3306,但是当我尝试安装 CMS 时,它给我错误提示它无法连接到数据库。我尝试扫描端口 3306,它显示它已打开,所以 mysql 是 运行。
为什么 docker 个容器中的两个看不到对方?
这是我的 docker-compose 文件:
phpfpm:
restart: always
extends:
file: php-fpm-5.6.yml
service: phpfpm
links:
- db:db
nginx:
restart: always
image: nginx
ports:
- "8000:80"
links:
- phpfpm:phpfpm
volumes:
- ./nginx/vhost.conf:/etc/nginx/conf.d/default.conf
- ./app:/var/www/html
- ./log/nginx:/var/log/nginx
db:
restart: always
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_DATABASE: database
要连接到数据库,请使用您提供的 link/alias 作为主机名。因此,您的 CMS 可以使用 db
作为主机名和端口 3306 连接到 MySQL。
您将无法连接到 localhost 或 127.0.0.1,因为 "localhost" 是 每个容器 内的本地主机,因此,使用 "localhost" 在 phpfpm 容器中将尝试连接到 phpfpm 容器内的 MySQL 数据库,但是那里没有服务器 运行。
Note that you don't have to publish ("3306":"3306"
) the MySQL ports if you only connect to the database from inside the linked containers. Publishing the ports exposes MySQL on the public network interface, which may be "the Internet"
我有 docker-compose 文件用于创建 mysql 图像并将端口暴露给 3306,但是当我尝试安装 CMS 时,它给我错误提示它无法连接到数据库。我尝试扫描端口 3306,它显示它已打开,所以 mysql 是 运行。
为什么 docker 个容器中的两个看不到对方?
这是我的 docker-compose 文件:
phpfpm:
restart: always
extends:
file: php-fpm-5.6.yml
service: phpfpm
links:
- db:db
nginx:
restart: always
image: nginx
ports:
- "8000:80"
links:
- phpfpm:phpfpm
volumes:
- ./nginx/vhost.conf:/etc/nginx/conf.d/default.conf
- ./app:/var/www/html
- ./log/nginx:/var/log/nginx
db:
restart: always
image: mysql
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_DATABASE: database
要连接到数据库,请使用您提供的 link/alias 作为主机名。因此,您的 CMS 可以使用 db
作为主机名和端口 3306 连接到 MySQL。
您将无法连接到 localhost 或 127.0.0.1,因为 "localhost" 是 每个容器 内的本地主机,因此,使用 "localhost" 在 phpfpm 容器中将尝试连接到 phpfpm 容器内的 MySQL 数据库,但是那里没有服务器 运行。
Note that you don't have to publish (
"3306":"3306"
) the MySQL ports if you only connect to the database from inside the linked containers. Publishing the ports exposes MySQL on the public network interface, which may be "the Internet"