CodeIgniter连接MySQL失败求助:MAMP环境下需调整哪些配置?
Hey there, let’s work through this frustrating access denied error you’re hitting while using CodeIgniter with MAMP. Here are the key tweaks to get your database connection working:
1. Update CodeIgniter’s Database Port for MAMP
MAMP runs MySQL on port 8889 by default (not the standard 3306 most frameworks expect). Open your CodeIgniter database config file at application/config/database.php and set the correct port:
$db['default']['port'] = '8889';
2. Use 127.0.0.1 Instead of localhost as Hostname
MySQL sometimes treats localhost as a socket connection, but MAMP’s socket path might not align with CodeIgniter’s defaults. Switch your hostname to the loopback IP to force a TCP connection:
$db['default']['hostname'] = '127.0.0.1';
3. Verify User Permissions for the Correct Host
Even if you granted permissions to AliWalsh@localhost, you need to explicitly allow access for AliWalsh@127.0.0.1 (since we’re now connecting via IP). You can fix this in phpMyAdmin (accessible via MAMP’s control panel at http://localhost:8888/phpmyadmin/):
- Go to the "User accounts" tab
- Edit the
AliWalshuser - Ensure permissions are set for both
localhostand127.0.0.1(or use%for all hosts, fine for local development)
Alternatively, run this SQL query directly in phpMyAdmin’s SQL tab:
GRANT ALL PRIVILEGES ON *.* TO 'AliWalsh'@'127.0.0.1' IDENTIFIED BY 'your_password_here'; FLUSH PRIVILEGES;
4. Double-Check Credentials and Database Existence
It’s easy to miss a typo! Confirm:
- The username in
database.phpexactly matchesAliWalsh(case-sensitive on some systems) - The password has no extra spaces or typos
- The database you’re trying to connect to actually exists in MAMP’s MySQL (check phpMyAdmin to verify)
5. Ensure MAMP’s MySQL Server is Running
Quick sanity check: Make sure the MySQL service is started in MAMP’s control panel. Sometimes it stops unexpectedly, so a restart can fix odd connection glitches.
After making these changes, reload your CodeIgniter news page—you should be able to connect to the database successfully!
内容的提问来源于stack exchange,提问作者A.Walsh

