2016-12-22 2 views
5

MySQL 컨테이너에 연결하는 데 문제가 있습니다.Docker PHP, MySQL, nginx 연결 문제로 작성하십시오

고정 표시기 - 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(); 
    } 
?> 

이 스크립트는 나를 reponse는 :

Connection failed: SQLSTATE[HY000] [2002] No such file or directory 

내 코드에 어떤 문제가 있습니까? 왜냐하면 나는 그것이 괜찮을 것이라고 생각하기 때문이다.

누구든지 더 나은 해결책을 가지고 있다면 도움을 주셔서 감사합니다.

답변

5

변경 $servername = "localhost";에서 $servername = "mysql";으로 변경하십시오. mysql 서비스가 웹 서버 컨테이너의 로컬 호스트에 없다. 대신 서비스 이름을 사용해야합니다.

+0

여전히 동일한 답변을 가지고 있습니다. – wiput1999

+0

감사합니다. D – wiput1999