Report this

What is the reason for this report?

how to setup mysql in python django project.

Posted on May 14, 2024

My project is built in Python Django which I have set up on Digital Ocean Cloud but it has inbuilt DBSQLITE database, I want to connect mysql with my database, when I connected mysql and loaded the data, I got empty tables, the data did not come. Please tell me step by step steps



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
0

Hey!

Are you using the DigitalOcean App Platform? If this is the case, the best approach here would be to use a managed MySQL cluster.

Using SQLite with the App Platform is not recommended as the App Platform has ephemeral storage, meaning that any data you store on the App Platform itself locally, will be lost after each redeployment.

To set up MySQL in your Python Django project on DigitalOcean, and resolve the issue of empty tables when migrating, follow these steps:

  1. Create a MySQL Cluster:

    • Follow the guide to create a MySQL cluster on DigitalOcean: Create a Managed MySQL Database
    • Note down the connection details (host, port, username, password, and database name).
  2. Install Django MySQL Adapter:

    pip install mysqlclient
    
  3. Update settings.py: Open your Django project’s settings.py file and update the DATABASES setting:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'your_database_name',
            'USER': 'your_mysql_user',
            'PASSWORD': 'your_mysql_password',
            'HOST': 'your_mysql_host',  # e.g., 'db-mysql-nyc3-12345-do-user-67890-0.b.db.ondigitalocean.com'
            'PORT': 'your_mysql_port',  # e.g., '25060'
        }
    }
    

    Replace your_database_name, your_mysql_user, your_mysql_password, your_mysql_host, and your_mysql_port with the details from your DigitalOcean Managed MySQL Database.

After that redeploy your project to the App Platform.

Let me know how it goes!

Best,

Bobby

Heya @zappcodeacademy,

The data you have in your SQLLITE db will not automatically migrate over to the MySQL.

Yes, you can run your migrations to create the structure of your DB in MySQL however you’ll need to find another way to dump the data from your SQLLITE to MySQL.

Let’s first make sure you ahve the proper stuff in your settings.py file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Migrate Your Existing Data

  1. Make sure all your Django models are up to date with your current SQLite database:
python manage.py makemigrations python manage.py migrate
  1. Dump data from SQLite to a JSON file:
python manage.py dumpdata > datadump.json
  1. Switch to your MySQL database (update your settings.py as described above), then apply migrations:
python manage.py migrate --run-syncdb
  1. Load your data into MySQL:
python manage.py loaddata datadump.json

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.