How to migrate SQLite Database to MYSQL in Django

Post Reply
learner
Posts: 8
Joined: 28 Mar 2019

How to migrate SQLite Database to MYSQL in Django

Post by learner » 20 Oct 2019

I am struggling in migrating SQLite to MYSQL in django and searching for best way to do so. If anyone knows then please guide the way to do so.

User avatar
Aisangam
Site Admin
Posts: 28
Joined: 24 Mar 2019
Location: Delhi, India
Contact:

Re: How to migrate SQLite Database to MYSQL in Django

Post by Aisangam » 20 Oct 2019

Great Question which is addressed here. Before giving answer let me proceed with knowing you how python programs are interfaced with the databases.
Do you know how python programs are interfacing with the databases.
If not, then the answer is through drivers. Let me write down below
Database Driver
PostgreSQL psycopg2 module
MYSQL MySQL-python or mysql-client

Now It is very easy if you have understand the above written concept. You need to migrate SQLite database to MYSQL so use driver mysqlclient. You can install it using pip. Please type below command

Code: Select all

pip install mysqlclient
Great you are done with the step 1. Now create name of database in the mysql by login mysql with the username and password.

If some one donot know how to login to mysql, then donot worry. Please type the below command in the terminal

Code: Select all

mysql -u root -p
Enter the password which one have set during mysql installation.

It is the time to create database. When you enter mysql through terminal, please enter the below command

Code: Select all

CREATE DATABASE **your_project_name** CHARACTER SET UTF8;
It is the time to grant privileges for the database created for the user.

Code: Select all

GRANT ALL PRIVILEGES ON your_project_name.* TO your_username@localhost;
Please flush the PRIVILEGES

Code: Select all

FLUSH PRIVILEGES;
Quit from database in the terminal using the below command

Code: Select all

QUIT
Go to settings.py inside the created django project and edit the below lines
Orginal code

Code: Select all

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
Modified Code

Code: Select all

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '',  # Name of database
        'USER': '', # user may be root or anything else
        'PASSWORD': '', password you login for mysql
        'HOST': 'localhost', 
        'PORT': '',
    }
}
Thanks and With Regards
http://www.aisangam.com/

Post Reply