django:python3.6.2+django2.0 Configuring MySQL

Source: Internet
Author: User
Tags mysql download safe mode

Continue learning in Django ...

The database that Django uses by default is Python's own SQLlite3, but Sqllite doesn't apply to large projects, so I swapped the database for MySQL, and here's how Django configures the database ...

The version I am using is: python3.6.2+django2.0+mysql5.7.21

MySQL download install skip, directly say the configuration process:

A. settings.py file modifies the database configuration to the following content:

# database# https://docs.djangoproject.com/en/2.0/ref/settings/#databasesDATABASES= {    'default': {        'ENGINE':'Django.db.backends.mysql',        'HOST':'127.0.0.1',        'PORT':'3306',        'NAME':'MySQL',        'USER':'Root',        'PASSWORD':'zwg123456',        'OPTIONS': {            'Init_command':"SET sql_mode= ' strict_trans_tables '",            },    }}

The configuration information is from top to bottom in the following sequence:

Drive (engine), host address (hostname), port number (port), database (name), user name (name), and login password (PASSWORD);

For Sql_mode settings, refer to the Django Documentation: https://docs.djangoproject.com/en/2.0/ref/databases/#setting-sql-mode

Second, add the following configuration in the __init_.py file:

# Coding=utf-8 Import pymysqlpymysql.install_as_mysqldb ()

The __init_.py file is an empty file because the Django connection MySQL uses the MySQLdb driver by default, but MySQLdb does not support Python3, so the MySQL driver is set to pymysql here.

III. implementation of data migration

Since the database that Django uses by default is Sqllite, the MySQL database is now reconfigured, so the original data needs to be migrated.

Once you have configured the above information, you can continue to develop your system by performing a data migration and then restarting the service.

Iv. issues that may be encountered

1. Error:

or  is required; You have 0.7.11.None

Mysqlclient currently only supports to python3.4, so if you are using a later version of Python, you can modify the following path file:

C:\Users\dell\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\django\db\backends\base\base.py

Comment out the following file:

If version < (1, 3, 3):

raise improperlyconfigured ("mysqlclient 1.3.3 or newer is required; You have%s" % Database. __version__)

2. Error:

" ' no_zero_date ', ' no_zero_in_date ' and ' Error_for_division_by_zero '
SQL modes should is used with strict mode. They'll is merged with strict mode in a future release. " ) = self._query (query) site"changing SQL mode ' No_auto_create_user ' is deprecated. It'll be removed in a future release. " ) = self._query (query)

When prompted, SQL modes should use Safe mode, that is, the feature will be discarded or merged in a future release, which is a common way to handle MySQL backwards compatibility;

Solution:

Modify the configuration file settings.py file in Django and comment out the settings sql_modes in the configuration of the database:

#Database#https://docs.djangoproject.com/en/2.0/ref/settings/#databasesDATABASES= {    'default': {        'ENGINE':'Django.db.backends.mysql',        'HOST':'127.0.0.1',        'PORT':'3306',        'NAME':'MySQL',        'USER':'Root',        'PASSWORD':'zwg123456',        #' OPTIONS ': {            #' Init_command ': "SET sql_mode= ' strict_trans_tables '",            # },    }}

Then run it again!

PS: If the configuration after the start of error, calm, check your modified file format, indentation, check the file path, check whether the MySQL database successfully installed ...

django:python3.6.2+django2.0 Configuring MySQL

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.