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

执行Doctrine创建数据库时遇SQLSTATE[HY000][2002]连接拒绝错误求助

Fixing SQLSTATE[HY000] [2002] Connection refused in Symfony Doctrine Database Creation

Hey there, let's break down why you're hitting this connection error and how to fix it—first off, restarting Apache won't help here because Doctrine connects directly to your MySQL server, not through the web server. The Connection refused error means Symfony can't reach your MySQL instance at all. Here's what to check step by step:

1. Make sure MySQL is actually running

First things first—verify your MySQL (or MariaDB) service is up and running:

  • Run this command to check status:
    sudo service mysql status
    
    (If you're using MariaDB, replace mysql with mariadb)
  • If it's not running, start it with:
    sudo service mysql start
    
  • To avoid this issue in the future, set it to start on boot:
    sudo systemctl enable mysql
    

2. Double-check your Symfony database configuration

Head to your project's .env or .env.local file (.env.local takes priority, so use that for local overrides) and look at the DATABASE_URL line. Common mistakes here include:

  • Using localhost instead of 127.0.0.1: Sometimes localhost tries to use a Unix socket instead of TCP/IP, which might not be configured correctly. Switching to 127.0.0.1 forces a TCP connection, which is more reliable for local development.
  • Wrong port number: Default MySQL port is 3306—if you changed it during setup, make sure it matches here.
  • Incorrect username/password: Double-check that the credentials match what you set up for your MySQL user.

Example of a correct DATABASE_URL:

DATABASE_URL="mysql://your_db_user:your_db_password@127.0.0.1:3306/your_db_name?serverVersion=8.0&charset=utf8mb4"

3. Verify MySQL user permissions

Even if your credentials are right, your MySQL user might not be allowed to connect from your local IP. Let's check:

  • Log into MySQL as root:
    mysql -u root -p
    
  • Run this query to see which users have access:
    SELECT user, host FROM mysql.user;
    
  • If your user only exists for localhost, but you're using 127.0.0.1 in your config, create a new user (or update the existing one) to allow connections from 127.0.0.1:
    GRANT ALL PRIVILEGES ON your_db_name.* TO 'your_db_user'@'127.0.0.1' IDENTIFIED BY 'your_db_password';
    FLUSH PRIVILEGES;
    

4. Check if port 3306 is blocked or occupied

  • Make sure nothing else is using port 3306:
    sudo lsof -i :3306
    
    (If you see another process using it, you'll need to either stop that process or change MySQL's port)
  • If you're using a firewall (like UFW on Ubuntu), ensure it's not blocking local connections to 3306 (though this is rare for local development):
    sudo ufw allow 3306
    

5. Test a raw PDO connection

To rule out Doctrine-specific issues, create a simple PHP file (e.g., test_db_connection.php) in your project root with this code:

<?php
try {
    $pdo = new PDO('mysql:host=127.0.0.1;port=3306', 'your_db_user', 'your_db_password');
    echo "Successfully connected to MySQL!";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>

Run it with php test_db_connection.php—if this fails, the problem is with MySQL itself, not Symfony/Doctrine.


内容的提问来源于stack exchange,提问作者Moslah

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:43:09