-1

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
    Your question is too broad. Web search for a tutorial on how to set up Django on your selected operating system. The steps will be the same when using a Compute Engine instance. Once you have a problem, post a question. To learn the basics of Google Cloud, a good resource is [Qwicklabs](https://qwiklabs.com/). They have many learning lessons on how to set up Compute Engine. Also, take a look at this [link](https://cloud.google.com/python/django) which shows how to use a preconfigured Django environment for Compute Engine. Practice with that and then deploy your own instance. – John Hanley Aug 08 '23 at 20:19
  • Ah, I see. I can't simply connect to the Cloud database and use just it, I need to have my whole application up and running inside of the google's ecosystem in order to use the database. Am I getting the right idea? I'd totally ditch Railway for it, then? – Patrick Pereira Aug 08 '23 at 21:11
  • No, you can connect to MySQL from anywhere with the correct authorization and configuration. However, if your goal is a cloud database, consider Google Cloud SQL for MySQL. That will give you a painless and simple deployment without management headaches. There are lots of tutorials on setting up MySQL in a VM. – John Hanley Aug 08 '23 at 21:34

1 Answers1

1

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.

Mihail Andreev
  • 979
  • 4
  • 6
  • For your answer add using the Cloud SQL Auth Proxy also clearly state you are recommending Google Cloud SQL for MySQL (which I agree with). The SQL Proxy solves the security and authorization item. Otherwise, MySQL is receiving clear text passwords and anyone can hack at the instance allowing `0.0.0.0`. – John Hanley Aug 08 '23 at 21:38
  • Dude. This was it. The only thing I had done wrong was that I did not know about having to set allowed networks and about 0.0.0.0/0 to let all IP addresses connect to my database. I'll figure out how to allow only the IP's that will connect to the database and this should have me set. Thanks a lot! – Patrick Pereira Aug 08 '23 at 21:43
  • Yep, it's not a production solution. But it's the right answer for the question. :) – Mihail Andreev Aug 08 '23 at 21:44