使用MAMP搭建WordPress遇数据库连接错误求助
Hey there, let's work through this database connection issue step by step—since you've already checked the basics, we'll dig into some less obvious fixes that often trip up MAMP users.
Double-check MAMP's MySQL port number
MAMP frequently uses a non-standard port for MySQL (usually8889instead of the default3306). Even if you setDB_HOSTtolocalhost, you might need to specify the port. Update yourwp-config.phpline to:define('DB_HOST', 'localhost:8889');If that doesn't work, check MAMP's main dashboard—it displays the MySQL port right next to the "Start Servers" button.
Verify the database user has full permissions
You created a user in phpMyAdmin, but make sure that user has all privileges for your WordPress database. Here's how to confirm:- Open phpMyAdmin via MAMP's "Open WebStart page" button
- Select your WordPress database from the left sidebar
- Navigate to the "Privileges" tab
- Find your database user, click "Edit Privileges"
- Check the "All Privileges" box at the top, then click "Go"
Try using MAMP's MySQL socket path instead of localhost
Sometimeslocalhostdoesn't resolve correctly with MAMP. Use the socket path instead—you can find this in MAMP's settings:- Open MAMP, go to "Preferences" > "Ports"
- Either click "Set to default Apache and MySQL ports" (simplifies things) or copy the socket path (usually
/Applications/MAMP/tmp/mysql/mysql.sock) - Update your
wp-config.phpline to:
define('DB_HOST', 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock');Hunt for tiny typos in wp-config.php
Even a missing quote, extra space, or curly quote (instead of straight) can break the connection. Double-check these critical lines:define('DB_NAME', 'your_database_name'); define('DB_USER', 'your_username'); define('DB_PASSWORD', 'your_password'); define('DB_HOST', 'localhost:8889'); // or your socket pathEnsure there are no extra spaces before/after values, and all quotes are standard straight quotes.
Test the connection directly with a simple PHP script
Create a file namedtest-connection.phpin your WordPress root folder, paste this code (replace placeholders with your actual credentials):<?php $servername = "localhost:8889"; $username = "your_db_user"; $password = "your_db_pass"; $dbname = "your_db_name"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully!"; $conn->close(); ?>Access it via
http://localhost/test-connection.php(orhttp://localhost:8888/test-connection.phpif MAMP uses port 8888 for Apache). If this fails, the issue is with MAMP's MySQL setup, not WordPress.Restart MAMP and clear browser cache
Sometimes MAMP's MySQL server doesn't initialize properly. Quit MAMP completely, wait 10 seconds, then restart it. Also, try accessing wp-admin in incognito mode to rule out cached errors.
If none of these fixes work, let me know what the test PHP script outputs—we can narrow it down further from there.
内容的提问来源于stack exchange,提问作者john

