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

Django项目执行runserver时无法连接MySQL服务器求助

Fixing Django-MySQL Connection Error (2003, "Can't connect to MySQL server on '127.0.0.1' (61)")

Hey there, let's work through this connection issue you're facing. That core error message means your Django app can't establish a connection to your local MySQL server, which is a super common problem with a few straightforward fixes:

1. Make Sure MySQL Server is Actually Running

First things first—did you forget to start MySQL? On macOS, this is the #1 culprit for error 61.

  • If you installed MySQL via Homebrew:
    • Check its status with brew services list
    • Start it if it's stopped: brew services start mysql
  • If you used the official MySQL DMG installer:
    • Open System Preferences > MySQL and click "Start MySQL Server"
  • For command-line fans: Run mysql.server start

2. Double-Check Your Django Database Settings

Open your project's settings.py and verify the DATABASES config is correct. Tiny typos here break connections all the time:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'your_project_db',  # Ensure this database exists in MySQL
        'USER': 'mysql_username',   # Your actual MySQL user account
        'PASSWORD': 'mysql_password', # Correct password for that user
        'HOST': '127.0.0.1',        # Try 'localhost' if this doesn't work
        'PORT': '3306',             # Default MySQL port—don't leave this blank!
    }
}

3. Fix Unix Socket vs TCP/IP Connection Issues

Sometimes MySQL is running but only listening on a Unix socket instead of TCP/IP (which Django tries to use with 127.0.0.1).

  • Check if MySQL is listening on port 3306: Run lsof -i :3306 in your terminal. If no results appear, update your Django settings to use the socket path:
DATABASES = {
    'default': {
        # Keep other settings the same
        'HOST': 'localhost',
        'PORT': '',
        'OPTIONS': {
            'unix_socket': '/tmp/mysql.sock',  # Default socket path on macOS
        },
    }
}

4. Ensure Your MySQL User Has Permission to Connect from 127.0.0.1

Your MySQL user might be restricted to connecting only from localhost (not the IP 127.0.0.1).

  • Log into MySQL as root: mysql -u root -p
  • Check your user's host permissions:
    SELECT user, host FROM mysql.user WHERE user='your_mysql_username';
    
  • If you don't see 127.0.0.1 in the host column, grant access:
    GRANT ALL PRIVILEGES ON your_project_db.* TO 'your_mysql_username'@'127.0.0.1' IDENTIFIED BY 'your_password';
    FLUSH PRIVILEGES;
    

5. Restart Everything

After making any changes, restart your MySQL server (using the method from step 1) and then restart your Django dev server:

python manage.py runserver

Start with step 1—9 times out of 10, MySQL just wasn't running!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:40:28