How Python3 and Django support MySQL and MARIADB databases

Source: Internet
Author: User

Original address:http://www.server110.com/mariadb/201404/10020.html

First, the default engine in [email protected] is django.db.backends.mysql. But in Python3 if this write, will find Django.db.backends.mysql rely on mysqldb[5], and MYSQLDB and incompatible Python3 then find a new way to continue to use MySQL.

First, according to MySQL document [3], since MySQL Connector/python 1.1.1 started, the introduction of Mysql.connector.django, can be directly used as a Django database engine, [4] but I tried 1.1.2 and 1.1.3 and found that it was not so.

Later, found Pymysql[1], support Python3, Google know how to use the combination with Django [2], the answer is as follows:

First, install Pymysql

For Windows

For Fedora


Pip-python3 Install Pymysql


Pip-python3 Install Pymysql

Then, add the following two lines to the __init__.py file in the Django site:


Import Pymysql

Pymysql.install_as_mysqldb ()


Import Pymysql

Pymysql.install_as_mysqldb ()

Finally, the configuration of the database (in settings.py)


DATABASES = {
' Default ': {
' ENGINE ': ' Django.db.backends.mysql ', #数据库引擎
' NAME ': ' Test ', #数据库名
' USER ': ' Root ', #用户名
' PASSWORD ': ' Root ', #密码
' HOST ': ', #数据库主机, localhost by default
' PORT ': ', #数据库端口, MySQL defaults to 3306
' OPTIONS ': {
' Autocommit ': True,
},
}
}


DATABASES = {

' Default ': {

' ENGINE ': ' Django.db.backends.mysql ', #数据库引擎

' NAME ': ' Test ', #数据库名

' USER ': ' Root ', #用户名

' PASSWORD ': ' Root ', #密码

' HOST ': ', #数据库主机, localhost by default

' PORT ': ', #数据库端口, MySQL defaults to 3306

' OPTIONS ': {

' Autocommit ': True,

},

}

}

Then synchronize the database:

For Windows

For Fedora

OK, It works!



How Python3 and Django support MySQL and MARIADB databases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.