"Can't connect to local MySQL server" 在 docker-撰写
"Can't connect to local MySQL server" in docker-compose
我有 Laravel 应用程序和 MySQL,我正在尝试使用 docker-compose 在 Docker 中 运行 它。但是当应用程序尝试连接数据库时,它会抛出:
PDOException in Connector.php line 55: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
我的docker-compose.yml:
load_balancer:
image: tutum/haproxy
links:
- web
ports:
- "80:80"
cache:
image: redis
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: Q1w2e3r4t5
MYSQL_DATABASE: regappbase
web:
image: andrewmclagan/nginx-hhvm
links:
- db
- cache
volumes:
- ./:/var/www
environment:
- APP_ENV=local
- DB_DATABASE=regappbase
- DB_PASSWORD=Q1w2e3r4t5
- VIRTUAL_HOST=laravel.local
我的.env:
APP_ENV=production
APP_DEBUG=true
APP_KEY=SomeRandomString
APP_URL=http://laravel.local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=regappbase
DB_USERNAME=root
DB_PASSWORD=Q1w2e3r4t5
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=laravel.local
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
Mysql 到 "localhost" 的连接默认尝试通过本地计算机上存在的套接字进行连接。您的 DB_HOST 应该是链接的容器名称,"db"。
来自MYSQL docs:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:
您可以在.env中将DB_HOST localhost更改为ip。
要接收 ip mysql docker 容器 需要写入命令:
docker exec -it <CONTAINER_ID> ip add show eth0
我有 Laravel 应用程序和 MySQL,我正在尝试使用 docker-compose 在 Docker 中 运行 它。但是当应用程序尝试连接数据库时,它会抛出:
PDOException in Connector.php line 55: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
我的docker-compose.yml:
load_balancer:
image: tutum/haproxy
links:
- web
ports:
- "80:80"
cache:
image: redis
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: Q1w2e3r4t5
MYSQL_DATABASE: regappbase
web:
image: andrewmclagan/nginx-hhvm
links:
- db
- cache
volumes:
- ./:/var/www
environment:
- APP_ENV=local
- DB_DATABASE=regappbase
- DB_PASSWORD=Q1w2e3r4t5
- VIRTUAL_HOST=laravel.local
我的.env:
APP_ENV=production
APP_DEBUG=true
APP_KEY=SomeRandomString
APP_URL=http://laravel.local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=regappbase
DB_USERNAME=root
DB_PASSWORD=Q1w2e3r4t5
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=laravel.local
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
Mysql 到 "localhost" 的连接默认尝试通过本地计算机上存在的套接字进行连接。您的 DB_HOST 应该是链接的容器名称,"db"。
来自MYSQL docs:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:
您可以在.env中将DB_HOST localhost更改为ip。 要接收 ip mysql docker 容器 需要写入命令:
docker exec -it <CONTAINER_ID> ip add show eth0