CodeIgniter多数据库连接访问被拒绝问题排查求助
Hey there, let's work through fixing that "access denied" error you're hitting with multiple database connections in CodeIgniter. I've tackled this exact issue a few times, so here's a step-by-step breakdown of what to check:
Let's start with the basics—your default DB config snippet looks like this:
$db['default'] = array( 'hostname' => 'localhost', 'username' => 'bd1', 'password' => 'pass3214', 'database' => 'db1', // ... other settings );
- Verify that
bd1has the correct password (pass3214) for thedb1database. Typos here are the #1 culprit—even an extra space or capitalization mismatch will trigger an access denial. - Test these credentials directly using a MySQL client (like phpMyAdmin, MySQL Workbench, or the command line):
mysql -u bd1 -p pass3214 db1
If you can't connect here, the problem is with your MySQL user setup, not CodeIgniter.
MySQL ties users to specific hosts, and this is a super common gotcha:
- Your user
bd1might only be allowed to connect fromlocalhost, but if your app is trying to use an IP (like127.0.0.1) or a remote host, it'll get blocked. - Run this query in your MySQL server to check allowed hosts for
bd1:SELECT user, host FROM mysql.user WHERE user = 'bd1'; - If you don't see
localhostor%(which allows connections from any host), grant the necessary permissions:GRANT ALL PRIVILEGES ON db1.* TO 'bd1'@'localhost' IDENTIFIED BY 'pass3214'; FLUSH PRIVILEGES;
Since you mentioned multiple connections, make sure your second DB group is configured correctly:
- If you added a second group (e.g.,
$db['second_db']), double-check that it has its own unique credentials, hostname, and database name—don't just copy-paste the default and forget to update values. - When loading the second connection in CodeIgniter, ensure you're specifying it properly to avoid overwriting the default:
// Load the second connection as a separate object $this->second_db = $this->load->database('second_db', TRUE); - If you're switching between connections, make sure you're referencing the correct object (e.g.,
$this->second_db->query(...)instead of$this->db->query(...)).
CodeIgniter uses environment settings that can override your default config:
- If
ENVIRONMENTis set toproductioninindex.php,db_debugis disabled, which hides the exact error message. Temporarily setdb_debugtoTRUEto get the full MySQL error—it'll tell you exactly why access is denied (wrong user, password, host, etc.). - Look for environment-specific config files (like
application/config/production/database.php)—these can overwrite your default settings without you realizing it.
- Make sure your MySQL server is running. On Linux, check with
systemctl status mysql; on Windows, check the Services panel. - Try swapping
hostnamefromlocalhostto127.0.0.1. Sometimeslocalhostuses a Unix socket that's misconfigured, while127.0.0.1uses TCP/IP which might work better.
Rare, but possible: local firewalls or antivirus software can block MySQL's default port (3306). Temporarily disable them to see if that fixes the issue.
内容的提问来源于stack exchange,提问作者Biswajit17




