2

Can someone help me to connect my PHP and MySQL I did manage to make it up and running, connect to DB with MySQL Workbench but when I try PDO connect from PHP file it fails for some reason...

docker-compose

web:
    image: nginx:latest
    ports:
        - "80:80"
    volumes:
        - ./:/var/www
        - ./site.conf:/etc/nginx/conf.d/site.conf
    links:
        - php
php:
    image: php:7-fpm
    volumes:
        - ./:/var/www
    links:
        - db

db:
    image: mysql:5.7
    volumes:
     - /var/lib/mysql
    environment:
     - MYSQL_ROOT_PASSWORD=123456
     - MYSQL_DATABASE=database
    ports:
     - "3306:3306"

site.conf

server {
    index index.php index.html;
    server_name lara.test;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

and index.php

<?php

$database = new PDO('mysql:host=localhost;dbname=database', "root", "123456");
echo "Connected to MySQL<br />";

?>

and error message:

Fatal error: Uncaught PDOException: could not find driver in /var/www/index.php:3 Stack trace: #0 /var/www/index.php(3): PDO->__construct('mysql:host=loca...', 'root', '123456') #1 {main} thrown in /var/www/index.php on line 3

what do I miss in order to make this work?

3
  • Dude, the PHP image is bare. The error is self explanatory, the MySQL drivers are missing. Create custom image or use another. Commented Feb 10, 2018 at 23:25
  • I tried put dockerfile with RUN docker-php-ext-install mysqli && \ docker-php-ext-install pdo_mysql, but no luck Commented Feb 10, 2018 at 23:26
  • No wonder, I’m going to sleep already. Hit me tomorrow should you still need help with this. Commented Feb 10, 2018 at 23:32

1 Answer 1

2

Just change

$database = new PDO('mysql:host=localhost;dbname=database', "root", "123456");

to

$database = new PDO('mysql:host=db;dbname=database', "root", "123456");

the name of host must same with the name of database image on docker-compose file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.