MySQLi not found dockerized php
Asked Answered
S

6

45

I'm trying to dockerize my website. I've got Nginx and PHP up and running and it's working find except I can't connect to a db. When the page is loaded I get the error:

Fatal error: Uncaught Error: Class 'MySQLi' not found in /private/conn.php:8 Stack trace: #0 /public_html/index.php(2): require() #1 {main} thrown in /private/conn.php on line 8

My connection line on line 8 is:

$db = new mysqli("$host", "$user", "$pass", "$db", "$port");

Now this used to work fine on my old set up but since moving to a new one and installing php7.1.5 I can't get it running. Now, I haven't used the mysqlnd so this may be a misunderstanding on my part but the mysqlnd includes the mysqli driver.

Output of phpinfo: config mysqlnd

How can I get this running? Or should I be using a different driver now?

Stratus answered 22/10, 2017 at 21:9 Comment(4)
Can you show the Dockerfile? You need to enable extensions there, but it depends on what is your base image.Fourteenth
I'm using the php:fpm from here https://hub.docker.com/_/php/Stratus
Just out of curiosity...were you using docker for windows?Runstadler
@Runstadler no it was on a Linux boxStratus
F
88

You need to enable the php extension in your Dockerfile:

FROM php:7
RUN docker-php-ext-install mysqli

There is no need to touch php.ini.

Fourteenth answered 23/10, 2017 at 19:8 Comment(4)
Some how it did not work for. This is in my Dockerfile FROM php:7.2-apache RUN docker-php-ext-install mysqli It did not give any error eitherDarlenadarlene
This is the basic thing. He got from docker php official image.Antivenin
Put where Id docker file?!?Weldonwelfare
@Darlenadarlene Need to run docker-compose up --build.Timer
U
15

i had similar issue with
php:7-apache image, which by default will not have mysqli installed
you can verify this inside the container

$ docker exec -it <phpcontainerid> bash  

inside the docker container bash terminal

# docker-php-ext-enable mysqli

if mysqli is not installed which you will come to know from the output of above command

# docker-php-ext-install mysqli

then i commited this change so the same image

$ docker commit -p <phpcontainerid> <new or same image name>
Unprincipled answered 9/1, 2019 at 17:37 Comment(2)
You helped me a lot, Abhishek. I didn't know that I can run docker-php-ext-install from container, then I lost much time running apt commandos trying to install in a traditional way. Thanks.Antivenin
This is how i got it running with php:7.4-apache on Ubuntu. Interestingly, the docker-php-ext-install mysqli command in my Dockerfile "worked" in the output, but without solving the issue when running PHP. Can't understand it.Flurried
S
12

works for me:

inside php container:

docker-php-ext-install mysqli

apachectl restart
  • docker images: mysql, php:7.2.30-apache

https://github.com/docker-library/php/issues/391

Subedit answered 24/4, 2020 at 16:16 Comment(2)
official php-apache docker does not include mysqli, if you really need mysqli, because you won't to change your code, this is the solutionFasta
This is the solution!! Thank you very much!Pliocene
E
5

I had same problem, fixed it by adding below line to dockercompose

RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

And rebuild docker

docker-compose up --build

Elegancy answered 22/12, 2022 at 11:47 Comment(0)
A
0

It looks like you don't have the MysqlI extension enabled in your php.ini. If enabled, you should see a section mysqli in your phpinfo.

More info on enabling the extension can be found here: Fatal error: Class 'MySQLi' not found

Athalee answered 22/10, 2017 at 22:45 Comment(2)
I added extension=php_mysqli.dll to my php.ini file but no change. Does it need to be installed? and if so - what is MySQLnd?Stratus
I think it's installed by default, but you have to restart apache first. php.net/manual/en/mysqli.installation.phpAthalee
W
0

Confirm that your MySQL version should compatible with PHP version for docker

Detail:-

First, ensure that in phpinfo page you should have mysqli installed information if not then..... enter image description here

otherwise, install it and if you think you installed it in docker but not working

then Confirm that your MySQL version should compatible with PHP version for docker like

first I was using this pair for docker

  • php:7.4-apache & mysql:5.7 (but it was not working and showing error Class 'mysqli' not found)

after that I installed

  • php:7.2-apache & mysql:5.7 and it works fine for me
Waken answered 17/1, 2021 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.