How to Solve Error Message Fatal error: Uncaught Error: Call to undefined function mysql_connect() when running wordpress in Docker Container

Posted on

Introduction

In this article, the focus is for solving an error message which appear upon accessing one of the service exist in the Docker container. Upon accessing the service which is a service running where it need to access the web platform, the page is showing the following error message :

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /var/www/html/wp-includes/wp-db.php:1564 Stack trace: #0 /var/www/html/wp-includes/wp-db.php(592): wpdb->db_connect() #1 /var/www/html/wp-includes/load.php(404): wpdb->__construct('wordpress', 'wordpress', 'wordpress', 'localhost') #2 /var/www/html/wp-settings.php(106): require_wp_db() #3 /var/www/html/wp-config.php(130): require_once('/var/www/html/w...') #4 /var/www/html/wp-load.php(37): require_once('/var/www/html/w...') #5 /var/www/html/wp-blog-header.php(13): require_once('/var/www/html/w...') #6 /var/www/html/index.php(17): require('/var/www/html/w...') #7 {main} thrown in /var/www/html/wp-includes/wp-db.php on line 1564

The above error message is showing that there is a missing library where it hold or it provide the function of ‘mysql_connect()’. Before going in further, the following is the ‘docker-compose.yml’ file where it is the base reference for building the Docker container providing the above web service. That content of ‘docker-compose.yml’ triggering the error exist below :

version: "3"
services:
  www:
     image: php:7.0-apache       
     ports:             
       - "80:80"         
     volumes:             
       - "./html:/var/www/html/"             
       - "./apache2:/var/log/apache2"         
     links:             
       - db         
     networks:             
       - default     
  db:         
    image: mysql         
    ports:             
      - "3306:3306"          
    environment:             
      MYSQL_DATABASE: wordpress             
      MYSQL_USER: wordpress             
      MYSQL_PASSWORD: wordpress             
      MYSQL_ROOT_PASSWORD: test         
    volumes:             
      - ./dump:/docker-entrypoint-initdb.d    
      - "./mysql:/var/lib/mysql"         
    networks:             
      - default     phpmyadmin:         
    image: phpmyadmin/phpmyadmin         
    links:             
      - db:db         
    ports:             
      - 9000:80         
    environment:             
      MYSQL_USER: user             
      MYSQL_PASSWORD: test             
      MYSQL_ROOT_PASSWORD: test 
    volumes:     
      persistent:

How to Solve Error Message Fatal error: Uncaught Error: Call to undefined function mysql_connect()

In order to solve the problem, there is a specific step which is becoming a requirement in order to do it. The image for the ‘www’ must be going through some sort of modification. It is obvious since the image of the Apache Webserver in general has a default library settings and configuration. So, in order to do that, there must be a Dockerfile file which is describing the image for the ‘www’ service. Below is the actual content of the Dockerfile :

FROM php:7.2-apache
# Install stuff
RUN apt-get update && apt-get upgrade -y
RUN apt-get install sudo unzip wget -y
RUN docker-php-ext-install mysqli
# Configure stuff
RUN a2enmod rewrite
RUN a2enmod ssl
RUN service apache2 restart
EXPOSE 80

In the above Dockerfile content, there is an important line which is actually the solution for solving the problem. That line is exist as follows :

RUN docker-php-ext-install mysqli

In that line, there is a specific command for installing a library with the name of ‘mysqli’. Furthermore, since the image in this context is no longer the same default image of ‘php:7-2-apache, the ‘docker-compose.yml’ file can no longer use the ‘image’ definition for the service ‘www’. So, there is a slight revision on the content of the ‘docker-compose.yml’. Below is the revision of the content :

version: "3"
services:
  www:
     build: .       
     ports:             
       - "80:80"         
     volumes:             
       - "./html:/var/www/html/"             
       - "./apache2:/var/log/apache2"         
     links:             
       - db         
     networks:             
       - default

Do not forget to place the Dockerfile in the same directory in the same level with the ‘docker-compose.yml’ file. After that, just execute the command for building the Docker container once more. If there is no more error appear, the page will no longer display the previous error.

Leave a Reply