Django MySQL: SSL Error using mysqlclient and Python 2.7 - python

I'm a Django newby (I started yesterday) and I'm trying to configure the DB. But since I think I'll always be using Django with MySQL I want to set it up. I read the doc and I saw that in order to use MySQL you have to install a module, and so I did. I installed mysqlclient since installing MySQLdb with pip return:
$ pip2 install mysqldb
Could not find a version that satisfies the requirement mysqldb (from versions: )
No matching distribution found for mysqldb
and the Oracle solution isn't working at all (django don't recognize it).
So I entered my credentials, and it look like this in project's settings.py:
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname_entered_and_tested',
'USER': 'username_entered_and_tested',
'PASSWORD': 'correct_password',
'HOST': 'externale.domain_name_to_db.com',
'PORT': '3306',
}
}
So it returned me
$ python manage.py runserver
... 30 lines of Exceptions ...
django.db.utils.OperationalError: (2026, 'SSL connection error: error:00000001:lib(0):func(0):reason(1)')
So I googled and I found Django AWS RDS MySQL Error: (2026, 'SSL connection error: error:00000001:lib(0):func(0):reason(1)')
So I set my settings to:
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbname_entered_and_tested',
'USER': 'username_entered_and_tested',
'PASSWORD': 'correct_password',
'HOST': 'externale.domain_name_to_db.com',
'PORT': '3306',
'OPTIONS': {
'skip-ssl',
},
}
}
And it returned me
$ python manager.py runserver
... 30 lines of Exceptions ...
ValueError: dictionary update sequence element #0 has length 8; 2 is required
So I managed to generate my own certificats after reading another stackoverflow post, using OpenSSL and the MySQL doc (https://dev.mysql.com/doc/refman/5.6/en/creating-ssl-files-using-openssl.html) and I succeded, but django threw the same:
django.db.utils.OperationalError: (2026, 'SSL connection error: error:00000001:lib(0):func(0):reason(1)')
I already tried connecting with multiple other clients to my db using the same credentials, disabling SSL, enabling SSL, another user account.
I found a solution but it involve downgrading the MySQL version to 5.6.27 (Django server not starting correctly seems to be a mysql issue.) and it won't be a problem... if I only could :C . The MySQL database since to be a pain for a lot of Django users (so much post talking about that out there ...) and I'm just a newbie but I don't want to be discouraged by a simple bug and stop Django :S So, does someone have an answer ?
EDIT: I'm using the latest version of both django, pip and mysqlclient

Related

django.db.backends.postgresql error when try to makemigrations my django project

I am trying to switch my Django database from SQLite3 to PostgreSQl, so I follow many tutorials to install and setup Postgres with Django project.
I did the following: pip install psycopg2, pip install psycopg2-binary and I modified the settings.py like that:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': BASE_DIR / 'db.postgresql',
'USER': 'muusername',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432'
}
}
Finally I maked my database, by running the command python manage.py makemigrations.
However, I got this error:
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'sqlite3'
Please note that I am also seccesufully install the pgAdmin in my OS which is Windows 10 in a first step.
I know that the problem is related by configuration of Postgres in my django project, but I don't know how to fix it, also I checked my djnago version which is the latest one, also, all needed packages are installed in my venv.
You cannot add like this in postgres:
'NAME': BASE_DIR / 'db.postgresql', #You got that error because of this. This setting is for only sqlite3 not for postgres
Just add db_name in NAME:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'your_db_name', #add your db name here
'USER': 'postgres',
'PASSWORD': 'your_db_password',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Make sure you have actually downloaded the PostgreSQL installer itself and installed it.
I solve this problem by just upgrade the django, as you can see here in the exception:
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'sqlite3'
Django ask the user to use mysql, oracle or sqlite3, but not the case of postgresql.
So this exception will be fixed when the user upgrade the django version.
I hope this answer can be helpful for someone another.

Django+MySQL - "Authentication plugin 'caching_sha2_password' cannot be loaded"

When running the command: python manage.py runserver I'm getting the following error:
django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: The specified module could not be found.\r\n")
These are the relevant settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'polls',
'USER': 'root',
'PASSWORD': '<my-password>',
'HOST': 'localhost'
}
}
I've seen solutions that went around the problem, such as in this thread. They suggest changing the engine to mysql.connector.django, however the engine recommended by django is mysqlclient and therefore I would like to use it.
Other suggestion were to use naive password instead of sha2. I'd rather not go with this solution for security reasons.
Versions:
mysql 8.0.17 for Win64 on x86_64 (MySQL Community Server - GPL)
python 3.7.4
Django 2.2.4
mysqlclient 1.4.4
Try
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '<my-password>';

Django 1.9.9 unable to connect Azure SQL database from windows

On my windows machine, I have following packages installed in my app.
django: 1.9.9
pyodbc==4.0.3
django-pyodbc-azure==1.10.4.0
In my settings.py file..
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USERNAME'),
'PASSWORD': os.environ.get('DB_PASSWORD'),
'HOST': 'tcp:sdohsandbox.database.windows.net',
'PORT': '1433',
'OPTIONS': {
'driver': 'SQL Server Native Client 11.0',
'MARS_Connection': 'True',
},
'CONN_MAX_AGE': 120,
}
}
When I start server I am getting following error..
django.core.exceptions.ImproperlyConfigured: Django 1.9.9 is not supported.
Though I have latest version of pyodbc I am getting error.
Need help.
You have installed a version of django-pyodbc-azure that does not support Django 1.10. The project page on pypi says:
If you want to use it on older versions of Django, specify an appropriate version number (1.9.x.x for Django 1.9) at installation like this:
pip install "django-pyodbc-azure<1.10"

Setting up Django with MySQL, syncdb giving segmentation fault?

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

Setting Django up to use MySQL

I want to move away from PHP a little and learn Python. In order to do web development with Python I'll need a framework to help with templating and other things.
I have a non-production server that I use to test all of web development stuff on. It is a Debian 7.1 LAMP stack that runs MariaDB instead of the common MySQL-server package.
Yesterday I installed Django and created my first project called firstweb. I have not changed any settings yet.
Here is my first big piece of confusion. In the tutorial I followed the guy installed Django, started his first project, restarted Apache, and Django just worked from then on. He went to his browser and went to the Django default page with no problems.
Me however, I have to cd into my firstweb folder and run
python manage.py runserver myip:port
And it works. No problem. But I'm wondering if it is supposed to work like this, and if this will cause problems down the line?
My second question is that I want to set it up so it uses my MySQL database. I go into my settings.py under /firstweb/firstweb and I see ENGINE and NAME but I'm not sure what to put here.
And then in the USER, PASSWORD, and HOST areas is this my database and its credentials? If I am using localhost can I just put localhost in the HOST area?
MySQL support is simple to add. In your DATABASES dictionary, you will have an entry like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
You also have the option of utilizing MySQL option files, as of Django 1.7. You can accomplish this by setting your DATABASES array like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/path/to/my.cnf',
},
}
}
You also need to create the /path/to/my.cnf file with similar settings from above
[client]
database = DB_NAME
host = localhost
user = DB_USER
password = DB_PASSWORD
default-character-set = utf8
With this new method of connecting in Django 1.7, it is important to know the order connections are established:
1. OPTIONS.
2. NAME, USER, PASSWORD, HOST, PORT
3. MySQL option files.
In other words, if you set the name of the database in OPTIONS, this will take precedence over NAME, which would override anything in a MySQL option file.
If you are just testing your application on your local machine, you can use
python manage.py runserver
Adding the ip:port argument allows machines other than your own to access your development application. Once you are ready to deploy your application, I recommend taking a look at the chapter on Deploying Django on the djangobook
Mysql default character set is often not utf-8, therefore make sure to create your database using this sql:
CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_bin
If you are using Oracle's MySQL connector your ENGINE line should look like this:
'ENGINE': 'mysql.connector.django',
Note that you will first need to install mysql on your OS.
brew install mysql (MacOS)
Also, the mysql client package has changed for python 3 (MySQL-Client works only for python 2)
pip3 install mysqlclient
To the very first please run the below commands to install python dependencies otherwise python runserver command will throw error.
sudo apt-get install libmysqlclient-dev
sudo pip install MySQL-python
Then configure the settings.py file as defined by #Andy and at the last execute :
python manage.py runserver
Have fun..!!
If you are using python3.x then Run below command
pip install mysqlclient
Then change setting.py like
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB',
'USER': 'username',
'PASSWORD': 'passwd',
}
}
As all said above, you can easily install xampp first from https://www.apachefriends.org/download.html
Then follow the instructions as:
Install and run xampp from http://www.unixmen.com/install-xampp-stack-ubuntu-14-04/, then start Apache Web Server and MySQL Database from the GUI.
You can configure your web server as you want but by default web server is at http://localhost:80 and database at port 3306, and PhpMyadmin at http://localhost/phpmyadmin/
From here you can see your databases and access them using very friendly GUI.
Create any database which you want to use on your Django Project.
Edit your settings.py file like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': '',
}}
Install the following packages in the virtualenv (if you're using django on virtualenv, which is more preferred):
sudo apt-get install libmysqlclient-dev
pip install MySQL-python
That's it!! you have configured Django with MySQL in a very easy way.
Now run your Django project:
python manage.py migrate
python manage.py runserver
Actually, there are many issues with different environments, python versions, so on. You might also need to install python dev files, so to 'brute-force' the installation I would run all of these:
sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev
pip install MySQL-python
pip install pymysql
pip install mysqlclient
You should be good to go with the accepted answer. And can remove the unnecessary packages if that's important to you.
Run these commands
sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev
pip install MySQL-python
pip install pymysql
pip install mysqlclient
Then configure settings.py like
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'HOST': '127.0.0.1',
'PORT': '3306',
'USER': 'root',
'PASSWORD': '123456',
}
}
Enjoy mysql connection
Install mysqlclient
sudo pip3 install mysqlclient
if you get error:
Command "python setup.py egg_info" failed with error code 1 in
/tmp/pip-install-dbljg4tx/mysqlclient/
then:
1. sudo apt install libmysqlclient-dev python-mysqldb
2. sudo pip3 install mysqlclient
Modify settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'website',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTION': {'init_command':"SET sql_mode='STRICT_TRANS_TABLE',"},
}
}
Andy's answer helps but if you have concern on exposing your database password in your django setting, I suggest to follow django official configuration on mysql connection: https://docs.djangoproject.com/en/1.7/ref/databases/
Quoted here as:
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/path/to/my.cnf',
},
}
}
# my.cnf
[client]
database = NAME
user = USER
password = PASSWORD
default-character-set = utf8
To replace 'HOST': '127.0.0.1' in setting, simply add it in my.cnf:
# my.cnf
[client]
database = NAME
host = HOST NAME or IP
user = USER
password = PASSWORD
default-character-set = utf8
Another OPTION that is useful, is to set your storage engine for django, you might want it in your setting.py:
'OPTIONS': {
'init_command': 'SET storage_engine=INNODB',
}
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER': 'root',
'PASSWORD': '*****',
'HOST': '***.***.***.***',
'PORT': '3306',
'OPTIONS': {
'autocommit': True,
},
}
}
then:
python manage.py migrate
if success will generate theses tables:
auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session
and u will can use mysql.
this is a showcase example ,test on Django version 1.11.5:
Django-pool-showcase
Follow the given steps in order to setup it up to use MySQL database:
1) Install MySQL Database Connector :
sudo apt-get install libmysqlclient-dev
2) Install the mysqlclient library :
pip install mysqlclient
3) Install MySQL server, with the following command :
sudo apt-get install mysql-server
4) Create the Database :
i) Verify that the MySQL service is running:
systemctl status mysql.service
ii) Log in with your MySQL credentials using the following command where -u is the flag for declaring your username and -p is the flag that tells MySQL that this user requires a password :
mysql -u db_user -p
iii) CREATE DATABASE db_name;
iv) Exit MySQL server, press CTRL + D.
5) Add the MySQL Database Connection to your Application:
i) Navigate to the settings.py file and replace the current DATABASES lines with the following:
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/etc/mysql/my.cnf',
},
}
}
...
ii) Next, let’s edit the config file so that it has your MySQL credentials. Use vi as sudo to edit the file and add the following information:
sudo vi /etc/mysql/my.cnf
database = db_name
user = db_user
password = db_password
default-character-set = utf8
6) Once the file has been edited, we need to restart MySQL for the changes to take effect :
systemctl daemon-reload
systemctl restart mysql
7) Test MySQL Connection to Application:
python manage.py runserver your-server-ip:8000
This is a considerably old question but if anyone is working on any latest versions of python and django you can follow the following steps
Note - Versions
Python version - 3.9.5
Django version - 3.2.4
MySQL server version - 5.7
After installing django, run the following command
pip install mysqlclient
In my IDE if I do pip list this is the list of Packages and Versions
Package Version
----------- -------
asgiref 3.3.4
Django 3.2.4
mysqlclient 2.0.3
pip 21.1.2
pytz 2021.1
setuptools 57.0.0
sqlparse 0.4.1
Now, make sure you already have created the Database schema you are going to use.
In the settings.py file of your django project under DATABASES change
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
to
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
You should be able to run both
python manage.py makemigrations
and
python manage.py migrate
You must create a MySQL database first. Then go to settings.py file and edit the 'DATABASES' dictionary with your MySQL credentials:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'YOUR_DATABASE_NAME',
'USER': 'YOUR_MYSQL_USER',
'PASSWORD': 'YOUR_MYSQL_PASS',
'HOST': 'localhost', # Or an IP that your DB is hosted on
'PORT': '3306',
}
}
Here is a complete installation guide for setting up Django to use MySQL on a virtualenv:
http://codex.themedelta.com/how-to-install-django-with-mysql-in-a-virtualenv-on-linux/
python3 -m pip install mysql-connector
pip install mysqlclient
These commands helpful to settingup the mysql db in django without errors

Categories

Resources