How To Connect MySQL Database In Django Project

This example will show you how to use PyMySQL to connect MySQL database server in Django project. Then you can use MySQL server as the backend database server to store your Django web project data.

1. Set MySQL As Django Project Default Database Server.

  1. Install pymysql library in your PyCharm Django project ( refer PyCharm Project Add External Library (PyMySQL) Path Example ) or in your operating system use pip command as below.
    pip3 install pymysql
  2. Create a Django project in PyCharm ( refer Hello World Django PyCharm Example ).
  3. Use MySQL databases settings to replace default SQLite3 database settings in Django project file like below. In below example the file is located in DjangoHelloWorld / DjangoHelloWorld.
        'default': {
            # MySQL database engine class.
            'ENGINE': 'django.db.backends.mysql',
            # MySQL database host ip.
            'HOST': '',
            # port number.
            'PORT': '3306',
            # database name.
            'NAME': 'dev2qa',
            # user name.
            'USER': 'jerry',
            # password
            'PASSWORD': 'jerry',
            # connect options
            'OPTIONS': {'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",},

    pycharm django user register and login example source files

2. Migrate Django Project Apps Databasse Tables To MySQL.

  1. Now you can run python3 migrate command to migrate Django project app database tables to the MySQL database server. You have two method to do it.
  2. Method 1  : Click Tools —> Run Tasks menu item in PyCharm, then input migrate in the bottom console.
  3. Method 2 : Open a terminal and cd into Django project root folder ( DjangoHelloWorld ), then run python3 migrate command.
  4. But both method will throw ModuleNotFoundError: No module named ‘MySQLdb’ error.
  5. To resolve this error, add below code in DjangoHelloWorld / DjangoHelloWorld / file.
    import pymysql
    # install pymysql as mysql database driver.
  6. Now migrate Django project apps database again, then you can see below migrate output.
    DjangoHelloWorld > python3 migrate
      Applying user_register_login.0001_initial... OK
    Process finished with exit code 0
  7. When the migrate complete successful, you can see below tables is created in MySQL.
    django project app tables in mysql

3. Create Django Admin Site Superuser.

Now create the Django project admin site superuser.

  1. Open terminal and cd into DjangoHelloWorld root folder.
  2. Then run python3 createsuperuser command.
  3. Now start DjangoHelloWorld project web server with command python3 runserver.
  4. Browse in web browser.
  5. Login the admin site use above user account.
READ :   Hello World Django PyCharm Example


  1. When I run the migrations i get the following Error:

    “mysqlclient 1.2.13 or newer ist required; you have 0.9.3.”

    The version 0.9.3. is the newest version of pymysql.
    Am I doing anything wrong?


    1. Hi Janik, the reason for this is that the version of mysqlclient (0.9.3) that comes with PyMySQL is outdated. Simply run “pip install mysqlclient” and delete the code in the file. This will allow you to run “python3 migrate” without a problem.


  2. When I try to run my migrations I get the following error:

    “mysqlclient 1.3.13 or newer is required; you have 0.9.3.”

    Sounds good but the version 0.9.3 is the newest version of pymysql.
    Am I doing anything wrong?

    I’m thankfull for any reply 🙂


  3. Hi Jerry Zhao, thanks for hardwork
    but, can you please tell me why my command
    python makemigrations
    shows the error stating that [email protected] access denied using password YES

    I will be waiting for your reply regards,
    and if anyone else is interested/they know the ans please give a reply your replies are most welcome…


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.