Django with Mariadb

lufy
September 07, 2017

Take reference from:


http://blog.csdn.net/wmj2004/article/details/53215944


As described in django documentation, mysql is supported by build in models, but in ./manage.py shell:


>>> from django.db import backends


There is no backends.mysql, and ./manage.py would pop errore: Error loading MySQLdb module: No module named MySQLdb, the official document doesn’t work well.


Here is how to deal with this error:


# pip install pymysql


Then modify __init__.py in mysite:


# vim ./mysite/__init__.py

# add followed lines

import pymysql

pymysql.install_as_MySQLdb()

 

Next is how to set django site:

Modify settings.py

# vim ./mysite/settings.py

DATABASES = {'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': './mysite/my.cnf',
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", }
}
}


# the last line is to activate strict mode of mysql, can be read at: https://docs.djangoproject.com/en/1.11/ref/databases/


Modify my.cnf


# vim ./mysite/my.cnf

[client]
database = dbname
user = dbuser
password = password
default-character-set = utf8


Make sure dbuser has proper authority.


After all  above, django site would run properly.


Comments (0)

Leave a Comment
Maximum 1000 characters

No comments yet. Be the first to comment!