.NET Core AWS RDS 连接 [英] .NET Core AWS RDS CONNECTION

查看:31
本文介绍了.NET Core AWS RDS 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Amazons AWS Elastic Beanstalk 上创建 .NET Core Web API.我正在尝试添加数据库,但他们添加数据库的指南不适用于 .Net Corehttp://docs.aws.amazon.com/elasticbeanstalk/最新/dg/create_deploy_NET.rds.html

I am creating a .NET Core Web API on Amazons AWS, Elastic Beanstalk. I am trying to add a database, but their guide to add a database does not work for .Net Core http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_NET.rds.html

它说使用​​ConfigurationManager.AppSettings;"获取相关信息,但这在.NET Core中是不可能的.

It says to get the relevant information using "ConfigurationManager.AppSettings;", but this is not possible in .NET Core.

谁能提供一些有关如何获取数据库信息的信息?(RDS_DB_NAME"RDS_USERNAME"RDS_PASSWORD"RDS_HOSTNAME")

Can anybody give some inforamtion about how to get the database informations? ( "RDS_DB_NAME" "RDS_USERNAME" "RDS_PASSWORD" "RDS_HOSTNAME" )

更新我试图阅读 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration但我还没有解决这个问题.我似乎仍然无法从 AWS 获取值.

UPDATE I tried to read on https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration But I have not resolved the problem. I still cannot seem to get the values from AWS.

它只返回我在自己的 appsettings.json 中设置的任何内容这是我的代码:MyOptions.cs

It just returns whatever I set in my own appsettings.json Here is my code: MyOptions.cs

public class MyOptions
{
    public MyOptions()
    {
        // Set default value.
    }
    public string RDS_HOSTNAME { get; set; }
    public string RDS_PORT { get; set; }
    public string RDS_DB_NAME { get; set; }
    public string RDS_USERNAME { get; set; }
    public string RDS_PASSWORD { get; set; }
}

StartUp.cs

public void ConfigureServices(IServiceCollection services)
{
    // Register the IConfiguration instance which MyOptions binds against.
    services.Configure<MyOptions>(Configuration);
    // Add framework services.
    services.AddApplicationInsightsTelemetry(Configuration);

    services.AddMvc();
}

HomeController.cs

HomeController.cs

namespace WebApplication2.Controllers
{
    [Route("")]
    public class HomeController : Controller
    {
        private readonly MyOptions _options;

        public HomeController(IOptions<MyOptions> optionsAccessor)
        {
            _options = optionsAccessor.Value;
        }

        [HttpGet]
        public IActionResult Index()
        {
            var RDS_DB_NAME = _options.RDS_DB_NAME;
            var RDS_HOSTNAME = _options.RDS_HOSTNAME;
            var RDS_PASSWORD = _options.RDS_PASSWORD;
            var RDS_PORT = _options.RDS_PORT;
            var RDS_USERNAME = _options.RDS_USERNAME;
            return Content($"RDS_DB_NAME = {RDS_DB_NAME}, RDS_HOSTNAME = {RDS_HOSTNAME}, RDS_PASSWORD = { RDS_PASSWORD}, RDS_PORT = {RDS_PORT}, RDS_USERNAME = { RDS_USERNAME}");
        }
    }
}

推荐答案

我遇到了这个确切的问题,它比我预期的要复杂得多.

I ran into this exact problem and it was MUCH more complicated than I anticipated.

步骤 1 - 我从另一个 Stack Overflow 问题修改了这个答案.我在 Startup.cs 中的代码如下所示:

Step 1 - I modified this answer from another Stack Overflow question. My code in Startup.cs looked like this:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
        .AddJsonFile(@"C:Program FilesAmazonElasticBeanstalkconfigcontainerconfiguration", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables();

    // This adds EB environment variables.
    builder.AddInMemoryCollection(GetAwsDbConfig(builder.Build()));

    Configuration = builder.Build();
}

private Dictionary<string, string> GetAwsDbConfig(IConfiguration configuration)
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();

        foreach (IConfigurationSection pair in configuration.GetSection("iis:env").GetChildren())
        {
            string[] keypair = pair.Value.Split(new[] { '=' }, 2);
            dict.Add(keypair[0], keypair[1]);
        }

        return dict;
    }

private string GetRdsConnectionString()
{
    string hostname = Configuration.GetValue<string>("RDS_HOSTNAME");
    string port = Configuration.GetValue<string>("RDS_PORT");
    string dbname = Configuration.GetValue<string>("RDS_DB_NAME");
    string username = Configuration.GetValue<string>("RDS_USERNAME");
    string password = Configuration.GetValue<string>("RDS_PASSWORD");

    return $"Data Source={hostname},{port};Initial Catalog={dbname};User ID={username};Password={password};";
}

第 2 步 - 您需要转到 AWS 控制台中的 RDS 服务.选择要连接的实例 -> 实例操作 -> 查看详细信息.您将能够找到 RDS_HOSTNAME(端点)和 RDS_PORT(端口).

Step 2 - You will need to go to the RDS Service in the AWS Console. Select the Instance that you want to connect to -> Instance Actions -> See Details. You will be able to find RDS_HOSTNAME (endpoint) and RDS_PORT (port).

RDS_DB_NAME 是您的代码要使用的数据库名称.

RDS_DB_NAME is the Database name that your code is meant to work with.

RDS_USERNAME 和 RDS_PASSWORD 是您在 Elastic Beanstalk 中创建数据库时使用的主用户名和密码.如果您转到 Elastic Beanstalk -> 配置 -> 数据层 ->(单击齿轮),您会看到您的主用户名,如果您忘记了密码,则可以选择更改密码.

RDS_USERNAME and RDS_PASSWORD are the master usernames and passwords you used when creating the database in Elastic Beanstalk. If you go to Elastic Beanstalk -> Configuration -> Data Tier -> (Click on the Gear) you see your Master username and have the option to change your Password if you have forgotten it.

第 3 步 - 现在您已拥有所有数据,您必须在 Elastic Beanstalk 中输入这些选项作为环境属性.转到 Elastic Beanstalk -> 软件配置 ->(单击齿轮).此页面底部是环境属性.您需要在此处输入第一步中代码中的名称以及第二步中 RDS 中的值.

Step 3 - Now that you have all the data, you will have to enter these options as Environmental Properties in Elastic Beanstalk. Go to Elastic Beanstalk -> Software Configuration -> (Click on the Gear). At the bottom of this page are the Environment Properties. Here is where you will want to enter the names in your code from step one and the values from RDS in step two.

有关 AWS 文档中的更多信息,请查看 这里这里.

For more information on this from AWS documentation check here and here.

这篇关于.NET Core AWS RDS 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆