【发布时间】:2017-04-01 05:01:20
【问题描述】:
我想将 Django (1.10) 从我的本地主机 (MacOS X) 连接到位于远程服务器 (Ubuntu 14.04) 而不是 sqlLite3 上的 Mysql 数据库 (Mysql-server)。
我在 Django 的 settings.py 文件中做了一些更改:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Etat_Civil',
'USER': 'root',
'PASSWORD': '*****',
'HOST': '172.**.**.58',
'PORT': '80',
}
在远程服务器上,我安装了mysql-server 并刚刚创建了一个表。
当我跑步时:
python manage.py migrate
我收到此错误:
MacBook-Pro-de-Valentin:Etat_Civil valentinjungbluth$ python manage.py migrate
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/Library/Python/2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Library/Python/2.7/site-packages/django/apps/config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Library/Python/2.7/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 119, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 316, in add_to_class
value.contribute_to_class(cls, name)
File "/Library/Python/2.7/site-packages/django/db/models/options.py", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 211, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 115, in load_backend
return import_module('%s.base' % backend_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 28, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
我需要安装另一个东西才能将 Django (Python) 连接到 MySQL 吗?或者我必须改变:
'ENGINE': 'django.db.backends.mysql',
通过
'ENGINE': 'mysql-server',
如果我跑步:
python3.5 manage.py migrate
我明白了:
MacBook-Pro-de-Valentin:Etat_Civil valentinjungbluth$ python3.5 manage.py migrate
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
import django
ImportError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
感谢您的帮助:)
经过一些步骤后进行编辑:
暂时没有。我听从了你的建议和本教程,暂时没有结果:tutorial
【问题讨论】:
-
您是否有忘记激活的虚拟环境设置?此外,如果您有需求文件,请确保运行它。
-
几个问题:1)你确定mysql服务器上的80端口吗?默认不是 80 2) 过去我必须安装 MySQL-python,如果安装了,你能用
pip freeze检查吗? -
@karthikr 我忘记了 virtual.env。所以我跟着一个教程,现在它被激活了,但我总是收到这个错误
Error loading MySQLdb module: No module named 'MySQLdb' -
@Aditya 我不确定港口的堡垒。我在哪里可以看到港口?在 mysql 配置中?对于
pip freeze,我没有类似MySQL-Python -
@Andromedae93 尝试 twp 步骤:1) 尝试设置
mysql port to 3306-> 看看它是否有效,如果无效 2)pip install MySQL-python
标签: python mysql django python-2.7 python-3.x