如何远程连接到位于 K8S 之上的 mongo DB

发布于 2025-01-12 06:57:12 字数 849 浏览 1 评论 0原文

目前我正在执行一项任务,将一些数据填充到 mongo DB 中。该 mongo DB 构建在 K8S 之上,如下所示。这是我第一次在这样的环境下工作。

,在“Shell”级别,我能够

mongo -u root -p 

手动进入 mongo 控制台页面,并手动处理其余的数据库查询。

但我的问题是,在相同的“Shell”级别,我可以将所有查询包装在 JS 文件中并使用远程数据库连接字符串来执行它吗?

这是我尝试过的 read.js 文件的示例:

var db = connect('127.0.0.1:27017/fext'),
    allDevices = null;

allDevices = db.names.find();

//iterate the names collection and output each document
while (allDevices.hasNext()) {
    printjson(allDevices.next());
}

当我在此命令中执行它时:

mongo read.js -u root -p 

我收到以下返回消息: 输入图片此处描述

评论?感谢您的帮助!

杰克

Currently I am working on a task to populate some data into a mongo DB. This mongo DB is built on top of K8S as follows. This is the first time I am working in such environment.
enter image description here

As you can see, on the "Shell" level, I was able to run

mongo -u root -p 

to manually get into the mongo console page, and manually worked on the rest of DB queries.

But my question is, at the same "Shell" level, can I have all queries wrapped in a JS file and with a remote DB connection string to have it executed ?

Here is a sample of my read.js file I have tried:

var db = connect('127.0.0.1:27017/fext'),
    allDevices = null;

allDevices = db.names.find();

//iterate the names collection and output each document
while (allDevices.hasNext()) {
    printjson(allDevices.next());
}

And when I was to have it executed in this command:

mongo read.js -u root -p 

I have got following returning message:
enter image description here

Comments ? Thanks for the help !

Jack

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文