如何在生产级别为不同服务器动态连接具有不同凭证的数据库? [英] How to connect database with different credential dynamically for different server in production level?

查看:87
本文介绍了如何在生产级别为不同服务器动态连接具有不同凭证的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接用于不同服务器(例如dev,staging,live,production)的数据库.所有服务器都有不同的数据库和凭据.调用服务器时,它将仅访问该特定数据库的数据. 在我的代码中,我使用带有sequelize的节点js进行数据库连接以在GraphQL中构建API,该怎么做?我已经尝试了很多.请给我建议.如果需要代码,我可以在这里发布.

I want to connect the databases which are for the different servers (like dev, staging, live, production). There are different databases and credential for all the server. When a server will be called it will access data only that particular database. In my code, I am using node js with sequelize for the database connection to build the API in GraphQL.How can I do this? I have already tried a lot. Please suggest to me. If need code I can post it here.

推荐答案

执行此操作的最佳方法是在每个服务器上保留一个单独的.env文件.并使用 dotenv 模块加载凭据表单.env文件本身.

The best way to do this is to keep a seperate .env file on each server. and load the credentials form .env file itself by using dotenv module.

您可以通过process.env.CREDENTIALS_KEY

示例

DB_USERNAME=username
DB_PASSWPRD=password
DB_DATABASE=db_name
DB_HOST=localhost

像这样在您的js/ts文件中加载

Load in your js/ts file like:

    "USERNAME": process.env.DB_USERNAME,
    "PASSWORD": process.env.DB_PASSWPRD,
    "DATABASE": process.env.DB_DATABASE,
    "HOST": process.env.DB_HOST,

这篇关于如何在生产级别为不同服务器动态连接具有不同凭证的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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