我尝试从[OCI]MongoDB客户端连接到Autonomous JSON数据库(使用MongoDB Shell)
首先
通过使用Oracle Database API for MongoDB,在MongoDB客户端和驱动程序中可以访问Autonomous JSON Database,并且可以像MongoDB一样操作Autonomous JSON Database。
所以,我尝试使用MongoDB Shell作为MongoDB的客户端连接到自主JSON数据库。
创建一个Compute实例。
这次我们选择了Oracle Linux 7.9作为操作系统,并创建了一个Compute实例。
2. MongoDB的安装和准备工作
为了比较MongoDB和Autonomous JSON Database,我们将在创建的Compute实例中安装MongoDB。
添加MongoDB 5.0的yum存储库。
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
使用yum install命令安装MongoDB。
[opc@work ~]$ sudo yum install mongodb-org-5.0.5 mongodb-org-server-5.0.5 mongodb-org-shell-5.0.5 mongodb-org-mongos-5.0.5 mongodb-org-tools-5.0.5
Loaded plugins: langpacks, osmsplugin, ulninfo
This system is receiving updates from OSMS.
mongodb-org-5.0 | 2.5 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org.x86_64 0:5.0.5-1.el7 will be installed
--> Processing Dependency: mongodb-org-database for package: mongodb-org-5.0.5-1.el7.x86_64
---> Package mongodb-org-mongos.x86_64 0:5.0.5-1.el7 will be installed
---> Package mongodb-org-server.x86_64 0:5.0.5-1.el7 will be installed
---> Package mongodb-org-shell.x86_64 0:5.0.5-1.el7 will be installed
---> Package mongodb-org-tools.x86_64 0:5.0.5-1.el7 will be installed
--> Running transaction check
---> Package mongodb-org-database.x86_64 0:5.0.5-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================
Package Arch Version Repository Size
==============================================================================
Installing:
mongodb-org x86_64 5.0.5-1.el7 mongodb-org-5.0 6.1 k
mongodb-org-mongos x86_64 5.0.5-1.el7 mongodb-org-5.0 19 M
mongodb-org-server x86_64 5.0.5-1.el7 mongodb-org-5.0 28 M
mongodb-org-shell x86_64 5.0.5-1.el7 mongodb-org-5.0 15 M
mongodb-org-tools x86_64 5.0.5-1.el7 mongodb-org-5.0 6.1 k
Installing for dependencies:
mongodb-org-database x86_64 5.0.5-1.el7 mongodb-org-5.0 6.2 k
Transaction Summary
==============================================================================
Install 5 Packages (+1 Dependent package)
Total download size: 62 M
Installed size: 231 M
Is this ok [y/d/N]: y
Downloading packages:
(1/6): mongodb-org-database-5.0.5-1.el7.x86_64.rpm | 6.2 kB 00:00
(2/6): mongodb-org-5.0.5-1.el7.x86_64.rpm | 6.1 kB 00:00
(3/6): mongodb-org-mongos-5.0.5-1.el7.x86_64.rpm | 19 MB 00:00
(4/6): mongodb-org-server-5.0.5-1.el7.x86_64.rpm | 28 MB 00:00
(5/6): mongodb-org-shell-5.0.5-1.el7.x86_64.rpm | 15 MB 00:00
(6/6): mongodb-org-tools-5.0.5-1.el7.x86_64.rpm | 6.1 kB 00:00
------------------------------------------------------------------------------
Total 39 MB/s | 62 MB 00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mongodb-org-mongos-5.0.5-1.el7.x86_64 1/6
Installing : mongodb-org-tools-5.0.5-1.el7.x86_64 2/6
Installing : mongodb-org-server-5.0.5-1.el7.x86_64 3/6
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
Installing : mongodb-org-shell-5.0.5-1.el7.x86_64 4/6
Installing : mongodb-org-database-5.0.5-1.el7.x86_64 5/6
Installing : mongodb-org-5.0.5-1.el7.x86_64 6/6
Verifying : mongodb-org-shell-5.0.5-1.el7.x86_64 1/6
Verifying : mongodb-org-server-5.0.5-1.el7.x86_64 2/6
Verifying : mongodb-org-tools-5.0.5-1.el7.x86_64 3/6
Verifying : mongodb-org-mongos-5.0.5-1.el7.x86_64 4/6
Verifying : mongodb-org-database-5.0.5-1.el7.x86_64 5/6
Verifying : mongodb-org-5.0.5-1.el7.x86_64 6/6
Installed:
mongodb-org.x86_64 0:5.0.5-1.el7
mongodb-org-mongos.x86_64 0:5.0.5-1.el7
mongodb-org-server.x86_64 0:5.0.5-1.el7
mongodb-org-shell.x86_64 0:5.0.5-1.el7
mongodb-org-tools.x86_64 0:5.0.5-1.el7
Dependency Installed:
mongodb-org-database.x86_64 0:5.0.5-1.el7
Complete!
[opc@work ~]$
启动MongoDB。
[opc@work ~]$ sudo systemctl start mongod
我将检查MongoDB的状态。
[opc@work ~]$ sudo systemctl status mongod -l
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2022-01-18 19:10:35 JST; 6min ago
Docs: https://docs.mongodb.org/manual
Main PID: 13505 (mongod)
CGroup: /system.slice/mongod.service
└─13505 /usr/bin/mongod -f /etc/mongod.conf
Jan 18 19:10:34 work systemd[1]: Starting MongoDB Database Server...
Jan 18 19:10:34 work mongod[13501]: about to fork child process, waiting until server is ready for connections.
Jan 18 19:10:34 work mongod[13501]: forked process: 13505
Jan 18 19:10:35 work systemd[1]: Started MongoDB Database Server.
[opc@work ~]$
将MongoDB设置为自动启动。
[opc@work ~]$ sudo systemctl enable mongod
使用MongoDB Shell(mongosh)连接到本地MongoDB。
[opc@work ~]$ mongosh
Current Mongosh Log ID: 61ea4fa3e9f03bc6f1b4a89a
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.1.9
Using MongoDB: 5.0.5
Using Mongosh: 1.1.9
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting:
2022-01-21T06:15:38.931+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2022-01-21T06:15:38.931+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
------
test>
连接到数据库管理员。
test> use admin
switched to db admin
admin>
创建管理者用户admin(密码:Demo#1Demo#1),并授予root角色。
admin> db.createUser({user:"admin", pwd:"Demo#1Demo#1", roles:[{role:"root", db:"admin"}]})
{ ok: 1 }
admin>
检查已创建的用户。
admin> db.getUsers()
{
users: [
{
_id: 'admin.admin',
userId: UUID("63aef149-23ed-4084-99b6-0ebe25572a3a"),
user: 'admin',
db: 'admin',
roles: [ { role: 'root', db: 'admin' } ],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
],
ok: 1
}
admin>
关闭MongoDB Shell。
admin> exit
[opc@work ~]$
停止 MongoDB。
[opc@work ~]$ sudo systemctl stop mongod
[opc@work ~]$
打开MongoDB的配置文件/etc/mongod.conf,并启用用户认证。
[opc@work ~]$ sudo vi /etc/mongod.conf
在文件末尾添加以下内容。
security:
authorization: enabled
启动MongoDB。
[opc@work ~]$ sudo systemctl start mongod
[opc@work ~]$
3. MongoDB的操作
使用MongoDB Shell,使用admin用户连接到本地的MongoDB。
[opc@work ~]$ mongosh -u admin -p Demo#1Demo#1
Current Mongosh Log ID: 61e7c76c26caf16738d30340
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB: 5.0.5
Using Mongosh: 1.1.8
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
The server generated these startup warnings when booting:
2022-01-19T00:31:08.448+09:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
------
test>
我将查看数据库的列表。
test> show dbs
admin 209 kB
config 61.4 kB
local 73.7 kB
test>
使用”コマンド”来创建数据库,并连接到数据库 “mongo”。
test> use mongo
switched to db mongo
mongo>
我要创建一个”Collection” 部门。
mongo> db.createCollection('dept')
{ ok: 1 }
mongo>
使用insertOne()方法创建dept集合,并添加JSON文档。
mongo> db.dept.insertOne({"deptno":10,"deptname":"ACCOUNTING","location":"NEW YORK"})
{
acknowledged: true,
insertedId: ObjectId("61e7c80d8845e822d33dab38")
}
mongo> db.dept.insertOne({"deptno":20,"deptname":"RESEARCH","location":"DALLAS"})
{
acknowledged: true,
insertedId: ObjectId("61e7c8128845e822d33dab39")
}
mongo> db.dept.insertOne({"deptno":30,"deptname":"SALES","location":"CHICAGO"})
{
acknowledged: true,
insertedId: ObjectId("61e7c8158845e822d33dab3a")
}
mongo> db.dept.insertOne({"deptno":40,"deptname":"OPERATIONS","location":"BOSTON"})
{
acknowledged: true,
insertedId: ObjectId("61e7c8198845e822d33dab3b")
}
mongo>
我将确认Collection部门的内容。
mongo> db.dept.find()
[
{
_id: ObjectId("61e7c80d8845e822d33dab38"),
deptno: 10,
deptname: 'ACCOUNTING',
location: 'NEW YORK'
},
{
_id: ObjectId("61e7c8128845e822d33dab39"),
deptno: 20,
deptname: 'RESEARCH',
location: 'DALLAS'
},
{
_id: ObjectId("61e7c8158845e822d33dab3a"),
deptno: 30,
deptname: 'SALES',
location: 'CHICAGO'
},
{
_id: ObjectId("61e7c8198845e822d33dab3b"),
deptno: 40,
deptname: 'OPERATIONS',
location: 'BOSTON'
}
]
mongo>
让我们指定搜索条件,提取deptno为10的JSON数据。
mongo> db.dept.find({"deptno": 10})
[
{
_id: ObjectId("61e7c80d8845e822d33dab38"),
deptno: 10,
deptname: 'ACCOUNTING',
location: 'NEW YORK'
}
]
mongo>
我們將嘗試從deptname為SALES的JSON數據中提取。
mongo> db.dept.find({"deptname": "SALES"})
[
{
_id: ObjectId("61e7c8158845e822d33dab3a"),
deptno: 30,
deptname: 'SALES',
location: 'CHICAGO'
}
]
mongo>
结束MongoDB Shell。
mongo> exit
4. 创建自主JSON数据库 JSON de
在MongoDB Shell中创建访问Autonomous JSON数据库。
创建MongoDB客户端访问用户并分配角色。
6. 自主JSON数据库的网络访问更改。
获取Oracle数据库API for MongoDB的连接字符串。
连接字符串的格式如下。
如果是新的MongoDB客户端/驱动程序情况下
mongodb://[user:password@]XXXXXXXXXX.adb.<region>.oraclecloudapps.com:27017/[user]?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true
除此之外
mongodb://[user:password@]XXXXXXXXXX.adb.<region>.oraclecloudapps.com:27016/[user]?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false
8. 通过MongoDB Shell访问自主JSON数据库。
因为准备工作已经完成,所以我将尝试从MongoDB Shell连接到Autonomous JSON Database。
我将把刚刚复制的连接字符串中的用户user替换为刚刚创建的用户”mongo”,并将密码替换为mongo用户的密码进行使用。
需要注意的是,如果密码中包含以下字符,则需要进行百分比编码。
: / ? # [ ] @
[opc@work ~]$ mongosh 'mongodb://mongo:Demo%231Demo%231@XXXXXXXXXX.adb.ap-tokyo-1.oraclecloudapps.com:27017/mongo?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true'
Current Mongosh Log ID: 61e8ab854e5e3b975b80c535
Connecting to: mongodb://<credentials>@XXXXXXXXXX.adb.ap-tokyo-1.oraclecloudapps.com:27017/mongo?authMechanism=PLAIN&authSource=$external&ssl=true&retryWrites=false&loadBalanced=true
Using MongoDB: 4.2.14
Using Mongosh: 1.1.8
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
mongo>
我成功地使用MongoDB Shell连接到了Autonomous JSON Database。
我会尝试以相同的方式对MongoDB执行之前的操作。
我要创建一个收藏的部门。
mongo> db.createCollection('dept')
{ ok: 1 }
mongo>
部门收藏已创建。
将检查已创建的收藏。
mongo> show collections
dept
mongo>
将JSON数据添加到“部门收藏夹”中。
mongo> db.dept.insertOne({"deptno":10,"deptname":"ACCOUNTING","location":"NEW YORK"})
{
acknowledged: true,
insertedId: ObjectId("61e8ac1472062cfd73e14ecf")
}
mongo> db.dept.insertOne({"deptno":20,"deptname":"RESEARCH","location":"DALLAS"})
{
acknowledged: true,
insertedId: ObjectId("61e8ac1b72062cfd73e14ed0")
}
mongo> db.dept.insertOne({"deptno":30,"deptname":"SALES","location":"CHICAGO"})
{
acknowledged: true,
insertedId: ObjectId("61e8ac2072062cfd73e14ed1")
}
mongo> db.dept.insertOne({"deptno":40,"deptname":"OPERATIONS","location":"BOSTON"})
{
acknowledged: true,
insertedId: ObjectId("61e8ac2572062cfd73e14ed2")
}
mongo>
我会确认Collection部门的内容。
mongo> db.dept.find()
[
{
_id: ObjectId("61e8ac1472062cfd73e14ecf"),
deptno: 10,
deptname: 'ACCOUNTING',
location: 'NEW YORK'
},
{
_id: ObjectId("61e8ac1b72062cfd73e14ed0"),
deptno: 20,
deptname: 'RESEARCH',
location: 'DALLAS'
},
{
_id: ObjectId("61e8ac2072062cfd73e14ed1"),
deptno: 30,
deptname: 'SALES',
location: 'CHICAGO'
},
{
_id: ObjectId("61e8ac2572062cfd73e14ed2"),
deptno: 40,
deptname: 'OPERATIONS',
location: 'BOSTON'
}
]
mongo>
我将从Collection部门中根据指定条件提取JSON数据。
mongo> db.dept.find({"deptno": 10})
[
{
_id: ObjectId("61e8ac1472062cfd73e14ecf"),
deptno: 10,
deptname: 'ACCOUNTING',
location: 'NEW YORK'
}
]
mongo> db.dept.find({"deptname": "SALES"})
[
{
_id: ObjectId("61e8ac2072062cfd73e14ed1"),
deptno: 30,
deptname: 'SALES',
location: 'CHICAGO'
}
]
mongo>
我确认可以对MongoDB执行的操作也可以对自主JSON数据库执行。
关闭MongoDB Shell。
mongo> exit
用SQL*Plus进行确认
我将使用SQL*Plus连接到Autonomous JSON Database,并且从MongoDB Shell中查看我创建的dept集合的外观。
[opc@work ~]$ sqlplus mongo/Demo#1Demo#1@ajd1_medium
SQL*Plus: Release 19.0.0.0.0 - Production on 木 1月 20 09:31:30 2022
Version 19.11.0.0.0
Copyright (c) 1982, 2020, Oracle. All rights reserved.
最終正常ログイン時間: 木 1月 20 2022 09:30:36 +09:00
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.1.0
に接続されました。
我会查看一下桌子。
SQL> col tname for a20
SQL> SELECT tname FROM tab;
TNAME
--------------------
DEPT
使用Mongo Sell创建的集合dept被作为一张表创建了。
我們來查看一下dept表的結構。
SQL> desc dept
名前 NULL? 型
----------------------------------------- -------- ----------------------------
ID NOT NULL VARCHAR2(255)
CREATED_ON NOT NULL TIMESTAMP(6)
LAST_MODIFIED NOT NULL TIMESTAMP(6)
VERSION NOT NULL VARCHAR2(255)
JSON_DOCUMENT BLOB
部门表包括ID、创建时间、最后修改时间、版本和JSON文档这五个列。
使用Mongo Shell插入的JSON数据似乎存储在BLOB类型的JSON_DOCUMENT中。
我们可以使用JSON_SERIALIZE来查看存储的JSON数据。
SQL> set linesize 200
SQL> SELECT JSON_SERIALIZE(json_document) FROM dept;
JSON_SERIALIZE(JSON_DOCUMENT)
--------------------------------------------------------------------------------
{"deptno":10,"deptname":"ACCOUNTING","location":"NEW YORK","_id":"61e8ac1472062cfd73e14ecf"}
{"deptno":20,"deptname":"RESEARCH","location":"DALLAS","_id":"61e8ac1b72062cfd73e14ed0"}
{"deptno":30,"deptname":"SALES","location":"CHICAGO","_id":"61e8ac2072062cfd73e14ed1"}
{"deptno":40,"deptname":"OPERATIONS","location":"BOSTON","_id":"61e8ac2572062cfd73e14ed2"}
SQL>
我确认在MongoDB Shell中插入的JSON数据已经存储在json_document列中。
请提供一些参考资料。
Oracle帮助中心:Oracle Database API for MongoDB
Oracle帮助中心:使用Oracle Database API for MongoDB
Oracle数据库内幕人士:Oracle Database API for MongoDB
MongoDB文档:连接字符串问题