SoFunction
Updated on 2024-11-15

Django configuration Mysql database connection implementation

preamble

Django natively comes with database support for sqlite, but the databases we commonly use now are usually mysql, mangodb and other databases. So we have to change the database link so that we can use mysql as the django database link.

operating line

Now find it in the code. Find DATABASES = in the code and then make changes to the code.

DATABASES = {
    'default': {
        'ENGINE': '',
        'NAME': 'django_test',
        'USER': 'root',
        'PASSWORD': 'xxxxx',
        'HOST': '',
        'PORT': 'xxxx'
    }
}

So django will go to link mysql, but django primary link is through mysqlclient, but we are more commonly used is actually pymysql, so we can then change django link database way.

Let's find the project's __init__.py and add the code.

import pymysql

pymysql.version_info = (1, 4, 13, "final", 0)
pymysql.install_as_MySQLdb()

This will modify the database link. But we have to pay attention to a problem, django will not help us to create the database, so we have to manually create this library django_test in the database, the name with the same value as the name in the modified code can be.

Finally we'll just let Django initialize this database for us. We just need to type in the terminal.

python  migrate

Our django will then start initializing the database.

concluding remarks

Then this way our Django will start running our Mysql database.

to this article on the Django configuration Mysql database connection to the realization of the article is introduced to this, more related to Django connection Mysql database content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!