如何修复CentOS 6.5中yum安装php-mysqli及安装后的报错问题
Hey there, let’s work through these two php-mysqli headaches you’re facing on CentOS 6.5— I’ve debugged similar problems plenty of times, so let’s break it down step by step.
Problem 1: Errors when installing php-mysqli via YUM
CentOS 6 is past its end-of-life, so most default repos are no longer active, which is the #1 cause of YUM install failures here. Let’s cover the most common fixes:
Fix outdated YUM repos
Since official CentOS 6 repos are offline, you’ll need to switch to archived repos. Here’s how:- Back up your existing repo config:
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak - Create a new
CentOS-Base.repofile with archived sources. Open it in an editor:
Paste this content (uses an archived CentOS 6 repo):vi /etc/yum.repos.d/CentOS-Base.repo[base]
name=CentOS-6 - Base - Archived
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/6.10/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6[updates]
name=CentOS-6 - Updates - Archived
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/6.10/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6[extras]
name=CentOS-6 - Extras - Archived
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos-vault/6.10/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-6 - Clean and refresh YUM cache:
yum clean all && yum makecache
Now try installing php-mysqli again with
yum install php-mysqli.- Back up your existing repo config:
Fix PHP version mismatches
CentOS 6 ships with PHP 5.3 by default, but if you’ve installed a newer PHP version (like 5.4/5.5 from third-party repos), the package name might differ. First check your PHP version:php -vThen install the matching mysqli package— for example, if you’re on PHP 5.4, use
yum install php54-mysqli.Fix dependency conflicts
If YUM throws dependency errors, try skipping broken packages temporarily (only do this if you’re sure the broken dependencies aren’t critical):yum install --skip-broken php-mysqliOr resolve the missing dependencies first by installing the required packages manually.
Problem 2: Abnormal behavior even after installing php-mysqli
Installing the package doesn’t always mean the extension is working correctly. Let’s troubleshoot the most common culprits:
Check if mysqli is enabled in PHP
First verify if the extension is loaded:php -m | grep mysqliIf there’s no output, it’s not enabled. Here’s how to fix that:
- Find your active php.ini file:
php --ini | grep "Loaded Configuration File" - Open the file in an editor and add/uncomment this line:
extension=mysqli.so - Restart your web service to apply changes:
- For Apache:
service httpd restart - For PHP-FPM (if using Nginx):
service php-fpm restart
- For Apache:
- Find your active php.ini file:
Verify extension path is correct
Sometimes PHP can’t find the mysqli.so file. Check your extension directory:php -i | grep "extension_dir"Make sure
mysqli.soexists in that path (e.g.,/usr/lib64/php/modules/for 64-bit systems). If not, you may need to specify the full path in php.ini:extension_dir = "/usr/lib64/php/modules/" extension=mysqli.soFix file permission issues
Ensure the mysqli.so file has proper permissions for your web server to read it:chmod 644 /usr/lib64/php/modules/mysqli.soAlso confirm the web server user (usually
apacheornginx) has read access to the file and its parent directories.Check MySQL-PHP compatibility
CentOS 6’s default MySQL version is 5.1. If you’ve upgraded to MySQL 5.6+/MariaDB, there might be compatibility gaps with the system’s php-mysqli package. In this case, you can either:- Downgrade MySQL to 5.1 to match the system PHP
- Recompile the mysqli extension against your newer MySQL version (more advanced)
Debug code-level issues
Sometimes the problem isn’t with the extension itself, but your code. Test with a simple connection script to rule this out:<?php // Replace with your database credentials $host = 'localhost'; $user = 'your_username'; $pass = 'your_password'; $dbname = 'your_database'; $conn = mysqli_connect($host, $user, $pass, $dbname); if (!$conn) { die('Connection failed: ' . mysqli_connect_error()); } echo 'Successfully connected to the database!'; mysqli_close($conn); ?>Save this as
test_mysqli.phpin your web root, access it via browser, and check the exact error message— this will point you to whether it’s a connection issue or something else.
内容的提问来源于stack exchange,提问作者mynk

