Home > Database > Redis > body text

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

王林
Release: 2023-06-03 20:04:15
forward
1296 people have browsed it

When we write code, we usually develop it on our own computer first, 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 interfaces, then during development, in order not to affect the online environment, we usually separate the database in the test environment and the database in the online environment.

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

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')
Copy after login

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, pull the latest code on the server and deploy it.

However, when you want to modify a new function and test it again, you need to modify these connection parameters to the parameters of the test environment and operate on your own computer. If you forget to modify and run directly, dirty data may be written to the online environment.

So, someone may use environment variables to control the parameters read, for example:

import os
import redis
import pymongo

if os.getenv('env', 'prod'):  # 线上环境 
    MONGODB_URI = 'mongodb://username:password@xx.xx.xx.xx:27017'
    REDIS_PARAMS = {'host': 'xx.xx.xx.xx', 'port': 6379, 'password': 'xxxx'}
else:  # 测试环境
    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)
Copy after login

In this way, you don’t need to manually modify the connection parameters of the database. Just set the environment variable env of the online environment to prod, and then the program will be deployed to the online environment. It automatically uses the parameters of the online database. As long as the environment variable env is not prod elsewhere, such as on your computer, or the environment variable simply does not exist, the parameters of the development environment will be automatically used.

Doing this does avoid problems caused by forgetting to modify parameters, but there is another problem: if other people also have access to this Git source, then they will know how to connect to the database in the online environment. They even manipulate data in the online environment without authorization, causing security risks or privacy leaks.

In order to be more secure, you can use a special file to store the configuration parameters, and the program reads the parameters from the file. The online environment file contains online parameters, and the development environment file contains development parameters. This configuration file is not uploaded to Git.

For example, we create a config.json file whose content is:

{
    "MONGODB_URI": "mongodb://username:password@127.0.0.1:27017",
    "REDIS_PARAMS": {"host": "127.0.0.1", "port": 6379, "password": "xxxx"}
}
Copy after login

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('配置文件不存在,自动使用测试环境参数!')
    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)
Copy after login

The above is the detailed content of How to correctly set the configuration parameters of mongodb and redis development environment and production environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!