I need a brief explanation into how to connect my Django application to a Google Cloud MySQL database

119 Views Asked by At

I'm pretty new to web development and I'm currently deploying my first web application. It's pretty much built with stock Django and a few dependencies. Right now, I have it deployed on Railway.app with a very standard SQLite database. However, I would like to have my data stored in a more long term solution, so I'm looking at using MySQL inside of a Google Cloud instance.

I have already tested my application using a local mySQL database, now, I'm trying to connect it to the remote one. However, I'm a bit stumped in how to do it on the Google Cloud instance. Google Cloud provides me an public IP, I have an user and password and a database created there, but that is not enough and I'd like some guidance in how to proceed from here.

From what I understand, Google Cloud requires the user to access through an allowed network and I'm unsure what that means. I'm also unsure what this would look like once deployed on Railway. Google Cloud in general uses a lot of names that I don't quite understand. Do I need to use a Cloud Console to do anything? Is there anyway for me to directly connect to the public IP? I've read in an older thread that I'd need to set my connections and allowed ones, but I can't find where to do that.

Any guidance would be of great help!

1

There are 1 best solutions below

3
Mihail Andreev On BEST ANSWER

I recommend reading these docs.

But here are some recommendations:

  1. Setting Up Google Cloud MySQL Database:

    1. Go to Google Cloud Console.
    2. Navigate to SQL in the left navigation pane.
    3. Click on your MySQL instance.
    4. Note down your Instance details like the Public IP address, which you'll need later.
  2. Configuring Connection Settings:

    1. Still in the SQL details page, go to the Connections tab.
    2. Under Public IP, you will see an option to add network. Click on Add Network.
    3. Enter a name and the IP address (or range) from which you'll be connecting. If you want to allow all IP addresses (not recommended for production, but okay for testing), use 0.0.0.0/0.
    4. Save the configuration.
  3. Installing Required Packages:

    • To connect Django with MySQL, you need a package called mysqlclient. You can install it using pip:
      pip install mysqlclient
      
  4. Django Database Settings:

    • In your Django settings.py file, update the DATABASES setting to look like this:
      DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.mysql',
              'NAME': 'your_database_name',
              'USER': 'your_database_user',
              'PASSWORD': 'your_database_password',
              'HOST': 'your_google_cloud_mysql_public_IP',
              'PORT': '3306',  # default MySQL port
          }
      }
      
      Replace placeholders (your_database_name, your_database_user, etc.) with your actual Google Cloud MySQL details.

That should help you get started. Remember to test the connection locally (or from your deployment environment) to make sure everything works as expected before migrating any data or making other changes.