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

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

问题描述

我正在Amazon AWS,Elastic Beanstalk上创建.NET Core Web API。
我正在尝试添加数据库,但是他们的添加数据库指南不适用于.Net Core
http://docs.aws.amazon.com/elasticbeanstalk/latest/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
但我尚未解决问题。

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中设置的内容。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

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 -我修改了此答案来自另一个堆栈溢出问题。我在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 Files\Amazon\ElasticBeanstalk\config\containerconfiguration", 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->软件配置->(单击Gear)。此页面底部是环境属性。在此处,您将要在步骤1的代码中输入名称,在步骤2的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文档中有关此内容的更多信息,请检查< a href = http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.db.html rel = nofollow noreferrer>此处和此处

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

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

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