简体   繁体   English

无法在 Asp.net 核心应用程序中连接到 RDS

[英]Can't connect to RDS in Asp.net core application

I've created a MySQL RDS on AWS console.我在 AWS 控制台上创建了一个 MySQL RDS。 Then I successfully connected to that RDS in MySQL Workbench using following steps:然后我使用以下步骤成功连接到 MySQL Workbench 中的 RDS:

  1. Connection Method: Standard (TCP/IP)连接方式:标准(TCP/IP)
  2. Hostname: zargham.ccir1327bjhl.us-east-2.rds.amazonaws.com主机名:zargham.ccir1327bjhl.us-east-2.rds.amazonaws.com
  3. Port: 3306端口:3306
  4. Username: [given at the time of RDS creation]用户名:[在创建 RDS 时给出]
  5. Password: [given at the time of RDS creation]密码:[在创建 RDS 时给出]

Then I try to connect it in my Asp.net core application, and it is giving me error on openConnection().然后我尝试在我的 Asp.net 核心应用程序中连接它,它在 openConnection() 上给我错误。 Following connection string is used:使用以下连接字符串:

"Server=zargham.ccir1327bjhl.us-east-2.rds.amazonaws.com; Port=3306; Database=aws; Uid=[same as above]; Pwd=[correct Password];" “Server=zargham.ccir1327bjhl.us-east-2.rds.amazonaws.com;Port=3306;Database=aws;Uid=[同上];Pwd=[正确密码];”

Although OpenConnection() successful when I connect to localhost on same port.虽然当我连接到同一端口上的本地主机时 OpenConnection() 成功。

异常图像入站访问 AWS 图片出站访问 AWS 图片

Try this method:试试这个方法:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace MVP9App.Models
{
  public class Helptest
  {
    public static string GetRDSConnectionString()
    {
      var appConfig = ConfigurationManager.AppSettings;

      string dbname = appConfig["RDS_DB_NAME"];

      if (string.IsNullOrEmpty(dbname)) return null;

      string username = appConfig["<<RDSUSERNAME>>"];
      string password = appConfig["<<RDSPASSWORD>>"];
      string hostname = appConfig["<<RDSHOSTNAME>>"];
      string port = appConfig["<<RDSPORT>>"];

      return "Data Source=" + hostname + ";Initial Catalog=" + dbname + ";User ID=" + username + ";Password=" + password + ";";
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM