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 settings.py file like below. In below example the settings.py file is located in DjangoHelloWorld / DjangoHelloWorld.
    DATABASES = {
        'default': {
            # MySQL database engine class.
            'ENGINE': 'django.db.backends.mysql',
            # MySQL database host ip.
            'HOST': '127.0.0.1',
            # 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 manage.py 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 manage.py 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 manage.py 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 / __init__.py file.
    import pymysql
    # install pymysql as mysql database driver.
    pymysql.install_as_MySQLdb()
  6. Now migrate Django project apps database again, then you can see below migrate output.
    DjangoHelloWorld > python3 manage.py 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 manage.py createsuperuser command.
  3. Now start DjangoHelloWorld project web server with command python3 manage.py runserver.
  4. Browse http://127.0.0.1:8000/admin/ in web browser.
  5. Login the admin site use above user account.
READ :   How To Install Anaconda On Linux, Windows, MacOS Correctly

1 Comment

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.