Current Location: Home> Latest Articles> How to Install and Enable MySQL Extension in PHP 8

How to Install and Enable MySQL Extension in PHP 8

M66 2025-07-02

How to Install and Enable MySQL Extension in PHP 8

With the release of PHP 8, many developers may encounter the issue of how to add the MySQL extension in PHP 8. This article will guide you through the process of installing and enabling the MySQL extension in PHP 8.

The MySQL extension allows developers to interact with MySQL databases to perform operations such as queries, inserts, updates, and deletes. Since PHP 7.0, the MySQL extension has been deprecated and fully removed in PHP 7.4. PHP 8 now uses the MySQLi extension, which provides better performance and more features. However, some projects may still rely on the old MySQL extension. In these cases, you need to manually install the MySQL extension.

Steps to Install MySQL Extension

Install MySQL Client Libraries

Before installing the MySQL extension, we need to install the MySQL client libraries. These libraries are essential for communication between the MySQL extension and MySQL servers.

On Ubuntu, you can use the following command to install the MySQL client libraries:

<span class="fun">sudo apt-get install mysql-client</span>

On CentOS, you can use the following command to install them:

<span class="fun">sudo yum install mysql</span>

Install PHP 8 Development Tools

In order to compile and install PHP extensions, you need to install PHP 8 development tools. You can install them using the following commands:

On Ubuntu:

<span class="fun">sudo apt-get install php8.0-dev</span>

On CentOS:

<span class="fun">sudo yum install php8.0-devel</span>

Download MySQL Extension Source Code

Next, you need to download the MySQL extension source code. You can find the latest version of the MySQL extension on PECL (PHP Extension Community Library).

Use the following command to download the source code:

<span class="fun">sudo pecl install mysql</span>

Compile and Install MySQL Extension

Once the source code is downloaded, go to the MySQL extension directory and run the following commands to compile and install the extension:

<span class="fun">cd mysql-</span>
<span class="fun">sudo phpize</span>
<span class="fun">sudo ./configure</span>
<span class="fun">sudo make</span>
<span class="fun">sudo make install</span>

Replace mysql- with the actual version of the MySQL extension you downloaded.

Enable MySQL Extension

After installation, you need to enable the MySQL extension in the PHP configuration file. Open the php.ini file and add the following line:

<span class="fun">extension=mysql.so</span>

Save and close the file.

Restart Web Server

Finally, restart your web server to apply the changes. On Ubuntu, use the following command to restart Apache server:

<span class="fun">sudo service apache2 restart</span>

On CentOS, use the following command to restart:

<span class="fun">sudo systemctl restart httpd</span>

Summary

With these steps, you can successfully install and enable the MySQL extension in PHP 8. While the MySQL extension has been deprecated, some legacy projects may still require it, and manual installation can resolve this issue.

That’s the detailed guide on how to add the MySQL extension in PHP 8. We hope this article has been helpful.