You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MAMP搭建WordPress遇数据库连接错误求助

Troubleshooting "Error establishing a database connection" with 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 (usually 8889 instead of the default 3306). Even if you set DB_HOST to localhost, you might need to specify the port. Update your wp-config.php line 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:

    1. Open phpMyAdmin via MAMP's "Open WebStart page" button
    2. Select your WordPress database from the left sidebar
    3. Navigate to the "Privileges" tab
    4. Find your database user, click "Edit Privileges"
    5. Check the "All Privileges" box at the top, then click "Go"
  • Try using MAMP's MySQL socket path instead of localhost
    Sometimes localhost doesn't resolve correctly with MAMP. Use the socket path instead—you can find this in MAMP's settings:

    1. Open MAMP, go to "Preferences" > "Ports"
    2. Either click "Set to default Apache and MySQL ports" (simplifies things) or copy the socket path (usually /Applications/MAMP/tmp/mysql/mysql.sock)
    3. Update your wp-config.php line 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 path
    

    Ensure 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 named test-connection.php in 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 (or http://localhost:8888/test-connection.php if 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:36:57