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

huangapple go评论84阅读模式
英文:

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

问题

我对web开发还比较新手,目前正在部署我的第一个Web应用程序。它基本上是使用标准的Django和一些依赖项构建的。目前,我已经在Railway.app上部署了它,使用的是一个非常标准的SQLite数据库。然而,我希望将我的数据存储在一个更长期的解决方案中,所以我正在考虑在Google Cloud实例中使用MySQL。

我已经使用本地的MySQL数据库测试了我的应用程序,现在我正在尝试将其连接到远程数据库。然而,我对如何在Google Cloud实例上进行操作感到有些困惑。Google Cloud要求用户通过允许的网络进行访问,我不确定这是什么意思。我也不确定在部署在Railway上后会是什么样子。总的来说,Google Cloud使用了很多我不太理解的术语。我需要使用Cloud Console来做些什么吗?我是否可以直接连接到公共IP?我在一个旧的帖子中读到,我需要设置我的连接和允许的连接,但我找不到在哪里进行设置。

任何指导都将非常有帮助!

英文:

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

得分: 1

我建议阅读这些文档

以下是一些建议:

  1. 设置 Google Cloud MySQL 数据库

    1. 打开Google Cloud 控制台
    2. 在左侧导航窗格中导航到 SQL
    3. 点击你的 MySQL 实例。
    4. 记下你的 实例详细信息,比如公共 IP 地址,稍后会用到。
  2. 配置连接设置

    1. 在 SQL 详细信息页面中,转到 连接 选项卡。
    2. 公共 IP 下,你将看到一个添加网络的选项。点击 添加网络
    3. 输入一个名称和你将要连接的 IP 地址(或范围)。如果你想允许所有 IP 地址连接(不推荐用于生产环境,但在测试中可以使用),使用 0.0.0.0/0
    4. 保存配置。
  3. 安装所需的包

    • 要将 Django 与 MySQL 连接,你需要一个名为 mysqlclient 的包。你可以使用 pip 安装它:
      pip install mysqlclient
      
  4. Django 数据库设置

    • 在你的 Django 的 settings.py 文件中,将 DATABASES 设置更新为以下内容:
      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',  # 默认的 MySQL 端口
          }
      }
      

      将占位符(your_database_nameyour_database_user 等)替换为你实际的 Google Cloud MySQL 详细信息。

这些步骤应该能帮助你入门。在迁移任何数据或进行其他更改之前,请记得在本地(或部署环境)测试连接,确保一切按预期工作。

英文:

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.

huangapple
  • 本文由 发表于 2023年8月9日 03:08:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定