國內首款 Serverless MySQL 數據庫!

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"騰訊雲重磅發佈了自研雲原生數據庫 "},{"type":"text","marks":[{"type":"strong"}],"text":"TDSQL-C Serverless "},{"type":"text","text":"(原 CynosDB Serverless),這是國內首款計算和存儲全 Serverless 架構的"},{"type":"text","marks":[{"type":"strong"}],"text":"雲原生 MySQL"},{"type":"text","text":", TDSQL-C Serverless能夠讓企業用戶像使用水、電、煤一樣使用雲數據庫,用戶不需爲數據庫的閒時進行付費,而是按照數據庫資源響應單元實際使用量進行計費,將騰訊云云原生技術普惠用戶。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作爲 Serverless 生態中的重要一環,TDSQL-C Serverless 的發佈也補齊了 Serverless 架構裏 MySQL 數據庫的空缺。因此,Serverless Framework 同步發佈了 TDSQL-C Serverless 組件。這樣,騰訊雲就擁有了一套Serverless 全棧的核心解決方案,包括 Serverless API網關、騰訊雲的 Serverless 函數計算服務、Serverless 對象存儲和數據庫。在數據存儲層可以通過 TDSQL-C Serverless 直接對數據庫連接並進行 SQL 操作。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"TDSQL-C Serverless"},{"type":"text","text":"(MySQL 數據庫),具備完全自動化的無感擴容能力,能夠隨着用戶業務的請求數的增加和減少,智能化“膨脹”和“縮小”,實現資源的自動“吞吐”。從此以後,開發者可以真正的完成 Serverless 架構應用的開發,專注於業務本身,免除運維,按需付費,不用不付費,享受 Serverless 架構帶來的衆多優勢。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"服務特性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"自動駕駛(Autopilot):"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據庫根據業務負載自動啓動停止,無感擴縮容,擴縮容過程不會斷開連接。"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"按使用計費(Utility Pricing):"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"按實際使用的計算和存儲量計費,不用不付費,按秒計量,按小時結算。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"適用場景"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"開發、測試環境等低頻數據庫使用場景。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"物聯網(IoT)、邊緣計算等不確定負載場景。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"中小企業建站等 SaaS 應用場景。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面的教程將以 Node.js 開發語言的函數,指導您如何快速創建 TDSQL-C Serverless MySQL 實例,並在雲函數中進行調用:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"操作步驟"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"配置環境變量"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"配置私有網絡:"},{"type":"text","text":" 通過 Serverless Framework VPC 組件 創建 VPC 和 子網,支持雲函數和數據庫的網絡打通和使用。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"配置 Serverless DB:"},{"type":"text","text":" 通過 Serverless Framework Cynosdb 組件 創建 MySQL 實例,爲雲函數項目提供數據庫服務。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"編寫業務代碼:"},{"type":"text","text":" 通過 Serverless DB SDK 調用數據庫,雲函數支持直接調用 Serverless DB SDK,連接 PostgreSQL 數據庫進行管理操作。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"部署應用:"},{"type":"text","text":" 通過 Serverless Framework 部署項目至雲端,並通過雲函數控制檯進行測試。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"移除項目:"},{"type":"text","text":" 可通過 Serverless Framework 移除項目。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"1. 配置環境變量"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在本地建立目錄,用於存放代碼及依賴模塊。本文以  "},{"type":"codeinline","content":[{"type":"text","text":"test-MySQL"}]},{"type":"text","text":" 文件夾爲例。"}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"mkdir test-MySQL && cd test-MySQL"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於目前 TDSQL-C Serverless 只支持 "},{"type":"codeinline","content":[{"type":"text","text":"ap-beijing-3"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":"ap-guangzhou-4"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":"ap-shanghai-2"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"ap-nanjing-1"}]},{"type":"text","text":" 四個區域,所以這裏還需要配置下,只需要在項目根目錄下創建 "},{"type":"codeinline","content":[{"type":"text","text":".env"}]},{"type":"text","text":" 文件,然後配置 "},{"type":"codeinline","content":[{"type":"text","text":"REGION"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"ZONE"}]},{"type":"text","text":" 兩個環境變量:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# .env\nREGION=xxx \nZONE=xxx"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"2. 配置私有網絡"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 "},{"type":"codeinline","content":[{"type":"text","text":"test-MySQL"}]},{"type":"text","text":" 目錄下創建文件夾 "},{"type":"codeinline","content":[{"type":"text","text":"VPC"}]},{"type":"text","text":"。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"mkdir VPC && cd VPC\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同時在 "},{"type":"codeinline","content":[{"type":"text","text":"VPC"}]},{"type":"text","text":" 中新建 serverless.yml 文件,使用 VPC 組件完成私有網絡和子網的創建。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"serverless.yml"}]},{"type":"text","text":" 示例內容如下,全量配置參考產品文檔"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"#serverless.yml\norg:mysql-app\napp:mysql-app\nstage:dev\ncomponent:vpc# (required) name of the component. In that case, it's vpc.\nname:mysql-app-vpc# (required) name of your vpc component instance.\ninputs:\n region:${env:REGION}\n zone:${env:ZONE}\n vpcName:serverless-mysql\n subnetName:serverless-mysql"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"3. 配置 Serverless DB"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 "},{"type":"codeinline","content":[{"type":"text","text":"test-MySQL"}]},{"type":"text","text":" 下創建文件夾 "},{"type":"codeinline","content":[{"type":"text","text":"DB"}]},{"type":"text","text":",並在 "},{"type":"codeinline","content":[{"type":"text","text":"DB"}]},{"type":"text","text":" 文件夾下新建 "},{"type":"codeinline","content":[{"type":"text","text":"serverless.yml"}]},{"type":"text","text":" 文件,並輸入以下內容,通過 Serverless Framework 組件完成雲開發環境配置。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"serverless.yml"}]},{"type":"text","text":" 示例內容如下,全量配置參考產品文檔"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"# serverless.yml \norg:mysql-app\napp:mysql-app\nstage:dev\ncomponent:cynosdb\nname:mysql-app-db\ninputs:\n region:${env:REGION}\n zone:${env:ZONE}\n vpcConfig:\n vpcId:${output:${stage}:${app}:mysql-app-vpc.vpcId}\n subnetId:${output:${stage}:${app}:mysql-app-vpc.subnetId}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"4. 編寫業務代碼與配置文件"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 "},{"type":"codeinline","content":[{"type":"text","text":"test-MySQL"}]},{"type":"text","text":" 下創建文件夾 "},{"type":"codeinline","content":[{"type":"text","text":"src"}]},{"type":"text","text":",用於存放業務邏輯代碼和相關依賴項。並在 "},{"type":"codeinline","content":[{"type":"text","text":"src"}]},{"type":"text","text":" 文件夾下創建文件 "},{"type":"codeinline","content":[{"type":"text","text":"index.js"}]},{"type":"text","text":",輸入如下示例代碼。在函數中通過 SDK 連接數據庫,並在其中完成 MySQL 數據庫的調用。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"exports.main_handler = async (event, context, callback) => {\n var mysql = require('mysql2');\n var connection = mysql.createConnection({\n host : process.env.HOST,\n user : 'root',\n password : process.env.PASSWORD\n });\n connection.connect();\n connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {\n if (error) throw error;\n console.log('The solution is: ', results[0].solution);\n });\n connection.end();\n }\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝所需依賴模塊:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"npm install mysql2"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"完成業務代碼編寫和依賴安裝後,創建 "},{"type":"codeinline","content":[{"type":"text","text":"serverless.yml"}]},{"type":"text","text":" 文件,示例文件如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"org:mysql-app\napp:mysql-app\nstage:dev\ncomponent:scf\nname:mysql-app-scf\n\ninputs:\n src:.\/\n functionName:${name}\n region:${env:REGION}\n runtime:Nodejs10.15\n timeout:30\n vpcConfig:\n vpcId:${output:${stage}:${app}:mysql-app-vpc.vpcId}\n subnetId:${output:${stage}:${app}:mysql-app-vpc.subnetId}\n environment:\n variables:\n HOST:${output:${stage}:${app}:mysql-app-db.connection.ip}\n PASSWORD:${output:${stage}:${app}:mysql-app-db.adminPassword}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"5. 快速部署"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"完成創建後,項目目錄結構如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" .\/test-MySQL\n ├── vpc\n │ └── serverless.yml # vpc 配置文件\n ├── db\n │ └── serverless.yml # db 配置文件\n ├── src\n │ ├── serverless.yml # scf 組件配置文件\n │ ├── node_modules # 項目依賴文件\n │ └── index.js # 入口函數\n └── .env # 環境變量文件\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用命令行在 "},{"type":"codeinline","content":[{"type":"text","text":"test-MySQL"}]},{"type":"text","text":" 下,執行以下命令進行部署。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sls deploy"}]},{"type":"blockquote","content":[{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"部署時需要掃碼授權,如果沒有騰訊雲賬號,請 註冊新賬號。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"如果是子賬號,請參考子賬號權限配置完成授權"}]}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"返回結果如下所示,即爲部署成功。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"mysql-app-vpc: \n region: xxx\n zone: xxx\n vpcId: xxxx-xxx\n ...\n\nmysql-app-db: \n dbMode: xxxx\n region: xxxx\n zone: xxxx\n ...\n\nmysql-app-scf: \n functionName: xxxx\n description: xxx\n ...\n\n59s › test-MySQL › \"deploy\" ran for 3 apps successfully.\n\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"部署成功後,您可通過 雲函數控制檯,查看並進行函數調試,測試成功如下圖所示:"}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/da\/dad5993ad091a45bcb2bdf93a11fac88.png","alt":"Image","title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"移除項目"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 "},{"type":"codeinline","content":[{"type":"text","text":"test-MySQL"}]},{"type":"text","text":" 目錄下,執行以下命令可移除項目。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sls remove\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"返回如下結果,即爲成功移除。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"serverless ⚡ framework\n4s › test-MySQL › Success\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了通過組件一鍵創建所有資源外,您也可以通過控制檯完成 Serverless 版本 MySQL 數據庫的創建,並在雲函數中正常使用 SDK 的方式完成調用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"頭圖:Unsplash"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者:April"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文:https:\/\/mp.weixin.qq.com\/s\/lUHHsF_fDAccpuavvkICvw"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文:國內首款 Serverless MySQL 數據庫重磅發佈,免費試用!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來源:TencentServerless - 微信公衆號 [ID:ServerlessGo]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"轉載:著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章