执行Doctrine创建数据库时遇SQLSTATE[HY000][2002]连接拒绝错误求助
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:
(If you're using MariaDB, replacesudo service mysql statusmysqlwithmariadb) - 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
localhostinstead of127.0.0.1: Sometimeslocalhosttries to use a Unix socket instead of TCP/IP, which might not be configured correctly. Switching to127.0.0.1forces 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 using127.0.0.1in your config, create a new user (or update the existing one) to allow connections from127.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:
(If you see another process using it, you'll need to either stop that process or change MySQL's port)sudo lsof -i :3306 - 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

