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

Function calculates how to access the SQL Server database

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article is to share with you about function calculation how to access the SQL Server database, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

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 SQL Server 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 SQL Server 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 SQL Server instances

To create an instance of SQL Server version of cloud database that meets business needs, please refer to SQL Server version of cloud database.

Note: to create a cloud database SQL Server 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 data Security in the navigation bar on the left side of the instance information page.

Click add whitelist grouping.

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 SQL Server database host as the private network address of the instance, and log in to the Aliyun console to view:

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 creation of an example of Python3 and php7.2 development language accessing SQL Server database functions:

Use the Fun tool to install dependencies and project deployments under the directory where code and dependency modules are created.

Python3

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: SQL-Server-test: Type: 'Aliyun::Serverless::Service' Properties: Description: This is SQL-Server service Role:' acs:ram::xxxxx:role/fc-public-test' LogConfig: Project: xxx Logstore: xxx VpcConfig: VpcId: vpc-xxx VSwitchIds:-vsw-xxx SecurityGroupId: sg-xxx 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 python3 RUN fun-install pip install pymssql

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, for example, / tmp/code/index.py:

#-*-coding: utf-8-*-import pymssql def handler (event, context): conn = pymssql.connect (host='rm-xxx.sqlserver.rds.aliyuncs.com', user='xxx Password='xxx', database='xxx' Charset='utf8') cursor = conn.cursor () cursor.execute ('SELECT * FROM inventory WHERE quantity > 152') result =''for row in cursor: result + =' row =% r\ n'% (row ) conn.close () return result

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 SQL-Server-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.

PHP7.2

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: SQL-Server-test: Type: 'Aliyun::Serverless::Service' Properties: Description: This is SQL-Server service Role:' acs:ram::xxx:role/fc-public-test' LogConfig: Project: xxx Logstore: xxx VpcConfig: VpcId: vpc-xxx VSwitchIds:-vsw-x'x'x'x SecurityGroupId: sg-xxx InternetAccess: true php-test: Type: 'Aliyun::Serverless::Function' Properties: Handler:' index.handler' Runtime: php7.2 Timeout: 10 MemorySize: 128 CodeUri:'. / 'EnvironmentVariables: ODBCINI: / code/.fun/root/etc/odbc.ini ODBCSYSINI: / code/.fun/root/opt/microsoft/msodbcsql17/etc

Create a Funfile file in this directory as follows:

RUNTIME php7.2 RUN apt-get update & & apt-get install-y apt-transport-https apt-utils RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add-RUN curl https://packages.microsoft.com/config/debian/8/prod.list > / etc/apt/sources.list.d/mssql-release.list RUN fun-install apt-get install unixodbc-dev RUN fun-install apt-get install msodbcsql17

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, for example, / tmp/code/index.php:

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