How to connect database with different credential dynamically for different server in production level?

非 Y 不嫁゛ 提交于 2019-12-11 14:59:41

问题


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.


回答1:


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.

you can load credenials by process.env.CREDENTIALS_KEY

Example

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

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,


来源:https://stackoverflow.com/questions/55904884/how-to-connect-database-with-different-credential-dynamically-for-different-serv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!