无法使用便携式 Xampp 通过 Django 连接到 MySQL
Posted
技术标签:
【中文标题】无法使用便携式 Xampp 通过 Django 连接到 MySQL【英文标题】:Can't connect to MySQL with Django using portable Xampp 【发布时间】:2013-03-04 07:43:25 【问题描述】:我需要测试一个使用mysql的django应用程序,所以我安装了portable-xampp并加载了数据库,但是我无法用django访问它。 它在我使用远程数据库时有效,但在本地无效。 用户,密码和数据库都可以,我也尝试使用root,只是为了测试目的 有什么想法吗?
在settings.py中:
DATABASES =
'default':
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'awe_db', # Or path to database file if using sqlite3.
'USER': 'awe_db', # Not used with sqlite3.
'PASSWORD': 'mypwd', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
错误:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 2.6.1\helpers\pycharm\django_manage.py", line 21, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python27\lib\runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Users\Fernando Alves\Dropbox\Projetos\nova_b2blue\manage.py", line 14, in <module>
execute_manager(settings)
File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 459, in execute_manager
utility.execute()
File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 231, in execute
self.validate()
File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "C:\Python27\Lib\site-packages\django\core\management\validation.py", line 103, in get_validation_errors
connection.validation.validate_field(e, opts, f)
File "C:\Python27\Lib\site-packages\django\db\backends\mysql\validation.py", line 14, in validate_field
db_version = self.connection.get_server_version()
File "C:\Python27\Lib\site-packages\django\db\backends\mysql\base.py", line 415, in get_server_version
self.cursor().close()
File "C:\Python27\Lib\site-packages\django\db\backends\__init__.py", line 306, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python27\Lib\site-packages\django\db\backends\mysql\base.py", line 387, in _cursor
self.connection = Database.connect(**kwargs)
File "C:\Python27\Lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python27\Lib\site-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 'localhost' (10061)")
【问题讨论】:
您是否尝试将主机设置为127.0.0.1
,将端口设置为10061
?
是的,但是我使用3306
端口,但它也不起作用。
不确定这是否对您有帮助,但请查看:***.com/questions/6051043/…
没有,但还是谢谢你。
它只发生在便携式设备上。 =(
【参考方案1】:
为了方便从 Google 找到此内容的人,请尝试以下操作:
DATABASES =
'default':
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname',
'USER': 'user',
'PASSWORD': 'password',
'HOST': '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
'PORT': '7777',
mysql.sock 文件是这里的关键。
【讨论】:
以上是关于无法使用便携式 Xampp 通过 Django 连接到 MySQL的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 android studio 连接到“xampp server localhost”