本篇文章給大家分享的是有關(guān)函數(shù)計(jì)算如何訪問SQL Server數(shù)據(jù)庫(kù),小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來(lái)看看吧。
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的秀英網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
函數(shù)計(jì)算(Function Compute):函數(shù)計(jì)算 是事件驅(qū)動(dòng)的全托管計(jì)算服務(wù)。使用函數(shù)計(jì)算,您無(wú)需采購(gòu)與管理服務(wù)器等基礎(chǔ)設(shè)施,只需編寫并上傳代碼。函數(shù)計(jì)算為您準(zhǔn)備好計(jì)算資源,彈性地可靠地運(yùn)行任務(wù),并提供日志查詢、性能監(jiān)控和報(bào)警等功能。借助函數(shù)計(jì)算,您可以快速構(gòu)建任何類型的應(yīng)用和服務(wù),并且只需為任務(wù)實(shí)際消耗的資源付費(fèi)。
訪問 SQL Server 數(shù)據(jù)庫(kù)是指在函數(shù)計(jì)算中通過編寫代碼調(diào)用數(shù)據(jù)庫(kù)驅(qū)動(dòng)庫(kù)通過 TCP 協(xié)議實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)進(jìn)行的插入、查詢等操作。通常函數(shù)計(jì)算中運(yùn)行的不同函數(shù)實(shí)例之間是不共享狀態(tài)的,對(duì)于結(jié)構(gòu)化的數(shù)據(jù)可以通過數(shù)據(jù)庫(kù)的形式進(jìn)行持久化以實(shí)現(xiàn)狀態(tài)共享。由于用戶函數(shù)運(yùn)行在函數(shù)計(jì)算的 VPC 中,而用戶的數(shù)據(jù)庫(kù)運(yùn)行在用戶所屬的 VPC 中,所以在函數(shù)計(jì)算平臺(tái)訪問數(shù)據(jù)庫(kù)會(huì)涉及到跨 VPC 訪問的場(chǎng)景,下面我們先來(lái)介紹一下其工作機(jī)制。
訪問 SQL Server 的原理、工作機(jī)制與訪問 MySQL 數(shù)據(jù)庫(kù)完全相同,本文不再重復(fù)闡述,更詳細(xì)的內(nèi)容請(qǐng)參考 訪問 Mysql 數(shù)據(jù)庫(kù) 中的工作機(jī)制章節(jié)。
登錄 VPC控制臺(tái)。
參閱 VPC搭建專有網(wǎng)絡(luò) 創(chuàng)建VPC和交換機(jī)。
在安全組控制臺(tái) 新建安全組,點(diǎn)擊 創(chuàng)建安全組,設(shè)置安全組名稱,網(wǎng)絡(luò)類型選擇 專有網(wǎng)絡(luò),并選擇剛才創(chuàng)建的專有網(wǎng)絡(luò)。
創(chuàng)建適合業(yè)務(wù)需求的云數(shù)據(jù)庫(kù) SQL Server 版實(shí)例可以參考 云數(shù)據(jù)庫(kù) SQL Server 版。
注意:創(chuàng)建云數(shù)據(jù)庫(kù) SQL Server 版實(shí)例需要選擇和函數(shù)計(jì)算配置相同的 VPC 實(shí)例,可以配置和函數(shù)計(jì)算不同的可用區(qū)的交換機(jī),因?yàn)橄嗤?VPC 實(shí)例下不同可用區(qū)交換機(jī)內(nèi)網(wǎng)是互通的。
創(chuàng)建成功后,在實(shí)例信息頁(yè)面左側(cè)的導(dǎo)航欄中單擊數(shù)據(jù)安全性。
單擊 添加白名單分組。
在彈出的對(duì)話框中,將函數(shù)計(jì)算所在的 VPC 網(wǎng)絡(luò)的網(wǎng)段地址配置在白名單輸入框中。
登錄 VPC 控制臺(tái),在專有網(wǎng)絡(luò)列表中找到應(yīng)用所在的 VPC,單擊該 VPC 的名稱進(jìn)入專有網(wǎng)絡(luò)詳情頁(yè)面。
復(fù)制應(yīng)用所在的 VPC 的 IPv4 網(wǎng)段。
在組內(nèi)白名單設(shè)置框中粘貼該 VPC 的 IPv4 網(wǎng)段地址,然后單擊確定。
最后訪問 SQL Server 數(shù)據(jù)庫(kù) host 為實(shí)例的內(nèi)網(wǎng)地址,可以登錄阿里云控制臺(tái)查看:
注意:函數(shù)計(jì)算服務(wù)所在區(qū)域與公共配置中創(chuàng)建的資源所在區(qū)域一致。
在 函數(shù)計(jì)算控制臺(tái) 創(chuàng)建服務(wù)。
創(chuàng)建服務(wù)步驟請(qǐng)參考文章 服務(wù)的增刪改查
【專有網(wǎng)絡(luò)配置】選項(xiàng)中,選擇您在步驟一中創(chuàng)建的 VPC 網(wǎng)絡(luò),交換機(jī)、安全組。
【權(quán)限配置】選項(xiàng)中,選擇【新建角色】,點(diǎn)擊【點(diǎn)擊授權(quán)】,在角色快速創(chuàng)建頁(yè)面,點(diǎn)擊【同意授權(quán)】。
這步的操作是授予函數(shù)計(jì)算對(duì) ENI 的操作權(quán)限,函數(shù)計(jì)算訪問 VPC 中資源需要的權(quán)限請(qǐng)參考文章 配置函數(shù)計(jì)算訪問 VPC 內(nèi)的資源
點(diǎn)擊確定,新建服務(wù)完畢。
###函數(shù)編寫與調(diào)試 下面演示 Python3 與 php7.2 開發(fā)語(yǔ)言訪問 SQL Server 數(shù)據(jù)庫(kù)函數(shù)示例創(chuàng)建:
使用 Fun 工具在建立存放代碼和依賴模塊目錄下安裝依賴和項(xiàng)目部署。
建立一個(gè)目錄,用于存放代碼和依賴模塊,在該目錄下新建 template.yml 文件,例如 /tmp/code/template.yml,內(nèi)容為:
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: './'
在該目錄下創(chuàng)建 Funfile 文件內(nèi)容為:
RUNTIME python3 RUN fun-install pip install pymssql
執(zhí)行fun install
命令安裝依賴:
$ fun install using template: template.yml start installing function dependencies without docker 安裝過程。。。。 Install Success
在函數(shù)根目錄下新建代碼文件,例如 /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
使用 fun 工具部署:
$ fun deploy using template: template.yml using region: cn-hangzhou using accountId: ***********3743 using accessKeyId: ***********Ptgk using timeout: 60 部署過程。。。 function python-test deploy success service SQL-Server-test deploy success
登錄控制臺(tái),即可看到相關(guān)的服務(wù)、函數(shù)被創(chuàng)建成功,且觸發(fā)執(zhí)行可以返回正確的結(jié)果。
建立一個(gè)目錄,用于存放代碼和依賴模塊,在該目錄下新建 template.yml 文件,例如 /tmp/code/template.yml,內(nèi)容為:
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
在該目錄下創(chuàng)建 Funfile 文件內(nèi)容為:
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
執(zhí)行fun install
命令安裝依賴:
$ fun install using template: template.yml start installing function dependencies without docker 安裝過程。。。。 Install Success
在函數(shù)根目錄下新建代碼文件,例如 /tmp/code/index.php:
<?php function handler($event, $context) { try { $conn = new PDO("sqlsrv:Server=rm-xxx.sqlserver.rds.aliyuncs.com;Database=xxx","xxx","xxx"); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $conn->query("set names utf-8"); $sql="SELECT * FROM inventory WHERE quantity > 152"; $result = $conn->prepare($sql); $result->execute(); print($result); return ("Connection successed."); } catch (PDOException $e) { return ("Connection failed: " . $e->getMessage()); } }
使用 fun 工具部署:
$ fun deploy using template: template.yml using region: cn-hangzhou using accountId: ***********3743 using accessKeyId: ***********Ptgk using timeout: 60 部署過程。。。 function php-test deploy success service SQL-Server-test deploy success
登錄控制臺(tái),即可看到相關(guān)的服務(wù)、函數(shù)被創(chuàng)建成功,且觸發(fā)執(zhí)行可以返回正確的結(jié)果。
注意事項(xiàng)
創(chuàng)建的函數(shù)需要設(shè)置兩個(gè)環(huán)境變量: ODBCINI: /code/.fun/root/etc/odbc.ini ODBCSYSINI: /code/.fun/root/opt/microsoft/msodbcsql17/etc
執(zhí)行fun install
命令安裝依賴后,修改 .fun/root/etc/odbc.ini 文件中 Driver 值指向.fun/root/opt/microsoft/msodbcsql17/lib64/目錄下的文件,如圖!
php環(huán)境中需要使用 pdo_sqlsrv 擴(kuò)展編譯可以參考 函數(shù)計(jì)算 php runtime 編譯非內(nèi)置的擴(kuò)展 文檔最后可以下載已經(jīng)編譯好的 pdo_sqlsrv 擴(kuò)展。
通過本文介紹可以快速實(shí)現(xiàn)函數(shù)計(jì)算訪問 SQL Server 數(shù)據(jù)庫(kù)。
使用函數(shù)計(jì)算帶來(lái)的優(yōu)勢(shì):
無(wú)需采購(gòu)和管理服務(wù)器等基礎(chǔ)設(shè)施,只需專注業(yè)務(wù)邏輯的開發(fā),可以大幅縮短項(xiàng)目交付時(shí)間和人力成本;
提供日志查詢、性能監(jiān)控、報(bào)警等功能快速排查故障;
免運(yùn)維,毫秒級(jí)別彈性伸縮,快速實(shí)現(xiàn)底層擴(kuò)容以應(yīng)對(duì)峰值壓力,性能優(yōu)異;
成本極具競(jìng)爭(zhēng)力;
以上就是函數(shù)計(jì)算如何訪問SQL Server數(shù)據(jù)庫(kù),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)標(biāo)題:函數(shù)計(jì)算如何訪問SQLServer數(shù)據(jù)庫(kù)
當(dāng)前路徑:http://muchs.cn/article28/jehpjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、關(guān)鍵詞優(yōu)化、App設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司、外貿(mào)網(wǎng)站建設(shè)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)