简体   繁体   中英

Connect mysql to django

I would to connect Django (1.10) from my localhost (MacOS X) to a Mysql Database (Mysql-server) which is located on a distant server (Ubuntu 14.04) instead of sqlLite3.

I made some changes in the Django' settings.py file :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'Etat_Civil',
        'USER': 'root',
        'PASSWORD': '*****',
        'HOST': '172.**.**.58',
        'PORT': '80',
    }

On the distant server, I installed mysql-server and I juste created a table.

And when I run :

python manage.py migrate 

I get this error :

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

I need to install another thing in order to connect Django (Python) to MySQL ? Or I have to change :

'ENGINE': 'django.db.backends.mysql',

by

'ENGINE': 'mysql-server',

If I run :

python3.5 manage.py migrate

I get :

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?

Thank you for help :)

EDIT AFTER SOME STEPS :

Nothing for the moment. I followed your advices and this tutorial and none result for the moment : tutorial

You can find your ans here Database configuration

Database configuration snippet from the link

Configure the Django Database Settings:

Now that we have a project, we need to configure it to use the database we created.

Open the main Django project settings file located within the child project directory:

 nano ~/myproject/myproject/settings.py 

Towards the bottom of the file, you will see a DATABASES section that looks like this:

 . . . DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } . . . 

This is currently configured to use SQLite as a database. We need to change this so that our MySQL/MariaDB database is used instead.

First, change the engine so that it points to the mysql backend instead of the sqlite3 backend. For the NAME, use the name of your database (myproject in our example). We also need to add login credentials. We need the username, password, and host to connect to. We'll add and leave blank the port option so that the default is selected:

 . . . DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'myproject', 'USER': 'myprojectuser', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '', } } . . . 

When you are finished, save and close the file.

the last line states Did you forget to activate a virtual environment? .. so, did you?

If it really is mysql related, make sure to install some modules:

$ pip install mysqlclient    # python 2 and 3
$ pip install MySQL-python   # python 2
$ pip install pymysql        # python 2 and 3

I had the same problem but I finally did it: First I installed the command line developer tools required by running the following code inside the virtualenv

$ xcode-select --install

Then I installed MySQL-python by using the following code

pip install MySQL-python

Finally I migrated the database using:

python manage.py migrate

It worked without any errors The main problem with the error was that I didn't installed the command line developer tools required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM