How do I configure php to enable pdo and include mysqli on CentOS?

How do I configure php to enable pdo and include mysqli on CentOS?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoupling Web and CLI: Enabling PDO and MySQLi on CentOS

You encounter a scenario where your PHP application works perfectly via HTTP, but when you attempt to run a script from the command line or use php directly in the shell, you get a fatal error regarding missing extensions. Specifically, pdo_mysql.so, pdo_sqlite.so, and mysqli are unavailable in CLI mode, even though they appear loaded for the web server.

This discrepancy is almost always caused by a mismatch between your web server's PHP configuration and your Command Line Interface (CLI) build. In your specific case on CentOS (RHEL 6), you are running PHP 5.3.3 with extensions enabled in .ini files but missing from the binary compilation flags.

The Root Cause: Compilation Flags vs. Runtime Configuration

The source of the problem lies in the --disable-pdo and --without-mysql flags present in your configure command. These flags tell the PHP build system to exclude PDO and MySQL support entirely from the executable compiled for CLI usage.

When you see .ini files like /etc/php.d/pdo_mysql.ini or extension=pdo.so in your /etc/php.ini, you are configuring a runtime environment that simply does not exist at the binary level. PHP cannot load an extension if the corresponding library was not compiled into the executable or linked against it during the build process.

The presence of these extensions in your .ini files suggests they might be intended for the Apache module (libphp5.so) rather than the CLI binary, or perhaps a previous installation left configuration files behind that were never cleaned up after a rebuild. Since web servers (like Apache via apxs2) often load shared object files dynamically, you can technically enable extensions at runtime for the web context. However, the CLI binary is static; it cannot dynamically load modules that were excluded during compilation.

Resolving the Configuration Mismatch

To fix this on CentOS without reinstalling PHP from source immediately, you must determine if a separate CLI package exists or if the extensions need to be re-enabled via a different build method.

On RHEL/CentOS 6, it is highly probable that you installed PHP using yum or rpm. The standard distribution packages usually provide both an Apache module and a CLI binary, but they are distinct binaries with potentially different compilation flags.

Step 1: Inspect Available Packages

Run the following command to see if separate CLI packages exist that might already include these extensions:

rpm -qa | grep php
yum list installed php-cli* --showduplicates

If you have a standard php-cli package alongside your current setup, it is possible your web server is running a custom-compiled version while the system has a pre-compiled CLI version. However, mixing custom-built PHP with system packages is generally discouraged due to dependency conflicts and library mismatches.

Step 2: Rebuild from Source with Correct Flags

If you are maintaining a custom build (as indicated by your specific configure command), you must re-run the configuration step without the disabling flags.

Edit your configure command to remove or comment out these lines:

  • --disable-pdo
  • --without-mysql
  • --without-sqlite (if you need SQLite support)

Additionally, ensure that extension=... lines are removed from the CLI-specific ini file if you intend to move them elsewhere, or keep them if you are using a shared object directory. The critical step is ensuring the binary itself knows these modules exist.

Risks and Trade-offs

Enabling these extensions introduces specific considerations regarding version compatibility and security:

  1. Library Dependencies: PDO requires libpdo libraries (often part of MySQL or SQLite development packages). If you are using PHP 5.3.3, ensure your underlying database client libraries (MySQL Connector/C) are compatible with this old PHP version. Modern MySQL drivers may not support such an old PHP release.
  2. Security: PHP 5.3.3 is end-of-life and contains known vulnerabilities. Enabling new extensions on an unsupported version does not mitigate existing security flaws. If possible, upgrade to a supported PHP version (e.g., PHP 7.4 or 8.x) where PDO and MySQLi are included by default in the standard builds.
  3. Performance: Loading shared libraries (extension=pdo.so) adds slight overhead compared to statically linked extensions, though this is rarely a bottleneck unless you have massive numbers of concurrent CLI requests.

Best Practices for Future Builds

When configuring PHP from scratch on CentOS or RHEL, avoid using --disable-* flags for essential database drivers unless you have a specific reason not to (such as avoiding unnecessary library dependencies). Instead, rely on the official EPEL repository or standard yum packages which handle these configurations reliably.

If you must stick with your current custom build, ensure that your configure script explicitly includes:

  • --enable-pdo
  • --with-pdo-mysql=... (pointing to your MySQL client)
  • --with-mysqli

Once recompiled and installed, verify the fix by running:

php -m | grep -i "pdo\|mysqli"

If these modules appear in the output, your CLI configuration now matches your web requirements. Remember that the .ini files you listed are parsed correctly only if the binary supports them; they act as a bridge, not the foundation.

Our network