Error: SQLSTATE[HY000] [2002] No such file or directory

后端 未结 2 1656
刺人心
刺人心 2021-01-15 14:01

I know this type of question as probably been asked a couple of times, but they all have something to do with laravel, in my case this is vanilla php with no framework.

2条回答
  •  礼貌的吻别
    2021-01-15 14:36

    I finally solved the problem, thanks @Dlk for your help.

    The cause of the problem was because in database.php, I was referring to the host for mysql as localhost instead of the name of the MYSQL service in the docker-compose.yml file. Hence database.php file should look like so :

     PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    try {
         $pdo = new PDO($dsn, $user, $pass, $options);
    } catch (\PDOException $e) {
         throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }
    
    ?>
    

    which correlates with my docker-compose.yml file

    version: '3.7'
    
    services:
      php:
        container_name: nairobi_php
        build:
          context: ./
        volumes:
          - './src:/var/www/html'
        depends_on:
          - mysql
        ports:
          - 80:80
    
      mysql:
        container_name: nairobi_mysql
        image: mysql:8.0
        environment:
          MYSQL_ROOT_PASSWORD: CUeHpADRmZCtnTFGctxp
          MYSQL_DATABASE: nairobi
          MYSQL_USER: admin
          MYSQL_PASSWORD: 123456
        restart: always
        command: --default-authentication-plugin=mysql_native_password
        ports:
          - 3306:3306
    
      adminer:
        image: adminer
        restart: always
        ports:
          - 8080:8080
    
    

提交回复
热议问题