將 MongoDB API 與 Oracle Autonomous Database on Dedicated Exadata Infrastructure 搭配使用
Oracle Database API for MongoDB 會將 MongoDB 纜線協定轉譯成由 Oracle Database 執行的 SQL 敘述句。It lets developers who have MongoDB skill sets write JSON document-store applications for Oracle Database that use drivers and tools that understand the MongoDB protocol.請參閱 Oracle Database API for MongoDB 中的 Oracle Database API for MongoDB 簡介,瞭解此 API 的詳細總覽。
啟用 MongoDB API
您可以從 OCI 主控台啟用 Autonomous Database 的 MongoDB API,或使用 Oracle REST Data Services (ORDS) 手動啟用。
若要將 MongoDB API 與 Autonomous Database 搭配使用,您必須個別安裝並設定客戶管理的 Oracle REST Data Services (ORDS),且 ORDS 版本必須為 22.3 或更新版本。請參閱 Oracle REST Data Services Installation and Configuration Guide 中的 Oracle API for MongoDB Support ,瞭解此 API 的架構概念。
- 在 Autonomous Database 詳細資訊頁面上,選取工具組態頁籤。
- 按一下編輯工具組態。
- 在 MongoDB API 資料列中,選取啟用工具資料欄以顯示已啟用。
- 按一下套用。
您也可以在啟動設定或複製執行處理時,選取顯示進階選項並選取工具頁籤來啟用 MongoDB API。
-
使用
ords install adb
命令安裝和設定 ORDS。如需詳細資訊,請參閱 Oracle REST Data Services Installation and Configuration Guide 中的 Installing and Configuring Customer Managed ORDS on Autonomous Database 。
- 建立啟用 ORDS 的使用者。
- 使用 MongoDB shell 連線至 ORDS。
如需上述步驟的示範,請參閱 Oracle REST Data Services Installation and Configuration Guide 中的 Getting Started 。
連接 MongoDB 應用程式
啟用 MongoDB API 之後,您可以建立 MongoDB 使用者並使用其連線字串連線至 Autonomous Database 。
您可以使用 MongoDB API 連線字串 MongoDB Shell (命令行公用程式) 來連線及查詢資料。
- 在 Autonomous Database 詳細資訊頁面上,選取工具組態頁籤。
- 在 MongoDB API 資料列的存取 URL 下,按一下複製。
如果您在 Exadata Cloud@Customer 上的 Autonomous Database 上使用 MongoDB API,您的網路 /DNS 團隊必須確保任何 MongoDB 連線字串主機名稱都能解析為叢集的 SCAN。若未這麼做,MongoDB 從屬端將無法連線至資料庫。您可以允許使用萬用字元 DNS 記錄連線至叢集內的所有 Autonomous Database ,或允許特定 Autonomous Database :
測試 MongoDB 連線
為 MongoDB 建立測試 Autonomous Database 使用者
您可以建立測試使用者或類似認證的使用者來進行測試。
- 開啟 Autonomous Database 的 OCI 主控台。
- 按一下資料庫動作。
- 從「資料庫動作啟動畫面」中,按一下管理 > 資料庫使用者。
- 按一下「資料庫使用者 (Database Users)」頁面上的 + 建立使用者 (Create User) 。
- 輸入測試使用者的使用者名稱與密碼。按一下 Web 存取,然後設定表格空間 DATA 的配額。
- 按一下授權的角色頁籤。
- 除了預設角色之外,請為使用者選取並新增
SODA_APP
角色。 - 按一下建立使用者。
使用命令行測試連線
- 以測試使用者身分登入。請遵循上一個範例中的步驟以取得說明。
$ mongosh --tls --tlsAllowInvalidCertificates 'mongodb://TESTUSER:<PASSWORD>@<database URL>. <OCI region>.oraclecloudapps.com:27017/admin?authMechanism=PLAIN&authSource=$external&ssl=true&loadBalanced=false' Current Mongosh Log ID: 614c9e2a01e3575c8c0b2ec7 Connecting to: mongodb://TESTUSER:<PASSWORD>@<database URL>.<OCIregion>.oraclecloudapps.com:27017/admin? authMechanism=PLAIN&authSource=$external&tls=true&loadBalanced=false Using MongoDB: 3.6.2 Using Mongosh: 1.0.7 For mongosh info see: https://docs.mongodb.com/mongodb-shell/admin > show dbs testuser 0 B >
附註:
使用 URI 百分比編碼取代連線字串 URI 中的任何保留字元 — 特別是使用者名稱和密碼中的字元。這些是保留字元及其百分比編碼:
!
#
$
%
&
'
(
)
*
+
%21
%23
%24
%25
%26
%27
%28
%29
%2A
%2B
,
/
:
;
=
?
@
[
]
%2C
%2F
%3A
%3B
%3D
%3F
%40
%5B
%5D
例如,如果您的使用者名稱是
RUTH
,而您的密碼是@least1/2#?
,則伺服器<server>
的 MongoDB 連線字串可能如下所示:'mongodb://RUTH:%40least1%2F2%23%3F@<server>:27017/ruth/ ...'
根據您使用的工具或驅動程式,您可以提供使用者名稱和密碼作為個別的參數,而不是提供 URI 連線字串的一部分。在此情況下,您可能不需要將其中包含的任何保留字元編碼。
另請參閱:
- 建立集合並將文件插入集合中。
testuser> show collections testuser> db.createCollection( 'fruit' ) { ok: 1 } testuser> show collections fruit testuser> db.fruit.insertOne( {name:"orange", count:42} ) { acknowledged: true, insertedId: ObjectId("614ca31fdab254f63e4c6b47") } testuser> db.fruit.insertOne( {name:"apple", count:12, color: "red"} ) { acknowledged: true, insertedId: ObjectId("614ca340dab254f63e4c6b48") } testuser> db.fruit.insertOne( {name:"pear", count:5} ) { acknowledged: true, insertedId: ObjectId("614ca351dab254f63e4c6b49") } testuser>
- 使用 SQL 從屬端 (例如「資料庫動作」) 查詢集合。
SELECT f.json_document.name, f.json_document.count, f.json_document.color FROM fruit f;
使用 Node.js 應用程式測試連線
-
下載 Node.js。如果您的環境已經下載或安裝 Node.js,您可以略過此步驟。
$ wget https://nodejs.org/dist/latest-v14.x/node-v14.17.5-linux-x64.tar.xz
-
擷取 Node,js 存檔的內容。如果您的環境已經下載或安裝 Node.js,您可以略過此步驟。
$ tar -xf node-v14.17.5-linux-x64.tar.xz
-
設定
PATH
環境變數。如果您的環境已經下載或安裝 Node.js,您可以略過此步驟。$ export PATH="`pwd`"/node-v14.17.5-linux-x64/bin:$PATH
-
使用 Javascript 範例測試您的連線。
-
建立新目錄。
$ mkdir autonomous_mongodb $ cd autonomous_mongodb $ npm init –y
-
安裝 Mongodb 相依性。
$ npm install mongodb
-
建立名為
connect.js
的 JavaScript 應用程式。const { MongoClient } = require("mongodb"); const uri = "mongodb://TESTUSER:<PASSWORD>@<Database URI>.<OCI region>.oraclecloudapps.com:27017/admin?authMechanism=PLAIN&authSource=$external&ssl=true&loadBalanced=false"; const client = new MongoClient(uri); async function run() { try { await client.connect(); const database = client.db('admin'); const movies = database.collection('movies'); // Insert a movie const doc = { title: 'Back to the Future', year: 1985, genres: ['Adventure', 'Comedy', 'Sci-Fi'] } const result = await movies.insertOne(doc); // Query for a movie that has the title 'Back to the Future' :) const query = { title: 'Back to the Future' }; const movie = await movies.findOne(query); console.log(movie); } finally { // Ensures that the client will close when you finish/error await client.close(); } } run().catch(console.dir);
附註:
使用 URI 百分比編碼取代連線字串 URI 中的任何保留字元 — 特別是使用者名稱和密碼中的字元。這些是保留字元及其百分比編碼:
!
#
$
%
&
'
(
)
*
+
%21
%23
%24
%25
%26
%27
%28
%29
%2A
%2B
,
/
:
;
=
?
@
[
]
%2C
%2F
%3A
%3B
%3D
%3F
%40
%5B
%5D
例如,如果您的使用者名稱是
RUTH
,而您的密碼是@least1/2#?
,則伺服器<server>
的 MongoDB 連線字串可能如下所示:'mongodb://RUTH:%40least1%2F2%23%3F@<server>:27017/ruth/ ...'
根據您使用的工具或驅動程式,您可以提供使用者名稱和密碼作為個別的參數,而不是提供 URI 連線字串的一部分。在此情況下,您可能不需要將其中包含的任何保留字元編碼。
另請參閱:
-
執行範例。執行結果看起來應如下所示。
$ node connect { _id: new ObjectId("611e3266005202371acf27c1"), title: 'Back to the Future', year: 1985, genres: [ 'Adventure', 'Comedy', 'Sci-Fi' ] }
-
For information about Oracle's commitment to accessibility, visit the Oracle Accessibility Program website at http://www.oracle.com/pls/topic/lookup?ctx=acc&id=docacc.
取得 Oracle 支援
Oracle 客戶對於 Oracle 支援服務的取得及使用,應以其 Oracle 訂單中針對適用服務所指定的條款與條件為依據。