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.
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.
Some file imported during setup is causing an exception
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).
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.
Related
In mysite.settings.py I have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.path.join(BASE_DIR, 'test'),
#'NAME': 'test',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
}}
When I run 'python manage.py migrate' in the PyCharm terminal, nothing happens, no error message etc. No new tables are created in the database specified. I tried two things for NAME as you can see.
MySQL Server version 8.0.19
MySQL Connector/Python 8.0.19
Python version 3.8
In PyCharm I have package mysqlclient 1.4.6 installed.
Any advice on how to find the problem? How to get an error message?
Thank you
DATABASES property specifies your DB configurations. Now NAME is the property which specifies your database name.
Currently here 'NAME': os.path.join(BASE_DIR, 'test'), won't work as it will give you something like C:/YourUser/YourPath... so this string is not a valid db name. basically (os.path.join() returns a path to your project directory)
First create a db manually on MySql, note the name and put it in property as 'Name': DBNAME in SETTINGS.py.
(Make sure you have your environment set while running following commands)
Then try python manage.py makemigrations for checking migration changes, if everything goes fine up till here then fire python manage.py migrate command.
I've got a Django settings file which works on mac and linux which will let me use my. my.cnf file to connect to a remote MySQL database on AWS. However, on Windows 10 this doesn't seem to be the case. It keeps on saying it can't connect to the local database as if the my.cnf file doesn't exist.
I've installed mysql connector and python mysql connector on windows 10, along with pip install mysqlclient as well like I would need to on linux however the problem still persists.
db_conf = Path("Project/my.cnf")
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '{}'.format(db_conf),
}
}
}
With the same settings file I can run makemigrations, migrate and run the server. On windows this same thing crashes it. my.cnf is the same on Windows/Mac/Linux any help would be appreciated.
I suspect it's a hangup with the way Windows does paths but I'm unsure how to resolve this as I usually code in Linux.
I've resolved the issue but..not in the way I wanted. I don't know why but django and windows don't seem to like using os.path or Path when trying to use it to connect to mysql database.
As such I've used a longer winded route using configparser and no longer having django read a config file instead give it the details in the settings file.
import configparser
db_conf = Path("Project/my.cnf")
conf = configparser.ConfigParser()
conf.read(db_conf)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': conf.get("client", "database"),
'USER': conf.get("client", "user"),
'PASSWORD': conf.get("client", "password"),
'HOST': conf.get("client", "host"),
'PORT': '3306',
}
}
What I find hilarious about this whole situation is that Path works normally except when trying to read it with django on Windows. It's not the situation I wanted but it does resolve the issue. I hope this helps others who may be struggling doing the same thing when connecting to a remote mysql server on Windows.
What can I make with it? I'm beginner in python and django. I download it and I i wrote py manage.py makemigrate and I've get error. Can u help me?
Your issue is with your DB configuration in the setting.py. If you are using the default SQLite then copy/paste this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
and your project will work just fine. After this, run
python manage.py makemigrations
python manage.py migrate #copy all migrations to the database
python manage.py createsuperuser #to have a admin user to login to adminpanel
python manage.py runserver #starting the server
Otherwise, take a look at the official documentation how to connect MySQL, PostgreSQL, Oracle databases and required configurations.
Your error is in here:
SQLite is not like MySQL or other databases. Actually, it is not a real database. You are using a port, username, password and etc. These are the cause of the error. SQLite is not running in the server or another place. It is just a single file contains data information. Update yours to mine above and it should start work again or change your database to MySQL or others.
You need to supply all environment variables that are listed in your settings file. Such as DB_NAME that presented in your screenshot. Search for os.environ[<VARIABLE_NAME>], every VARIABLE_NAME should be defined.
If you are a beginner it is better to stay with the documentation and do like https://docs.djangoproject.com/en/2.1/intro/tutorial01/
If you could share the DB part of the settings.py it would help.
Generally python manage.py startapp appname should create the necessary files for you.
After which a python manage.py makemigrations and python manage.py migrate should work properly. And this should not come.
What are the steps I need to take to migrate from the default SQLite database to Postgres database?
I'm doing this to get my local development environment as close to my live server (which uses postrgres).
Or is there a reason why local development uses SQLite? Is it not recommended to use Postgres for local development?
You can try the following steps:
1. Install psycopg2 to configure the database:
pip install psycopg2
2. Inside the default settings.py
Change original values:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
To:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'NAME_OF_DB',
'USER': 'DB_USER_NAME',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost',
'PORT': 'PORT_NUMBER',
}
}
3. Migrate the DB:
python manage.py makemigrations
python manage.py migrate
EDIT:
Thanks #robotHamster comment. Here is the method to sync the existing data:
Backup the data first:
python manage.py dumpdata > datadump.json
After changing the DB setting:
python manage.py loaddata datadump.json
Source: What's the best way to migrate a Django DB from SQLite to MySQL?
when you are changing the database you might get a UNICODEERRO:'utf-8'
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
After wasting more than 5 days I finally got the solution .. you will never got that accurate error in internet, I figured it out by myself .
python manage.py dumpdata > datadump.json
then change the database settings.py as per your desired database and then apply the following commands ...
python manage.py makemigrations
python manage.py migrate
python manage.py loaddata datadump.json
and then if u got the error I have mentioned earlier, please follow step by step guide :
1.Install notepad ++
2.open your datadum.json file in notepad++
3.on the bottom right corner you will get the encoding will be anything else than utf-8
4.on the top bar select encoding to UTF-8
you are good to go ..then again
python manage.py load data datadump.json
I have suffered a lot for this ...so please upvote, and shares are also appreciated.
Thank you!
and for more clearance, you can watch this video:https://youtu.be/RBtEr3TXNwg
Here is a great tutorial on how to do this from Django Girls
It shows you the installation as well as the required changes in settings.py.
Hope I am not late. So to my experience if you already have data in your sqlite db, you might face some challenges because some fields in sqlite don't directly match with fields in postgres. For example datetime, and boolean fields.
I found a library that helped me to do this:
https://github.com/Hitman23/pgloader
The library does any needed conversions.
I'm currently working on building a Django app. I'm following the "tangowithdjango" tutorial, which uses Django 1.54. In their tutorial, they use Sql-lite, but I'm planning on building this app for most robust purpose, which is why I'm attempting to connect MySQL instead.
Needless to say, it's been a nightmare. I can't get MySQL to connect for the life of me.
Here's what my settings.py looks like:
DATABASE_PATH = os.path.join(PROJECT_PATH, 'app.db')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'rideb',
'USER': 'root',
'PASSWORD': 'nantucket',
#'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
#'PORT': '', # Set to empty string for default.
}
}
And here's my output I'm getting...
(rideb)grantmcgovern#gMAC:~/Dropbox/Developer/Projects/RideB/master$ python manage.py syncdb
Segmentation fault: 11
I've installed python-mysqldb, and now I'm simply getting this, and I'm very perplexed to say the least. Is it some Django compatibility issue?
Everything works fine as the tutorial suggests with SQL-lite, but not looking to use that.
OS:
Mac OSX 10.10 Yosemite
MySQL (installed via .dmg on Oracle's site):
Server version: 5.6.19 MySQL Community Server (GPL)
I had a similar problem and it turns out it was related to incorrect mysql implementation on my Mac (OS X 10.10 Yosemite). I had "mysql-connector-c" installed instead of "mysql". Uninstalling "mysql-connector-c" (brew uninstall mysql-connector-c) and installing "mysql" (brew install mysql) resolved the problem right away.
To find out if you are facing this exact problem as I did, open the Console app on your Mac, go to User Diagnostic Reports and look for the report for your crash (should start with Python_). Under queue, if it shows you "0 libmysqlclient.18.dylib", then you have the same problem as I had.
As already mentioned you do not need DATABASE_PATH.
My working configuration looks like:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_pass',
'HOST': '127.0.0.1',
}
}
I am using a different engine, because it is required for Python3. → see documentation
After that you have to create the database and the user. Do not forget to give all needed rights to the user.
With python manage.py migrate your database is populated with your models.
syncdb the predecessor to migrate will be removed in Django 1.9