[英]Connecting to a remote mongoDB server
I have a remote machine which I connect to using SSH, I installed mongoDB on it, and I wish to use it remotely, how do I connect to it using nodejs and mongoDB compass?我有一台使用 SSH 连接的远程机器,我在上面安装了 mongoDB,我希望远程使用它,如何使用 nodejs 和 mongoDB 指南针连接到它? the localhost is the IP?
本地主机是 IP?
const db = "mongodb://what do I write here?";
const connectDB = async () => {
try {
await mongoose.connect(db, { useNewUrlParser: true, useCreateIndex: true });
console.log("MongoDB Connected...");
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
connectDB();
Login to your machine, open mongodb configuration file located at /etc/mongod.conf
and change the bindIp
field to your machine ip address (it is the same ip address which you are using to ssh to your machine), after that restart mongodb server. Login to your machine, open mongodb configuration file located at
/etc/mongod.conf
and change the bindIp
field to your machine ip address (it is the same ip address which you are using to ssh to your machine), after that restart mongodb server .
Open /etc/mongod.conf
file using any of the editor, if you are running a desktop version then you can make use of gedit
utility tool使用任何编辑器打开
/etc/mongod.conf
文件,如果您运行的是桌面版本,则可以使用gedit
实用工具
sudo gedit /etc/mongod.conf
If you are running a server version, then you can make use of vi editor command如果您正在运行服务器版本,则可以使用vi 编辑器命令
sudo vi /etc/mongod.conf
The file should contain the following kind of content:该文件应包含以下类型的内容:
systemLog: destination: file path: "/var/log/mongodb/mongod.log" logAppend: true storage: journal: enabled: true processManagement: fork: true net: bindIp: 127.0.0.1 // enter your ip address here port: 27017 setParameter: enableLocalhostAuthBypass: false
Once you change the bindIp
, then you have to restart the mongodb, using the following command更改
bindIp
后,必须使用以下命令重新启动 mongodb
sudo service mongod restart
Now you'll be able to connect to the mongodb server, with the same ip address which you are using to ssh to your system.现在,您将能够使用与 ssh 相同的 ip 地址连接到 mongodb 服务器。
mongoose.connect('mongodb://<machine_ip_address>:27017/<database_name>')
mongoose.connect('mongodb://username:password@host:port/database')
Now for the host
, is there any hostname or IP you could use?现在对于
host
,您可以使用任何主机名或 IP 吗?
first try this: mongoose.connect('mongodb://localhost:27017/database')
首先试试这个:
mongoose.connect('mongodb://localhost:27017/database')
mongoose.connect('mongodb://<machine_ip_address>:27017/<database_name>')
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[database][?options]]
mongodb://[用户名:密码@]host1[:port1][,...hostN[:portN]][/[数据库][?options]]
check https://docs.mongodb.com/manual/reference/connection-string/检查https://docs.mongodb.com/manual/reference/connection-string/
Try this one:试试这个:
mongoose.connect("mongodb://localhost/<database-name>", { useNewUrlParser: true });
const db = mongoose.connection
db.on('error', (error) => console.error(error));
db.once('open', () => console.log('Connected to Database'));
Make sure to run MongoDB确保运行 MongoDB
mongod --config /usr/local/etc/mongod.conf
mongod --config /usr/local/etc/mongod.conf
If you are using MongoDb Compass如果您使用的是 MongoDb 指南针
Basicly the link is mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]基本上链接是 mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.