Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to correctly set the configuration parameters of mongodb and redis development environment and production environment

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to correctly set the configuration parameters of the mongodb and redis development environment and production environment". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to correctly set the configuration parameters of the mongodb and redis development environment and production environment" can help you solve your doubts.

When we write code, we usually develop it on our own computers, and then deploy the code to the server. If a piece of code involves reading and writing a database, or accessing some other online service interface, then during development, in order not to affect the online environment, we generally separate the database of the test environment from the database of the online environment.

For example, our program needs to access MongoDB and Redis, so in the code, we might write:

Import pymongo

Import redis

Handler = pymongo.MongoClient ('mongodb://username:password@127.0.0.1:27017'). Db.col

Client = redis.Redis (host='127.0.0.1', port=6379, password='xxxx')

When you want to deploy the program to the online environment, you manually change the MongoDB connection parameters and Redis connection parameters in the code to the parameters of the online environment. Then submit the code to Git, drop the latest code on the server and deploy it.

However, when you want to modify a new feature and retest it, you have to change these connection parameters to the parameters of the test environment on your computer. If you forget to modify it and run it directly, you may write dirty data to the online environment.

As a result, someone may use environment variables to control the parameters read, such as:

Import os

Import redis

Import pymongo

If os.getenv ('env',' prod'): # online environment

MONGODB_URI = 'mongodb://username:password@xx.xx.xx.xx:27017'

REDIS_PARAMS = {'host':' xx.xx.xx.xx', 'port': 6379,' password': 'xxxx'}

Else: # Test environment

MONGODB_URI = 'mongodb://username:password@127.0.0.1:27017'

REDIS_PARAMS = {'host':' 127.0.0.1, 'port': 6379,' password': 'xxxx'}

Handler = pymongo.MongoClient (MONGODB_URI) .db.col

Client = redis.Redis (* * REDIS_PARAMS)

In this way, you do not need to manually modify the database connection parameters, as long as the online environment environment variable env is set to prod, then the program deployed to the online environment, it will automatically use the online database parameters. As long as other places, such as your computer, the environment variable env is not prod or simply does not exist, then the development environment parameters will be used automatically.

This does avoid the problem of forgetting to change the parameters, but there is another problem: if others have access to this Git source, they will know how to connect to the database in the online environment. Even manipulate the data of the online environment without authorization, resulting in security risks or privacy disclosure.

Therefore, it is safer to use a file to store these configuration parameters, and the program goes to this fixed location to read the parameters. Online environment this file puts online parameters, development environment, this file writes development parameters. This configuration file is not uploaded to Git.

For example, let's create a config.json file with the following contents:

{

"MONGODB_URI": "mongodb://username:password@127.0.0.1:27017"

"REDIS_PARAMS": {"host": "127.0.0.1", "port": 6379, "password": "xxxx"}

}

Then our code is modified like this:

Import os

Import json

Import redis

Import pymongo

CONFIG_PATH ='/ etc/config/config.json'

If not os.path.exists (CONFIG_PATH):

Print ('profile does not exist, automatically use test environment parameters!')

MONGODB_URI = 'mongodb://username:password@127.0.0.1:27017'

REDIS_PARAMS = {'host':' 127.0.0.1, 'port': 6379,' password': 'xxxx'}

Else:

With open (CONFIG_PATH, encoding='utf-8') as f:

Config = json.load (f)

MONGODB_URI = config ['MONGODB_URI']

REDIS_PARAMS = config ["REDIS_PARAMS"]

Handler = pymongo.MongoClient (MONGODB_URI) .db.col

Client = redis.Redis (* * REDIS_PARAMS) read here, this article "how to correctly set the configuration parameters of mongodb and redis development environment and production environment" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report