首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docker与PHP,MySQL,nginx的连接问题

问Docker与PHP,MySQL,nginx的连接问题
EN

Stack Overflow用户
提问于 2016-12-22 16:10:03
回答 0查看 798关注 0票数 6

连接到MySQL容器时出现问题。

docker-compose.yml

代码语言:javascript
复制
version: '2'

services:
    mysql:
        image: mysql:latest
        environment:
            MYSQL_ROOT_PASSWORD: JoeyW#1999
            MYSQL_DATABASE: wiput
            MYSQL_USER: web
            MYSQL_PASSWORD: Web#1234
        volumes:
            - ./mysql:/var/lib/mysql
        networks:
            - code-network
    php:
        image: wiput1999/php:latest
        volumes:
            - ./code:/code
        networks:
            - code-network
    nginx:
        image: nginx:latest
        ports:
            - "80:80"
            - "443:443"
        volumes:
            - ./code:/code
            - ./site.conf:/etc/nginx/conf.d/default.conf
            - /etc/letsencrypt:/etc/letsencrypt
        networks:
            - code-network
networks:
    code-network:
        driver: bridge

PHP测试脚本:

代码语言:javascript
复制
<?php
$servername = "localhost";
$username = "root";
$password = "JoeyW#1999";

try {
    $conn = new PDO("mysql:host=$servername;dbname=wiput", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>

这个脚本对我的回应是:

代码语言:javascript
复制
Connection failed: SQLSTATE[HY000] [2002] No such file or directory

我的代码出了什么问题?因为我觉得这应该没问题

如果有人有更好的解决方案,谢谢你的帮助。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41278492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档