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

Configure proprietary network VPC for MongoDB database and Python3 to access MongoDB database

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces in detail the configuration of proprietary network VPC for MongoDB database and the method of Python3 to access MongoDB database. The sample code in this article is very detailed, which can be referenced by interested friends.

Public allocation

Create a proprietary network VPC

Log into the VPC console. Create a security group

Create a new security group in the Security Group console, click create Security Group, set the security group name, select the private network for the network type, and select the private network you just created.

After creating and configuring a MongoDB instance, click whitelist settings in the navigation bar on the left side of the instance information page.

Click modify to the right of the default area box.

In the pop-up dialog box, configure the network segment address of the VPC network where the function calculation is located in the whitelist input box.

Log in to the VPC console, find the VPC where the application is located in the list of proprietary networks, and click the name of the VPC to enter the private network details page. Copy the IPv4 network segment of the VPC where the application is located.

Paste the IPv4 segment address of the VPC in the whitelist settings box in the group, and then click OK.

Finally, access the MongoDB database host as the private network address of the instance, and log in to the Ali Cloud console to view

The function calculation configuration VPC creates a service in the function calculation console. For the steps to create a service, please refer to the "add, delete, modify" option of "proprietary Network configuration" in the article, and select the VPC network, switch and security group you created in step 1. In the "permission configuration" option, select "New role", click "Click Authorization", and then click "agree to Authorization" on the role Quick creation page. The operation of this step is to grant the function permission to operate on the ENI. To calculate the permissions required to access the resources in the VPC, please refer to the article configuring the function to calculate the click to access the resources in the VPC. The new service is completed.

# function writing and debugging

The following demonstrates the creation of a function example:

Create a directory to store the code and dependency modules, and create a new template.yml file in that directory, such as / tmp/code/template.yml The content is: ROSTemplateFormatVersion: '2015-09-01'Transform:' Aliyun::Serverless-2018-04-03'Resources:MongoDB-test: Type: 'Aliyun::Serverless::Service' Properties: Description: This is MongoDB service Role:' acs:ram::XXXX:role/fc-public-test' LogConfig: Project: XXX Logstore: XXX VpcConfig: VpcId: vpc-XXXX VSwitchIds:-vsw-XXX SecurityGroupId : sg-XXXX InternetAccess: true python-test: Type: 'Aliyun::Serverless::Function' Properties: Handler:' index.handler' Runtime: python3 Timeout: 10 MemorySize: 128 CodeUri:'. / 'create a Funfile file in this directory as follows: RUNTIME python3RUN fun-install pip install pymongo

Execute the fun install command to install dependencies:

$fun installusing template: template.ymlstart installing function dependencies without docker installation process. Install Success

Create a new code file under the root of the function, such as / tmp/code/index.py, and use pymongo in the code:

#-*-coding: utf-8-*-import uuidfrom pymongo import MongoClientdef handler (event, context): CONN_ADDR1 = 'dds-XXX.mongodb.rds.aliyuncs.com:3717' CONN_ADDR2 =' dds-XXXX.mongodb.rds.aliyuncs.com:3717' REPLICAT_SET = 'XXX' username =' XXX' password = 'XXXX' # get mongoclient client = MongoClient ([CONN_ADDR1, CONN_ADDR2], replicaSet=REPLICAT_SET) # authorization. The user here is based on the admin database authorization client.admin.authenticate (username, password) # use the collection:testColl of the test database as an example, insert doc, and then look up demo_name = 'python-' + str (uuid.uuid1 ()) print (' demo_name:'+ demo_name) doc = dict (DEMO=demo_name) based on the DEMO name MESG= "Hello ApsaraDB For MongoDB") doc_id = client.test.testColl.insert (doc) for d in client.test.testColl.find (dict (DEMO=demo_name)): print ('find documents:'+ str (d)) return' success'

Deploy using the fun tool:

$fun deployusing template: template.ymlusing region: cn-hangzhouusing accountId: * 3743using accessKeyId: * Ptgkusing timeout: 60 deployment process. Function python-test deploy successservice MongoDB-test deploy success

Log in to the console to see that the relevant services and functions have been created successfully, and the correct results can be returned by triggering the execution.

The above is the method of configuring proprietary network VPC for MongoDB database and Python3 to access MongoDB database. If you want to know more about it, 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

Servers

Wechat

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

12
Report