gpt4 book ai didi

python - 如何将 Django 开发者服务器连接到 MySQL 数据库?

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:55 24 4
gpt4 key购买 nike

开始之前请尊重我是新手,问我解决这个问题还需要什么信息,而不是关闭问题之类的。 ;) 好的。我正在尝试将我的 Django 开发人员服务器连接到远程服务器上托管的 MySQL 数据库。我已经安装了 MySQL 和 MySQL-Python,并在“settings.py”中进行了这些设置:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'database_name', # Or path to database file if using sqlite3.
'USER': 'database_user', # Not used with sqlite3.
'PASSWORD': 'database_password', # Not used with sqlite3.
'HOST': 'db9.subsys.no', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}

当我尝试使用 python manage.py runserver 启动开发者服务器时,我得到了这个:

Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Python/2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 411, in get_server_version
self.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
self.connection = Database.connect(**kwargs)
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/__init__.py", line 81, in Connect
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'db9.subsys.no' (60)")

当我将 'HOST': 'db9.subsys.no' 更改为 'HOST': '' 时,我得到了这个:

Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
self.validate(display_num_errors=True)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Python/2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 411, in get_server_version
self.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor
self.connection = Database.connect(**kwargs)
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/__init__.py", line 81, in Connect
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'database_user'@'localhost' (using password: YES)")

是否无法使用开发人员服务器连接到 MySQL,或者我是否需要对数据库进行某种配置?如果您有答案或需要更多信息,请回复。

提前致谢!

最佳答案

如果你想连接到远程 MySQL 服务器,你应该确定三件事:

  1. 服务器正在使用网络而不是套接字(my.cnf 中没有 skip-networking 行),并且服务器正在监听公共(public)IP(带绑定(bind)地址)。

  2. 您的防火墙允许远程访问 MySQL 端口,默认为 3306

  3. 允许用户远程访问:

    GRANT ALL ON someDatabase.* to someUser@some.remote.ip IDENTIFIED BY 'foopass';

关于python - 如何将 Django 开发者服务器连接到 MySQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10664311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com