How to configure MySQL with Django - python

Am trying to use MySQL with Django 2.0, I have installed it using "pip install mysqlclient" and it installed successfully. What do i need to do next?

In settings.py you'll need:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'user_name',
'PASSWORD': 'user_password',
'HOST': 'localhost',
'PORT': '3306',
}
You'll need to create the db and user and grant permissions within MySQL.
You'll also need to ensure you have MySQL setup and running.

Related

Django error when using mysql-connector-python

I want to set the mysql database using mysql-connector-python for setting up a web site made with Django using a database backend but I get this error:
django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
My credentials (name, user, password, host, port) works fine, so the problem isn't here.
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'library',
'USER': 'username',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
'autocommit': True,
'use_pure': True,
},
}
}
I'm using python 3.8.5 and mysql-connector-python 8.0.21 and I understand that mysql-connector-python, starting from version 8.0.13, has a bug, which makes it impossible to use with Django and to avoid the bug I added ‘use_pure’: True in database options. But it doesn't work.
I found this question but it doesn't say anything about using ‘use_pure’: True.
If the mysql-connector-python isn't compatible with python 3, what can I use instead? (I can't wait for the support release).
you do not have the option to use mysql connector, you have to install mysql-client from https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient and try installing all the wheels incase you get errors, while installing using pip install

Issue with Connecting Django to Postgresql database

When I try to connect my app to postgresql db I always get this same error:
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'sqlite3'
Here is my code in settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': 'pass123',
'HOST': 'localhost',
'PORT': '5432'
}
}
I am using django version 3.0.2 and my psycopg2 version 2.8.5 . I did downgrade to earlier version of django and still the problem persists. Also my python version is 3.8.
I tried doing this too
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': 'pass123',
'HOST': 'localhost',
'PORT': '5432'
}
}
I thought it was the issue with the virtual env so I decided to perform on other projects too but still does not work. Can anyone please help me.
I fixed it by uninstalling the psycopg2. I installed psycopg2-binary.
pip install psycopg2-binary

Only protocol 3 supported error

I'm trying to configure Django with PostgreSQL on Windows Server 2012.
but after a while i get this error
only protocol 3 supported
I'm using Postgres 9.6 and psycopg2 2.7.3 and Django 1.11 and mod_wsgi.
i also tried Postgres 9.5 and psycopg2 2.6 but did not work
In settings.py I have
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'USER': 'postgres',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
}
}
i read this question and also this question but did not found any solutions.
Do you know any solutions for the problem?

Python mysql xampp

i installed xampp server. I want to access MySQL database through python. I tried to install MySQL-python package. It needs visual studio which I don't want to install.
I tried through MySQL connectors, but it is not allowing to do so.
How can I access MySQL database through python?
You can try like this,
db=MySQLdb.connect(user="user_name",passwd="password",db="database_name",unix_socket="mysql.sock file path")
OR try running, python manage.py runserver
OR once check your default setting of database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'database_name', # Or path to database file if using sqlite3.
'USER': 'user_name', # Not used with sqlite3.
'PASSWORD': 'password', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306' # Set to empty string for default. Not used with sqlite3.
}
}

connect mysql database with python

i am very new to python Django. i need to connect the MySQL database with python, i really don't have any idea about that ..
i am using python 1.7.
tell me how to connect MySQL database with python. what settings i need to fill in settings.py.
i have goggled it but i am not getting a proper answer please tell me from the beaning how to achieve it.
i have run this command now what are the next steps ?
sudo apt-get install python-mysqldb
i really don't have any idea about the further steps, please guide me
Well you just installed MySQL Database interface for python
Make sure to run pip freeze and see MySQL-python==1.2.5 is there or not .)
And include below snippet in your settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'yourdatabasename',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '',
}
}

Categories