将 MongoDB API 与 Oracle Autonomous Database on Dedicated Exadata Infrastructure 结合使用
Oracle Database API for MongoDB 将 MongoDB 线协议转换为由 Oracle Database 执行的 SQL 语句。它允许拥有 MongoDB 技能集的开发人员为 Oracle Database 编写 JSON 文档存储应用程序,这些应用程序使用了解 MongoDB 协议的驱动程序和工具。有关此 API 的详细概述,请参阅 Oracle Database API for MongoDB 中的 Oracle Database API for MongoDB 概述。
启用 MongoDB API
您可以从 OCI 控制台为 Autonomous Database 启用 MongoDB API,也可以使用 Oracle REST Data Services (ORDS) 手动启用。
要将 MongoDB API 与 Autonomous Database 一起使用,您必须单独安装和配置客户管理的 Oracle REST Data Services (ORDS),并且 ORDS 的版本必须为 22.3 或更高版本。有关此 API 的体系结构概念,请参阅 Oracle REST Data Services Installation and Configuration Guide 中的 Oracle API for MongoDB Support 。
- 在 Autonomous Database 详细信息页面上,选择 Tool configuration(工具配置)选项卡。
- 单击编辑工具配置。
- 在 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 详细信息页面上,选择 Tool configuration(工具配置)选项卡。
- 在 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 Actions 。
- 从 Database Actions 启动板中,单击 Administration(管理)> Database Users(数据库用户)。
- 单击“数据库用户”页上的 + 创建用户。
- 为测试用户输入用户名和密码。单击 Web 访问,然后设置表空间 DATA 上的限额。
- 单击 Granted Roles 选项卡。
- 除了默认角色外,还为用户选择并添加
SODA_APP
角色。 - 单击 Create User(创建用户)。
使用命令行测试连接
- 以测试用户身份登录。有关说明,请按照上一个示例中的步骤操作。
$ 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 客户机(例如 Database Actions)查询集合。
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 订单中规定的条款和条件。