Skip to main content
 首页 » 编程设计

python之无法从 django 连接到 mysql 将验证模型

2024年11月01日30TianFang

我不明白发生了什么,我的 mysql 运行良好 我用相同的用户名和密码进行了测试连接,并且它有效,但是当我尝试连接 Django 时出现此错误。服务器也运行正常。

 Unhandled exception in thread started by <bound method Command.inner_run of     <django.contrib.staticfiles.management.commands.runserver.Command object at 0x129e0d0>> 
Traceback (most recent call last): 
 File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site- packages/django/core/management/commands/runserver.py", line 91, in inner_run 
 self.validate(display_num_errors=True) 
 File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate 
num_errors = get_validation_errors(s, app) 
File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/django/core/management/validation.py", line 103, in get_validation_errors 
connection.validation.validate_field(e, opts, f) 
 File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 14, in validate_field 
db_version = self.connection.get_server_version() 
File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 415, in get_server_version 
self.cursor().close() 
File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/django/db/backends/__init__.py", line 306, in cursor 
cursor = self.make_debug_cursor(self._cursor()) 
File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 387, in _cursor 
self.connection = Database.connect(**kwargs) 
File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.5-i386.egg/MySQLdb/__init__.py", line 81, in  Connect 
return Connection(*args, **kwargs) 
File "/Library/Frameworks/Python.framework/Versions/7.1/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.5-i386.egg/MySQLdb/connections.py", line 187, in __init__ 
super(Connection, self).__init__(*args, **kwargs2) 
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost'   (using password: YES)") 

设置.py

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

请您参考如下方法:

您已在 settings.py 中正确设置所有详细信息,您的问题似乎是,您没有 localhost@root 的所有权限,要获得所有权限,请运行以下命令。

mysql> grant all privileges on *.* to root@localhost identified by 'password' with grant option; 

在 MySQL 命令提示符下,您将拥有以 root 身份访问本地主机的所有权限。