简体   繁体   English

猫鼬没有连接到本地主机

[英]mongoose not connecting to localhost

I seem to have issues when trying to create a mongoose connection. 尝试创建猫鼬连接时似乎出现问题。 I am following a book published in 2014 so my immediate response was to check the mongoose docs but they agree that the book gives the correct format. 我关注的是2014年出版的一本书,因此我的立即反应是检查猫鼬文档,但他们同意这本书的格式正确。 Below is my connection: 以下是我的联系:

var dbURI = 'mongodb://localhost/Loc8r';
mongoose.connect(dbURI);

As soon as I add these lines, I get the following error: 添加这些行后,会出现以下错误:

Mongoose connection error: mongodb://127.0.0.1/Loc8r
(node:743) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
(node:743) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

If I remove the connection, I have no errors...but that would defeat the purpose of trying to connect to a DB. 如果删除连接,则不会有任何错误...但是那会破坏尝试连接到数据库的目的。 I also tried substituting localhost in the URI for 127.0.0.1 which is known to solve some issues but I had no luck there either. 我还尝试用URI代替localhost来解决127.0.0.1 ,这可以解决一些问题,但是我也没有运气。 I am running the localhost on a simple $nodemon command via terminal and nothing else. 我通过终端在一个简单的$nodemon命令上运行localhost ,没有别的。

Useful info: 有用的信息:

  • MongoDB v3.6.3 MongoDB v3.6.3
  • Mongoose v5.0.10 猫鼬v5.0.10
  • Express v4.15.5 快递v4.15.5
  • Node v8.9.4 节点v8.9.4

Help would be appreciated. 帮助将不胜感激。

Thanks. 谢谢。

try this way 尝试这种方式

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  db.close();
});

You are going on the right path but you forgot to put the port on which MongoDB runs, MongoDB runs on the port 27017 您走的路正确,但是您忘了在其上运行MongoDB的端口,在MongoDB的端口27017

Do not forget to start MongoDB server, If you are on mac open terminal and enter mongod and it will start your MongoDB server then run your code it will work fine. 不要忘记启动MongoDB服务器,如果您在mac open终端上并输入mongod ,它将启动您的MongoDB服务器,然后运行您的代码,它将正常工作。

var dbURI = 'mongodb://localhost:27017/Loc8r';
mongoose.connect(dbURI);
mongoose.connection.on("connected", () => {
    console.log("Connected to database");
});
mongoose.connection.on("error", (err) => {
  console.log("Database error:" + err);
});

I apologise for this as I was able to find the solution myself but I guess this may help others some day. 我为此感到抱歉,因为我自己可以找到解决方案,但是我想这可能有一天会帮助其他人。

The fault lay with the fact that I only installed MongoDB via Homebrew and I stopped as soon as MongoDB was literally "installed" and considered MongoDB to be "installed". 故障在于我仅通过Homebrew安装了MongoDB,而在字面上“安装”了MongoDB并认为MongoDB被“安装”后,我就停止了。 I went back to look at how MongoDB should properly be installed and I noticed that I did not make the directory /data/db and give it its permissions which was an important step. 我回过头来看看应该如何正确安装MongoDB,我注意到我没有创建目录/data/db并为其提供权限,这是重要的一步。

Once I did this, I ran mongod globally, and nodemon on a separate terminal, locally at the root of the app and it worked completely fine and got a successful message in the console. 完成此操作后,我在全球范围内运行mongod ,并在单独的终端上(位于应用程序的本地位置)运行nodemon ,它运行良好,并在控制台中获得成功消息。 Every time I tried to connect, it was searching for /data/db which it obviously could not find as I had not made it. 每次我尝试连接时,它都在搜索/data/db ,但由于我没有成功,它显然找不到。 But I would not have came to this conclusion if Nikhil had not mentioned running mongod . 但是如果尼克希尔(Nhilhil)没有提到跑mongod我不会得出这个结论。

To summarise, ensure you follow all installing steps in future. 总而言之,请确保将来执行所有安装步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM