微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Docker Compose与PHP,MySQL,nginx连接问题

我有连接到MySQL容器的问题.

泊坞窗,compose.yml

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测试脚本:

<?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();
    }
?>

这个脚本响应我:

Connection Failed: sqlSTATE[HY000] [2002] No such file or directory

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

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

解决方法:

更改$servername =“localhost”; to $servername =“MysqL”;.您的MysqL服务不在您的Web服务器容器的localhost上.您应该使用服务的名称

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐