Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marcosdipaolo's avatar

Cannot connect to database with docker and pdo_mysql in laravel

Cannot connect to database with docker and pdo_mysql in laravel

The errors are: -SQLSTATE[HY000] [2002] No handler for this scheme (that's with 'localhost' as DB_HOST in .env) -SQLSTATE[HY000] [2002] Connection refused (that's with '127.0.0.1' as DB_HOST in .env)

previous errors were: -could not find driver, that was before I coded in Dockerfile: RUN docker-php-ext-install pdo_mysql RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini" and uncomment extension=pdo_mysql at php.ini at $PHP_INI_DIR which is /usr/local/etc

Honestly I don't know if that was entirely necesary but error was gone, insted now at php artisan tinker inside docker container i get PHP Warning: Module 'pdo_mysql' already loaded in Unknown on line 0

If i don't do that i get the error even if i rename php.development.ini to php.ini and uncomment the line extension=pdo_mysql (phpinfo says that it is not loading the file anyways??)

thanks in advance

Dockerfile

FROM php:7.2-apache

# RUN apt-get update && apt-get install php-mysql
RUN apt-get update
RUN apt-get install nano
#RUN docker-php-ext-install pdo_mysql
#RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

RUN a2enmod rewrite
RUN service apache2 restart

ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

docker-compose.yml

version: '3.1'

services:
    web:
        build: .
        ports:
            - "8080:80"
        links:
            - mysql
        depends_on:
            - mysql
        volumes:
            - ./:/var/www/html
    mysql:
        image: mysql:5.7.25
        ports:
            - "3306:3306"
        environment:
            MYSQL_ROOT_PASSWORD: my_password

.env file

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=dashboard
DB_USERNAME=root
DB_PASSWORD=my_password
0 likes
12 replies
hajrovica's avatar

Module 'pdo_mysql' already loaded in Unknown on line 0 - that error comes for docker container just informing you that pdo is already loaded before uncomented pdo in php.ini though that should not hinder your ability to connect

Can you use some GUI client to connect to Mysql (i.e. HeidiSQL on Win) fire it up and try to connect to loclhost:3306 or127.0.0.1:3306 and see if you are successful ?

marcosdipaolo's avatar

Hi hajrovica Yes, of course i can connect with Seque Pro, mysql docker container is working properly. Also accesing it from the command line works.

hajrovica's avatar

ok ... then there should not be an issue when connecting with app though if i understood correctly you are getting error: SQLSTATE[HY000] [2002] Connection refused

cross reference your login data from sequelpro and .env file ?

marcosdipaolo's avatar

Already checked..., many times. Something to point out, i have full access to the database from outside the container, i can migrate, create users from the tinker, the problem is from inside the container. Dus the problem is how the docker system is built (i guess).

hajrovica's avatar

yes can you check mysql users or even better create new user with full privileges and try that one ? maybe mysql users got messed - bcs there is no logic that with root you have full access outside of container but not inside,

and also in docker case i think this (using sequel pro to connect) does not count as outside connection

marcosdipaolo's avatar

I don't know if i unsertand you correctly, but this is what is happening outside and inside:

Outside:

➜  backoffice git:(master) ✗ php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.15 — cli) by Justin Hileman
>>> $ur = app(MDiPaolo\Repositories\UserRepository::class)
=> App\Infrastructure\Repositories\Doctrine\UserDoctrineRepository {#3159}
>>> $u = new MDiPaolo\Entities\User
=> MDiPaolo\Entities\User {#3295}
>>> $u->setEmail('[email protected]')
=> MDiPaolo\Entities\User {#3295}
>>> $u->setPassword(password_hash('1234', PASSWORD_BCRYPT))
=> null
>>> $ur->save($u)
=> null
>>>

Inside:

➜  backoffice git:(master) ✗ docker exec -it backoffice_web_1 bash
root@042969f0229c:/var/www/html# php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.17 — cli) by Justin Hileman
>>> $ur = app(MDiPaolo\Repositories\UserRepository::class)
Doctrine/DBAL/Exception/ConnectionException with message 'An exception occurred in driver: SQLSTATE[HY000] [2002] Connection refused'
hajrovica's avatar

@marcosdipaolo

can you try in your .env file DB_HOST=mysql ? and then try docker exec (from inside) command ?

1 like
iwo's avatar

Great! It worked! I've been searching for this more 2-3 hours!

marcosdipaolo's avatar

OMG! that did it!!! Thanks a lot!, but ofc now i cannot access from the outside, is there a way of being able to access the DB from both environments?

Edit: found it! adding

127.0.0.1   mysql

to hosts file!!, thanks a lot!!

1 like
hajrovica's avatar

hmm not directly - there is a way to go around by making custom command in your site

containing something like

COMPOSE="docker-compose -f docker-compose.$COMPOSE_FILE.yml"

if [ $# -gt 0 ];then

    if [ "" == "art" ];then
      shift 1
      $COMPOSE run --rm $TTY \
        -w /var/www/html \
        app \
        php artisan "$@"

this is extensively explained in shipping docker course but a gist is that you do docker compose run command by calling ./develop art route:list command will see that first arg is art then strip it and run php artisan with rroute:list (though in my example i am using image called app for you it would be web) I hope this kinda rushed info makes sense ?

Please or to participate in this conversation.