Problem with Docker Pdo installation

Asked

Viewed 701 times

1

I’m having a problem with my containers in Docker, I was trying to raise a Lamp with Docker Compose, with the default image of php:apache and mysql, but when I install PDO this error appears.

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

https://github.com/AurinoJunior/Lamp-Docker-with-pdo

Dockerfile

FROM php:apache

RUN apt-get update && \
    docker-php-ext-install pdo pdo_mysql

COPY ./app /var/www/html/

Docker-Compose.yml

version: "3"
services:
  php-apache:
    build: .
    ports:
     - "8080:80"
    links:
     - dbmysql
    volumes:
     - ./app:/var/www/html

  dbmysql:
    image: mysql
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
    ports:
      - "3306:3306"

I’ll be grateful for any response !!!

Aurino Junior

  • 1

    Dude. First, put the code here and not the link. I looked there... Replace the RUN apt-get update && \ for RUN apt-get update && apt-get install -y \ Test it. Don’t forget to indulge the rest ...&& docker-php-ext-install pdo pdo_mysql

  • Next time I publish the codes, thank you for tidying up the post, I’ll try as you suggested.

1 answer

0


Replace the RUN apt-get update && \ for RUN apt-get update && apt-get install -y \ Add right after && docker-php-ext-install pdo pdo_mysql

Dockerfile

FROM php:apache

RUN apt-get update && apt-get install -y \
    && docker-php-ext-install pdo pdo_mysql

COPY ./app /var/www/html/

At this link(https://mysqlserverteam.com/mysql-8-0-4-new-default-authentication-plugin-caching_sha2_password/) you will be able to read about what is happening with your refused connection.

If you don’t need to use version 8, downgrade to version 5. Then follow the following. Delete your local mysql volume:

$ docker-compose down
$ sudo rm -r mysql/

Use a developer username and password(dev, dev123), forget the root. However, you can use root, or if you need to get the root password that will be generated randomly. Go to the terminal when Mysql is being lifted and search for the generated password. The log line will look like this: dbmysql_1 | GENERATED ROOT PASSWORD: odut5doo2ahngie5shohV8Eingah7us4

Do so in your Docker-Compose.yml, changing the Mysql version(image: mysql:5), setting the database, root password and user creation dev.

version: "3"
services:
  php-apache:
    build: .
    ports:
     - "8081:80"
    links:
     - dbmysql 
    volumes:
     - ./app:/var/www/html

  dbmysql:
    image: mysql:5
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: 1
      MYSQL_DATABASE: database
      MYSQL_USER: dev
      MYSQL_PASSWORD: dev123
    ports:
      - "3306:3306"

Now rebuild in the environment. If your environment is accurate, run the commands as admin, use: sudo

$ docker-compose up --build

In his config.php, use dev user, or use root:

<?php

    $dsn = "mysql:host=dbmysql;dbname=database";
    $dbuser = "dev";
    $dbpass = "dev123";


    try{
        $pdo = new PDO($dsn,$dbuser, $dbpass);
        echo "Conectado!";

    }catch (PDOException $e){
        echo $e->getMessage();
    }
  • Continues with the same SQLSTATE[HY000] error [2002] In such file or directory, it is something related to mysql folder permission.

  • @Aurinojr Do you really need the latest version of Mysql (8.0.11)? There is a problem with Default Authentication Plugin : caching_sha2_password. I will edit My answer with a solution. Then you test.

  • 1

    Thanks Fabiano worked out now, I was some 3 days banging my head trying to solve this kkk vlw same, I can commit this your solution on my github ?

  • @Aurinojr I was going up the changes. But I think you already changed

  • I just changed the port and the name of the database and this all normal thanks again :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.