8

Getting a strange error. I created a database in MySQL, set the database to use it. Using the right settings in my Django settings.py. But still there's an error that no database has been selected.

First I tried:

python manage.py syncdb

Got this traceback:

django.db.utils.OperationalError: (1046, 'No database selected')

settings.py:

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

What have I missed?

lol5433
  • 539
  • 4
  • 21
  • 6
    For others who encounter 'no database selected', the issue for me was an incorrect setting key. I was using 'DATABASE' instead of 'NAME'. Not the same issue here, but it seems any problem with the config just returns no database selected, so be very careful that everything is correct. – Aurelion May 06 '19 at 01:46

3 Answers3

6

Check to make sure your database my_db exists in your MySQL instance. Log into MySQL and run;

show databases;

make sure my_db exists. If it does not, run

create database my_db;
tornesi
  • 269
  • 2
  • 8
  • It exists. Therefor I got very confused by this error. Is there something with my connector? – lol5433 Dec 18 '15 at 13:04
  • Try changing host to an empty string and see if that works. You could also try changing host to 127.0.0.1. – tornesi Dec 18 '15 at 14:22
  • After changing host to 127.0.0.1 the same error exists. – lol5433 Dec 18 '15 at 16:58
  • What else is in your settings.py file? Is it just the database information? There are some django commands that have be run ahead of time, where django creates additional tables it needs in the database. One command that needs to be run is, `python manage.py migrate` – tornesi Dec 18 '15 at 17:12
  • It's really a Mezzanine project with the standard settings. I have tried with the different commands such as what you wrote `python manage.py migrate` and also `python manage.py syncdb`. It's really weird, should there be anything else than `django.db.backends.mysql` to use mysqlclient 1.3.7? – lol5433 Dec 18 '15 at 17:20
  • That should be all you need in your settings file. Are you running on Windows or Linux? Is port 3306 open? Are there any tables in the database my_db? Does django successfully create them when running the manage.py migrate call? If not, maybe try making a different database name and try it again or delete that database and create it again. – tornesi Dec 18 '15 at 17:24
  • Yes, exactly. As I thought then. I'm running on OS X. `Open TCP Port: 3306 mysql`. Django doesn't even create the tables because I get the traceback that the database is not selected. So, no, there are no tables in the database. I tried creating many different databases with the same result. – lol5433 Dec 18 '15 at 17:33
  • As a test, maybe try switching to SQLite to see if it works. That will possibly tell us if this is a Django configuration problem or something with MySQL. It also appears that there maybe an issue with Python+Django+Mac+Mysql. http://stackoverflow.com/questions/21792569/mac-os-x-python-django-mysql http://macosxbits.com/2015/04/how-to-configure-django-and-mysql-setup-in-mac-os-x/ – tornesi Dec 18 '15 at 17:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98367/discussion-between-lol5433-and-tornesi). – lol5433 Dec 18 '15 at 17:46
0

GRANT access privileges to the user mentioned in the file

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';

You need not grant all privileges. modify accordingly.

Vishnu Kiran
  • 620
  • 7
  • 15
0
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my_db',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

Here name means database
Just delete database my_db and create again
Step 1:- Use drop database databaseName
Step 2:- Create Database databaseName
Then use the migrate command
It will definitely work

moken
  • 3,227
  • 8
  • 13
  • 23