gpt4 book ai didi

mysql - mysql容器连接被拒绝

转载 作者:行者123 更新时间:2023-12-02 18:44:05 26 4
gpt4 key购买 nike

我有一个使用mysql服务的docker-compose,当我与mysql工作台建立连接时,它工作正常,但是当我尝试与我的symfony应用程序连接时,它使我拒绝连接错误。

正确设置主机,端口,用户和密码。

这是我写的:

version: "3.1"

services:

mysql:
build:
context: .
dockerfile: docker/mysql/Dockerfile
restart: always
working_dir: /app
volumes:
- .data:/usr/data
environment:
- MYSQL_ROOT_PASSWORD=000000
- MYSQL_DATABASE=my_database
- MYSQL_USER=admin
- MYSQL_PASSWORD=000000
ports:
- "3200:3306"

webserver:
image: nginx:alpine
working_dir: /app
volumes:
- .:/app
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- '8000:80'
depends_on:
- php-fpm

php-fpm:
build: docker/php-fpm
working_dir: /app
volumes:
- .:/app
- ./docker/php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini
links:
- mysql
depends_on:
- mysql

这是mysql的dockerfile:
FROM mysql:5.7


ENV MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
ENV MYSQL_DATABASE=${MYSQL_DATABASE}
ENV MYSQL_USER=${MYSQL_USER}
ENV MYSQL_PASSWORD=${MYSQL_PASSWORD}

COPY ./docker/mysql/my_database.sql /docker-entrypoint-initdb.d/init.sql

EXPOSE 3306

这是我的symfony配置参数:
parameters:
database_driver: pdo_mysql
database_host: mysql
database_port: 3200
database_name: my_database
database_user: admin
database_password: '000000'

这是抛出的错误:
"exception": {
"message": "An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused",
"code": 0,
"previous": {
"message": "SQLSTATE[HY000] [2002] Connection refused",
"code": 2002,
"previous": {
"message": "SQLSTATE[HY000] [2002] Connection refused",
"code": 2002,
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php",
"line": 43,
"args": []
},
{
"namespace": "",
"short_class": "PDO",
"class": "PDO",
"type": "->",
"function": "__construct",
"file": "/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php",
"line": 43,
"args": [
[
"string",
"mysql:host=mysql;port=3200;dbname=my_database;charset=UTF8;"
],
[
"string",
"admin"
],
[
"string",
"000000"
],
[
"array",
[]
]
]
}

可能是什么问题?

最佳答案

当您公开mysql容器的端口3306并将其映射到端口3200时,这意味着外界应该使用端口3200访问它。但是使用同一网络的其他容器必须使用端口3306。

关于mysql - mysql容器连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55244239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com