[英]Symfony docker container can't connect to Mysql
My stack is quite common, one container for my Symfony app, another one for Mysql. 我的堆栈很普通,一个用于我的Symfony应用程序的容器,另一个用于Mysql的容器。 For some reason, Symfony can't connect to Mysql container with following error :
由于某些原因,Symfony无法通过以下错误连接到Mysql容器:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
致命错误:消息为'SQLSTATE [HY000] [2002]的未捕获异常'PDOException'无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器
A simple php file to test the MySQL connection, on the Symfony container works with this 一个简单的php文件可以测试MySQL连接,在Symfony容器上可以使用
<?php
$dbh = new PDO('mysql:host=database;dbname=mysite', 'mysite', 'password');
Here is my Symfony parameters file: 这是我的Symfony参数文件:
database_driver: pdo_mysql
database_host: database
database_port: 3306
database_name: mysite
database_user: mysite
database_password: password
And here is my docker-compose file : 这是我的docker-compose文件:
site:
build: webapp
ports :
- "80:80"
volumes:
- /home/myuser/dev/mysite:/var/www/html/
links:
- database
database:
image: mysql:5.5
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=mysite
- MYSQL_USER=mysite
- MYSQL_PASSWORD=password
I also tried with a fresh Symfony project without error. 我也尝试了一个新的Symfony项目,没有错误。
Remove the database driver and port to use. 删除要使用的数据库驱动程序和端口。 As you didn't use port forwarding, you don't have to specify the port for MySQL.
由于您没有使用端口转发,因此不必为MySQL指定端口。 Also, there's a mistake into your driver, it's not just
pdo
but pdo_mysql
( documentation ). 另外,驱动程序存在错误,不仅是
pdo
还包括pdo_mysql
( documentation )。
database_host: database
database_name: mysite
database_user: mysite
database_password: password
Also, why do you specify a database_path
? 另外,为什么还要指定一个
database_path
? MySQL doesn't need because you call it by TCP/IP. MySQL不需要,因为您通过TCP / IP调用它。 Old SQLite database ?
旧的SQLite数据库?
You could also checkout your config.yml
file : 您也可以签出
config.yml
文件:
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.