MySQL (MariaDB) backend for Django under Python3 on Debian - mysql

I am attempting to configure Django running under Python3 on a Debian box to use MariaDB as its backend. If I alter my mysite/settpings.py as per the tutorial i.e.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'foo',
'USER': 'tim',
'PASSWORD': 'swordfish',
'HOST': 'localhost'
}
}
I get a lot of grief, culminating in
File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 30, in <module>
'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.
Did you install mysqlclient or MySQL-python?
Now, obviously, MySQLdb doesn't work on Python3 and although I have installed the mysql connector package doesn't seem to work.
If I try using the connector recommended by MySQL
i.e.
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'foo',
'USER': 'tim',
'PASSWORD': 'swordfish',
'HOST': 'localhost'
then I get a load of grief culminating in
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'
Error was: cannot import name 'BaseDatabaseFeatures'
Any suggestions?
UPDATE
Further to Matt Seymour's suggestion if I attempt a pip (actually pip3 as my system Python is still 2.x) install of mysqlclient I get the following...
tim#merlin:~/mysite$ sudo pip3 install mysqlclient
Collecting mysqlclient
Using cached mysqlclient-1.3.10.tar.gz
Complete output from command python setup.py egg_info:
/bin/sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-9uub69zo/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/tmp/pip-build-9uub69zo/mysqlclient/setup_posix.py", line 44, in get_config
libs = mysql_config("libs_r")
File "/tmp/pip-build-9uub69zo/mysqlclient/setup_posix.py", line 26, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
OSError: mysql_config not found
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-9uub69zo/mysqlclient
Could the fact that I am using MariaDB rather than MySQL be significant here?

OK, after some messing around I have an answer of sorts, although not as satisfactory as I would like.
The MySQLdb library isn't supported under Python 3.x, and so you should use the mysqlclient fork of it instead. However, because I am using the MariaDB fork of MySQL, some of Debian's dependencies required to install mysqlclient are unmet. I don't want to revert to MySQL as I am using MariaDB for other purposes.
So, as all I needed was a fairly robust, multi-user back-end RDBMS, I've installed PostgreSQL for use with Django which seems to work nicely enough.

Related

How to connect superset with mysql database?

I was trying to connect superset with mysql database with this SQLAlchemy URI:mysql://localhost:3306/mysql/database/table1 but not able to connect.
I was getting this error:
ERROR: {"error": "Connection failed!
The error message returned was:
No module named 'MySQLdb'", "stacktrace": "Traceback (most recent call last):
File \"/Users/akay/venv/lib/python3.7/site-packages/superset/views/core.py\", line 1703, in testconn
engine = database.get_sqla_engine(user_name=username)
File \"/Users/akay/venv/lib/python3.7/site-packages/superset/utils/core.py\", line 131, in __call__
value = self.func(*args, **kwargs)
File \"/Users/akay/venv/lib/python3.7/site-packages/superset/models/core.py\", line 923, in get_sqla_engine
return create_engine(url, **params)
File \"/Users/akay/venv/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py\", line 479, in create_engine
return strategy.create(*args, **kwargs)
File \"/Users/akay/venv/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py\", line 87, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File \"/Users/arkay/venv/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py\", line 118, in dbapi
return __import__(\"MySQLdb\")
ModuleNotFoundError: No module named 'MySQLdb'
"}
Is there any way to connect to a MySQL database?
MySQLdb is not compatible with Python 3, You need to use pymysql.
Install pymysql:
$ pip install pymysql
Connect to mysql data source with below URI string.
mysql+pymysql://user:pass#host/databasename
Configure your database user with hostname=localhost
You need to install mysql-connector-python module:
$ pip install mysql-connector-python
Had similiar issue. I'm sure this will save time for some of you out there:
Use 127.0.0.1 instead of localhost.
Here is the perfect template for MySQL string:
SQLALCHEMY_DATABASE_URI = 'mysql://username:password#127.0.0.1:3306/schema-name'
Install MySQL:
pip3 install mysqlclient
Upgrade SQLAlchemy:
pip install sqlalchemy==1.3.24
Load DB:
superset db upgrade
Create Admin user:
superset fab create-admin
If you are using Docker try using host.docker.internal as host name. It worked for me

unable to install mysqlclient on mac

I'm attempting to install mysqlclient using the command:
pip install mysqlclient
but i get the following error:
Using cached mysqlclient-1.3.12.tar.gz
Complete output from command python setup.py egg_info:
/bin/sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/8d/xdwdnphs1w5b_vtxrc67__5h0000gn/T/pip-build-ifu54299/mysqlclient/setup.py", line 17, in <module>
metadata, options = get_config()
File "/private/var/folders/8d/xdwdnphs1w5b_vtxrc67__5h0000gn/T/pip-build-ifu54299/mysqlclient/setup_posix.py", line 44, in get_config
libs = mysql_config("libs_r")
File "/private/var/folders/8d/xdwdnphs1w5b_vtxrc67__5h0000gn/T/pip-build-ifu54299/mysqlclient/setup_posix.py", line 26, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
OSError: mysql_config not found
I uninstalled python3 using brew uninstall --force python3 and downloaded the binary from pythons website but to no avail. I also attempted to download mysql connector from oracle website and again to no avail. I have tried upgrading pip again still doesnt work.
PS mysql is running in a docker container and i'm running django on my local machine.
Any suggestions on why i'm getting an error attempting to install mysqlclient
For Python I can recommend pymysql is pretty good and I always use it.
https://github.com/PyMySQL/PyMySQL
or you can follow this answer
Installing MySQL Python on Mac OS X
you need to install mysql and mysql connector before install my sql client
brew install mysql
brew install mysql-connector-c

Can't get MySQL/MariaDB to work with Django 1.10.1 and Python 3.5 on Fedora 24?

Besides the above, I'm using virtualenv and virtualenvwrapper. MariaDB server 10.1.17 was installed via dnf install
I'm working through the first Django tutorial. My runserver command:
(djTut3)$ python manage.py runserver
ran OK, with a warning about migration.
In part 2, here: https://docs.djangoproject.com/en/1.10/intro/tutorial02/
I ran:
(djTut3)$ python manage.py migrate
as instructed, and got the following errors:
ImportError: No module named 'MySQLdb'
During handling of the above exception, another exception occurred:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
I did a lot of searching, and tried the below (among other things), mostly from SO:
===
$ sudo dnf install MySQL-python
<No error>
$ python3 manage.py migrate
<Same errors as before>
$ sudo dnf install mariadb-devel
Skipping packages with conflicts:
(add '--best --allowerasing' to command line to force their upgrade)
$ sudo dnf install mariadb-devel --best --allowerasing
<No error>
$ python3 manage.py migrate
<Same errors as before>
$ pip3 install mysql-python
ImportError: No module named 'ConfigParser'
$ pip3 install mysql-connector
<No error>
$ pip3 install mysql-client
_mysql.c:40:20: fatal error: Python.h: No such file or directory
#include "Python.h"
compilation terminated.
error: command 'gcc' failed with exit status 1
$sudo dnf install python-devel mysql-devel
<already installed>
===
Now I still can't get
(djTut3)$ python manage.py migrate
to work, and
(djTut3)$ python manage.py runserver
gives the same error as the migrate command.
My settings.py includes:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djTut3',
'USER': os.environ['DB_USERNAME'],
'PASSWORD': os.environ['DB_PASSWORD'],
'HOST': '127.0.0.1',
'PORT': '',
}
}
Can anyone offer some help with this?
Thanks very much in advance for any response.
This is why you should install Python packages via pip, not your OS package manager. MySQLdb is not available for Python 3, as you can see from the error when you tried via pip3.
The reason you had issues installing mysql-client is presumably because you need the python-devel package, or whatever the Fedora equivalent is; that would be a dnf install.
However since you did succeed with mysql-connector, you can use that; you need to configure Django to use it as shown in that library's documentation, by replacing 'django.db.backends.mysql' with 'mysql.connector.django'.

settings causing mysql to throw error

I have installed mysql in my virtual environment using this command and it has installed successfully..
sudo apt-get install libmysqlclient-dev
sudo pip install MySQL-python
Now in my settings.py I have done this..
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.path.join(BASE_DIR, 'db_hotelnepal'),
}
}
But when I run server it throws error saying ""Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb""...
Whats the prob and how can I fix ?
MySQL-python is not compatible with python 3, and it gives this error. You didn't mention the python version you are using, but I have only seen this when using python 3 and MySQL, so I will make that assumption. I use MySQL-for-Python-3 which can be found here: MySQL-for-Python-3. It is compatible with python 2.7-3.3, haven't had a project with MySQL and python 3.4 to try it yet.
I install by running pip install https://github.com/davispuh/MySQL-for-Python-3/archive/1.0.tar.gz

Problem running syncdb in django

I am trying to go through the django docs tutorial and having a problem syncing mysql. On the command python manage.py syncdb I get the following error (note I'm running in windows 7):
...
File "C:\python27\lib\site-packages\django\db\backends\mysql\base.py", line 14, in <module>
raise Improperlyconfigured("Error loading Mysqldb module: %s" % e)
django.core.excepions.Improperlyconfigured: Error loading Mysqldb module: No module named mySQLdb
I have initialized the db in setting.py as:
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite', #the name I gave in Mysql with 'CREATE DATABASE mysite;'
'USER': 'root',
'PASSWORD': 'mypassword', # as set in MysqlInstanceConfig
'HOST': '',
'PORT': '',
So how do I get syncdb to run correctly? What does the missing module error mean and how do I correct it?
You need to install the python mysql library.
Here is a django-related guide to do this on windows
You have to install the mySQLdb module.
You most probably haven't installed the Python module that handles the mysql connections. You can install it through the Cheeseshop with pip or easy_install and it's called MySQL-python.