17

When I run docker-compose up and do some composer commands, I get error

In Connection.php line 664: could not find driver (SQL: select id, name from users In Connector.php line 68: could not find driver ...

Why cant Laravel connect to mysql? I can do mysql -h db in docker-compose exec web bash and it works.

My setup

docker-compose.yml

version: '3'

services:
  web:
    build: ./webserver
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - //docker/dockertest/webserver/app:/var/www/vhosts/app
    links:
      - db
    command:
       - /usr/local/bin/apache2_install_composer_dependencies.sh

  db:
    image: mysql:8.0
    container_name: db
    ports:
      - "3306:3306"
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_DATABASE: myDb
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test
    volumes:
      - //docker/dockertest/install/db_dump:/docker-entrypoint-initdb.d
      - persistent:/var/lib/mysql
    networks:
      - default

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - db:db
    ports:
      - 8000:80
    environment:
      MYSQL_USER: user
      MYSQL_PASSWORD: test
      MYSQL_ROOT_PASSWORD: test



volumes:
  persistent:

Laravel .env (I reference these values in config/database.php)

...
DB_CONNECTION=mysql
#host points to Docker container
DB_HOST=db
DB_PORT=3306
DB_DATABASE=myDb
DB_USERNAME=user
DB_PASSWORD=test
....

webserver/Dockerfile

FROM php:7.2.19-apache-stretch

# Configure Apache server
COPY config_apache/sites-available /etc/apache2/sites-available

# Create symlink in sites-enabled
WORKDIR /etc/apache2/sites-enabled
RUN ln -s /etc/apache2/sites-available/app.conf app.conf

RUN mkdir -p /var/www/vhosts/app/logs

COPY /build_files/install_composer_dependencies.sh /usr/local/bin/apache2_install_composer_dependencies.sh

RUN apt-get update -y && apt-get install -y  curl nano libapache2-mod-geoip git zip unzip mysql-client

# Install Composer
RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \
&& curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig \
# Verify installer
&& php -r "if (hash('SHA384', file_get_contents('/tmp/composer-setup.php')) !== trim(file_get_contents('/tmp/composer-setup.sig'))) { unlink('/tmp/composer-setup.php'); echo 'Invalid installer' . PHP_EOL; exit(1); }" \
&& php /tmp/composer-setup.php --no-ansi --install-dir=/usr/local/bin --filename=composer --snapshot \
&& rm -f /tmp/composer-setup.*

RUN a2enmod rewrite
RUN a2enmod geoip
RUN service apache2 restart
Zezi Reeds
  • 1,286
  • 1
  • 16
  • 29
  • Changing `DB_CONNECTION=mysql` -> `DB_CONNECTION=mysqli` should most likely help.. As mysql is deprecated – Raymond Nijland Jun 25 '19 at 17:58
  • Are you using Windows or Linux? You may need to make sure that PDO is installed and enabled. – aynber Jun 25 '19 at 18:00
  • @RaymondNijland That setting in Laravel is for the database type, not the driver itself. https://laravel.com/docs/5.8/database – aynber Jun 25 '19 at 18:00
  • 1
    Possible duplicates: https://stackoverflow.com/questions/50220506/php-7-2-pdo-could-not-find-driver https://stackoverflow.com/questions/51982770/could-not-find-driver-in-laravel – aynber Jun 25 '19 at 18:02
  • *"That setting in Laravel is for the database type, not the driver itself"* to mine defense @aynber i didn't use laraval in ages so i pretty much made a guess there.. – Raymond Nijland Jun 25 '19 at 18:05
  • 1
    @RaymondNijland No worries. I had to double-check the config myself. – aynber Jun 25 '19 at 18:06
  • @aynber PDO is enabled (I checked with `extension_loaded('pdo')`) – Zezi Reeds Jun 25 '19 at 18:09
  • 1
    See if https://stackoverflow.com/questions/41955914/docker-could-not-find-any-mysql-database-drivers-mysqli-or-pdo-required solves the problem. – Nigel Ren Jun 25 '19 at 18:39
  • @NigelRen Do you have an idea why `docker-php-ext-install mysql` outputs `error: /usr/src/php/ext/mysql does not exist`? I guess just creating that directory wont fix it? – Zezi Reeds Jun 25 '19 at 18:47
  • 2
    Have you tried with all of the options `docker-php-ext-install mysql mysqli pdo pdo_mysql`? – Nigel Ren Jun 25 '19 at 18:53
  • @NigelRen Look if i do `RUN docker-php-ext-install mysql mysqli pdo pdo_mysql` then output while `docker-compose build` is ` ---> Running in 1c043aebb64d error: /usr/src/php/ext/mysql does not exist` – Zezi Reeds Jun 25 '19 at 20:57
  • Edit: I removed 'mysql' from that line and the build succeeded. Now when doing docker-compose up I get "SQLSTATE[HY000] [2002] Connection timed out" on that query – Zezi Reeds Jun 25 '19 at 21:11

2 Answers2

32

Adding RUN docker-php-ext-install mysqli pdo pdo_mysql (without mysql) to Dockerfile as @NigelRen suggested resolved this error.

Zezi Reeds
  • 1,286
  • 1
  • 16
  • 29
6

try to install php-mysql with following command sudo apt-get install php-mysql and restart your server

Levon Babayan
  • 266
  • 2
  • 18