![Django 无法通过 ubuntu 10.04 连接 MySQL 数据库](https://linux22.com/image/54136/Django%20%E6%97%A0%E6%B3%95%E9%80%9A%E8%BF%87%20ubuntu%2010.04%20%E8%BF%9E%E6%8E%A5%20MySQL%20%E6%95%B0%E6%8D%AE%E5%BA%93.png)
我有一个简单的 Django 1.3.* 项目,我想在我的本地主机上运行它。我安装了所有必要的模块,例如:python-mysqldb、rptz 等...我安装了 ubuntu 10.04 和 python 2.7.2。当我尝试在终端中启动项目时
thor@thor:/media/SAJAT - Projects/project/simple project$ python manage.py runserver
我收到以下错误消息:
> Validating models...
Unhandled exception in thread started by <bound method Command.inner_run of <django.core.management.commands.runserver.Command object at 0x1224910>>
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 88, in inner_run
self.validate(display_num_errors=True)
File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "/usr/lib/python2.7/dist-packages/django/core/management/validation.py", line 102, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "/usr/lib/python2.7/dist-packages/django/db/backends/mysql/validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "/usr/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 338, in get_server_version
self.cursor()
File "/usr/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 250, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 322, in _cursor
self.connection = Database.connect(**kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '172.17.30.101' (110)")
我不知道这个IP地址来自哪里 172.17.30.101' (110) 因为我的mysql服务器已经安装在我的计算机上(localhost)
这是我的 settings.py 文件
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dbname', # Or path to database file if using sqlite3.
'USER': 'dbuser', # Not used with sqlite3.
'PASSWORD': '123456', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
#'PORT': '3306', i tried with this too # Set to empty string for default. Not used with sqlite3.
#'PORT': '3360', i tried with this too # Set to empty string for default. Not used with sqlite3.
},
# 'read': {
# 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
# 'NAME': 'newsonia', # Or path to database file if using sqlite3.
# 'USER': 'dbuser', # Not used with sqlite3.
# 'PASSWORD': 'dbname', # Not used with sqlite3.
# 'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
# 'PORT': '', # Set to empty string for default. Not used with sqlite3.
# }
}
我不知道应该如何解决此类错误。
答案1
首先也是最重要的是,Django 1.3 已经一年多不受支持了(自 2013 年 2 月 1.5 发布以来)。你应该考虑一下危险的在面向公众的情况下离开。 Ubuntu 10.04 Server 也将在不到一年的时间内达到这种状态...因此,除了您的实际问题之外,您还需要考虑基础设施升级。
您的站点正在连接到标准 POP3 端口上的一个看似随机的保留端口(它不是合法的公共 IP)。我看不出有任何情况会意外发生这种情况。你的设置中有一些东西是导致这会发生。
我的钱花在你的设置上,就像DATABASES
你没有注意到的另一个定义一样。通过将其添加到您的视图之一来确认这一点:
from django.conf import settings
print settings['DATABASES']
看看另一边会发生什么。从那里开始就可以找到问题所在。
如果失败了
- 检查旧
DATABASE_*
设置不会干扰类似的方法 - 通过 MySQL 模块代码追踪连接。您可以在跟踪中看到发生了什么。跟踪该信息(如果必须的话,插入打印语句),直到到达正在形成连接的位置。