收藏
回答

VSCode本地调试例子程序(Django模版),为什么无法连接本地MariaDB数据库?

AppID
wx8a5cdffff90f2128

报错信息如下:(我用本地UI工具HeidiSQL连接没有问题):

连接参数:      "envParams": {

        "MYSQL_USERNAME": "root",

        "MYSQL_PASSWORD": "kkkkkkkk",

        "MYSQL_ADDRESS": "127.0.0.1:3306"

      },


 *  Executing task: docker run --rm -t  --network wxcb0 --name wxcloud_wxcloudrun-django-main -l role=container -l wxcloud=wxcloudrun-django-main -l hostPort=27081 -l wxPort=27082 -p 127.0.0.1:27081:80/tcp -e 'MYSQL_USERNAME=root' -e 'MYSQL_PASSWORD=abc123__' -e 'MYSQL_ADDRESS=127.0.0.1:3306' --cpus 1 -m 2GB --mount type=bind,source="C:\Users\brian\.wxcloudbase\.tencentcloudbase",target=/.tencentcloudbase,readonly wxcloud_wxcloudrun-django-main:latest 


[2025-04-22 01:42:26,204] [autoreload.py:637] [autoreload:run_with_reloader] [INFO]- Watching for file changes with StatReloader

Performing system checks...


System check identified some issues:


WARNINGS:

wxcloudrun.Counters.count: (fields.W122) 'max_length' is ignored when used with IntegerField.

        HINT: Remove 'max_length' from field

wxcloudrun.Counters.createdAt: (fields.W161) Fixed default value provided.

        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`

wxcloudrun.Counters.updatedAt: (fields.W161) Fixed default value provided.

        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`


System check identified 3 issues (0 silenced).

Exception in thread django-main-thread:

Traceback (most recent call last):

  File "/root/.local/lib/python3.8/site-packages/pymysql/connections.py", line 613, in connect

    sock = socket.create_connection(

  File "/usr/lib/python3.8/socket.py", line 808, in create_connection

    raise err

  File "/usr/lib/python3.8/socket.py", line 796, in create_connection

    sock.connect(sa)

ConnectionRefusedError: [Errno 111] Connection refused


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "/root/.local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection

    self.connect()

  File "/root/.local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner

    return func(*args, **kwargs)

  File "/root/.local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect

    self.connection = self.get_new_connection(conn_params)

  File "/root/.local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner

    return func(*args, **kwargs)

  File "/root/.local/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection

    connection = Database.connect(**conn_params)

  File "/root/.local/lib/python3.8/site-packages/pymysql/connections.py", line 353, in __init__

    self.connect()

  File "/root/.local/lib/python3.8/site-packages/pymysql/connections.py", line 664, in connect

    raise exc

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)")


The above exception was the direct cause of the following exception:


Traceback (most recent call last):

  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner

    self.run()

  File "/usr/lib/python3.8/threading.py", line 870, in run

    self._target(*self._args, **self._kwargs)

  File "/root/.local/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper

    fn(*args, **kwargs)

  File "/root/.local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run

    self.check_migrations()

  File "/root/.local/lib/python3.8/site-packages/django/core/management/base.py", line 486, in check_migrations

    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])

  File "/root/.local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__

    self.loader = MigrationLoader(self.connection)

  File "/root/.local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__

    self.build_graph()

  File "/root/.local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 220, in build_graph

    self.applied_migrations = recorder.applied_migrations()

  File "/root/.local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations

    if self.has_table():

  File "/root/.local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table

    with self.connection.cursor() as cursor:

  File "/root/.local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner

    return func(*args, **kwargs)

  File "/root/.local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor

    return self._cursor()

  File "/root/.local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor

    self.ensure_connection()

  File "/root/.local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner

    return func(*args, **kwargs)

  File "/root/.local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection

    self.connect()

  File "/root/.local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__

    raise dj_exc_value.with_traceback(traceback) from exc_value

  File "/root/.local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection

    self.connect()

  File "/root/.local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner

    return func(*args, **kwargs)

  File "/root/.local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect

    self.connection = self.get_new_connection(conn_params)

  File "/root/.local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner

    return func(*args, **kwargs)

  File "/root/.local/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 234, in get_new_connection

    connection = Database.connect(**conn_params)

  File "/root/.local/lib/python3.8/site-packages/pymysql/connections.py", line 353, in __init__

    self.connect()

  File "/root/.local/lib/python3.8/site-packages/pymysql/connections.py", line 664, in connect

    raise exc

django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)")


回答关注问题邀请回答
收藏

1 个回答

  • Alfred Huang
    Alfred Huang
    04-23

    本地调试方式运行的?本地调试也是在容器里运行的,但是容器内网络和主机网络是区分开的。可以把mysql address 改为本地的实际 ip,不要使用 127.0.0.1


    04-23
    有用
    回复
登录 后发表内容
问题标签