In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to access the Mongo database by function calculation. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Functional Computing (Function Compute): functional Computing is an event-driven fully managed computing service. Using function calculations, you don't need to purchase and manage infrastructure such as servers, just write and upload code. Function computing prepares computing resources for you, runs tasks flexibly and reliably, and provides functions such as log query, performance monitoring and alarm. With function computing, you can quickly build any type of application and service, and only pay for the resources actually consumed by the task.
Accessing the MongoDB database refers to the operation of inserting and querying the database through the TCP protocol by writing code to call the database driver library. Usually, the state of different function instances running in function calculation is not shared, and the structured data can be persisted in the form of database to achieve state sharing. Since the user function runs in the VPC of function calculation, and the user's database runs in the VPC to which the user belongs, accessing the database in the function computing platform will involve cross-VPC access, so let's first introduce its working mechanism.
Working mechanism
The principle and working mechanism of accessing MongoDB is exactly the same as accessing Mysql database. I will not repeat it in this article. For more details, please refer to the working mechanism section of accessing Mysql database.
Configuration and function Writing Public configuration to create proprietary Network VPC
Log into the VPC console.
See VPC to build a proprietary network to create VPC and switches.
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.
Create and configure MongoDB instances
To create an instance of MongoDB version of cloud database that meets business needs, please refer to MongoDB version of cloud database.
Note: to create a cloud database MongoDB instance, you need to select a VPC instance with the same configuration as function calculation. You can configure and function to calculate switches in different availability zones, because the private networks of switches in different availability zones are interconnected under the same VPC instance.
After the creation is successful, 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 Aliyun console to view:
Multi-language client connection Aliyun MongoDB can refer to MongoDB client connection.
Function calculates configuration VPC
Note: the area where the function calculation service is located is the same as that of the resources created in the public configuration.
Create a service in the function calculation console.
For the steps to create a service, please refer to the article about the addition, deletion, modification and query of the service.
In the "proprietary Network configuration" option, select the VPC network, switch and security group that 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. The function calculates the permissions required to access the resources in the VPC. Please refer to the article configuring the function to calculate the access to the resources in the VPC.
Click OK to complete the new service.
Function writing and debugging
The following demonstrates the example creation of a Python3 development language access MongoDB database function:
Use the Fun tool to install dependencies and project deployments under the directory where code and dependency modules are created.
Create a directory to store the code and dependency modules, and create a new template.yml file, such as / tmp/code/template.yml, in this directory, with the following contents:
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 install using template: template.yml start 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 deploy using template: template.yml using region: cn-hangzhou using accountId: * 3743 using accessKeyId: * Ptgk using timeout: 60 deployment process. Function python-test deploy success service 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.
Through the introduction of this paper, we can quickly realize function calculation and access Mongo database.
Advantages of using function calculations:
Without the need to purchase and manage infrastructure such as servers, you only need to focus on the development of business logic, which can greatly reduce project delivery time and labor costs
Provide log query, performance monitoring, alarm and other functions for rapid troubleshooting
Operation and maintenance-free, millisecond elastic expansion, rapid bottom expansion to cope with peak pressure, excellent performance
The cost is very competitive
About the function calculation how to access the Mongo database to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.