我想连接Django(1.10)从我的本地主机(MacOS X)到Mysql数据库(Mysql-server),它位于远程服务器(Ubuntu 14.04)而不是sqlLite3。
我对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?
谢谢你的帮助:)
编辑:
暂时没有。我听从了你的建议和这个教程,目前没有结果:教程
可以在这里找到您的ans数据库配置
链接中的数据库配置片段
配置Django数据库设置:
现在我们有了一个项目,我们需要配置它以使用我们创建的数据库。
打开子项目中的主Django项目设置文件项目目录:
nano ~/myproject/myproject/settings.py
在文件的底部,您将看到一个DATABASES部分如下所示:
. . . DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } . . .
当前配置为使用SQLite作为数据库。我们需要
修改为MySQL/MariaDB数据库。首先,更改引擎,使其指向mysql后端而不是sqlite3后端。对于NAME,使用您的数据库(在我们的例子中是myproject)。我们还需要添加登录凭证。我们需要用户名、密码和要连接的主机。我们将添加并留空端口选项,以便默认值为选择:
. . . DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'myproject', 'USER': 'myprojectuser', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '', } } . . .
完成后,保存并关闭文件。
我遇到了同样的问题,但我最终还是做到了:首先,我安装了所需的命令行开发工具,在virtualenv
中运行以下代码$ xcode-select --install
然后使用以下代码安装MySQL-python
pip install MySQL-python
最后我迁移数据库使用:
python manage.py migrate
这个错误的主要问题是我没有安装所需的命令行开发工具。
最后一行显示Did you forget to activate a virtual environment?
. .那你呢?
如果真的是mysql相关的,确保安装一些模块:
$ pip install mysqlclient # python 2 and 3
$ pip install MySQL-python # python 2
$ pip install pymysql # python 2 and 3