0

I have seen 10 different ways of configuring the MySQL database with Django, and none of them work.

I am running MySQL 8.0 and Django 2.0

I updated the Project/settings.py with the database settings as followed:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'name_of_db',
        'USER': 'root_user',
        'PASSWORD': 'not_telling_u_my_pass_:)',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

When I run the command to test the connectivity:

python manage.py dbshell

I get error:

django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

No other settings have been made, however, I have tried to follow a few guides which say that I must declare:

set DJANGO_SETTINGS_MODULE=mysite.settings

I have also issued:

python manage.py shell

With no luck. I am running Windows 7 as well. I have all of the mysql dependencies installed for connecting from Python to MySQL. I know this question has been asked a number of times, but a thread should exist with clear instructions. Many of the other threads do not have clear and concise instructions, and I'm using a later version of MySQL so hopefully this will help solve others issues experiencing the same.

Project Tree:

Server
|
 +-- Server
|  |
|  +-- _init_.py
|  +-- settings.py
|  +-- urls.py
|  +-- wsgi.py
+-- manage.py

Update 1 (10 mins after posting): I ran the python manage.py migrate and this installed all of the tables into my database schema. However, when I run the python manage.py dbshell command again, I still get the same error above saying the settings are not configured.

Update 2: After running:

python manage.py shell --settings=Server.settings

I get:

python manage.py dbshell
CommandError: You appear not to have the 'mysql' program installed or on your path.
dataviews
  • 2,466
  • 7
  • 31
  • 64
  • this may be userful. https://stackoverflow.com/questions/15556499/django-db-settings-improperly-configured-error – KAYKISIZ Jun 20 '18 at 14:14
  • @KAYKISIZ none of those have worked for me – dataviews Jun 20 '18 at 14:18
  • @dataviews show us your settings.py and your project tree if possible – Radek Hofman Jun 20 '18 at 14:24
  • @RadekHofman settings.py is the default settings.py, with only changes made to the DATABASES section like above. I will list the project tree now, just a moment and check my main post. Done the tree – dataviews Jun 20 '18 at 14:32
  • so when you try: python manage.py shell --settings=Server.settings ? the same? – Radek Hofman Jun 20 '18 at 14:32
  • That processes fine, but goes into shell mode (" >>> ")..Then when I try to run "python manage.py dbshell" it tells me "You appear not to have the mysql program installed or on your path" @RadekHofman – dataviews Jun 20 '18 at 14:36
  • it isearching for binary `mysql`. or mysql.exe on windows. You have to add it to your PATH environmental variable – Radek Hofman Jun 20 '18 at 14:38
  • @RadekHofman I'm not entirely sure how to do that. Could you suggest an article to follow ? – dataviews Jun 20 '18 at 14:44

2 Answers2

0

The ImproperlyConfigured often masks other errors. As you note, your database configuration is correct, which means the problem is most likely one of two problems.

  1. Some file imported during setup is causing an exception
  2. There is an import in settings.py which is in turn trying to use a setting (i.e., settings is used while being loaded)

First, if you have any imports in your settings.py, make sure those aren't going to in turn import django.conf.settings (imports like import os) are fine.

Next, inspect files from your project that might be imported during setup (apps, urls, custom middleware, template contexts, etc.) for possible errors that might arise during import. Make sure you can import all files on their own (some may complain because they require setup to be run first, you'll just have to inspect those extra carefully, or remove references to settings so that you can test them on their own).

Nikolas Stevenson-Molnar
  • 4,235
  • 1
  • 22
  • 31
  • This project is a clean install. A brand new project, in which I am immediately changing the database settings right after the project folder is created. I can't imagine that I would need to go through everything to confirm all of this... – dataviews Jun 20 '18 at 14:43
0

I met the same problem and I have fixed the problem finally. The reason is that MySql will treat different when you using the 'localhost' parameter. It will connect to the local server using a Unix socket file.

Check this document: https://dev.mysql.com/doc/refman/5.7/en/connecting.html

So you should use '127.0.0.1' instead of 'localhost' in DATABASES.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'name_of_db',
        'USER': 'root_user',
        'PASSWORD': 'not_telling_u_my_pass_:)',
        'HOST': '127.0.0.1', #This string will works!
        'PORT': '3306',
        #If you want to use 'localhost', you should provide the path 
        # of the UNIX socket. You should check /etc/my.cnf(the mysql 
        # config file) to get this parameter. 
        'OPTIONS': {'unix_socket': '/tmp/mysql.sock'}
    }
}

That will work in MySql 5.7, I am not test in MySql 8.0. Another suggestion: check user privileges in MySql, may be something wrong.

charles
  • 382
  • 5
  • 10