Python Use pymongo to Connect to Local Database Prompt Positive Rejection?

  mongodb, question

The problem is just like the topic. I will describe the specific situation in detail.
The same question was raised in Zhihu.

When I taught myself python crawler, I came into contact with MongoDB. According toA novice courseIt is quite normal to install the website, and I raised warning, but after checking it, I probably don’t have a user name, password, local database or something. The following figure:
安装结果

Cfg:

systemLog:
 destination: file
 path: H:\data\log\mongo.log
 logAppend: true
 storage:
 dbPath: h:\data\db
 
 net:
 bindIp: 127.0.0.1

There is nothing strange here, I then follow the steps of the novice tutorial input in the shell input mongo get feedback:
反馈
This is inconsistent with the results of the rookie tutorial:
不一致结果

In fact, it is not harmful, because the following addition and insertion of data were successful.

But according to other tutorials, I entered “localhost:27107” in chrome to prompt me:
访问失败

The next step is to return to the question I am asking now. After reading some codes, I wrote a very simple code to play with, as follows:

from pymongo import MongoClient
 
 client = MongoClient('localhost', 27107)
 
 db = client.test
 
 posts = db.posts
 
 post = {
 "name": "shiki",
 "age": "21"
 }
 
 posts.insert(post)

Whether my code is written correctly or not, I found that I made a mistake on the second line:
图片描述

At this time, my windows service MongoDB is running, and I should be running MongoDB (In fact, I’m not sure if it has been opened, but I already have the interface of the first picture in CMD, and I will open it by default.)

This concludes the description of the problem. The following are some information about my computer, and the attempts I have made, so that I can make a good analysis.

-dividing line-

  1. There is a statement on the internet that 27107 may not be open or something. I have already set up the entry rules for firewall. Useless
  2. I have tried to hang SSR and not to hang SSR, but I can’t either.
  3. I found someone who said that there was an ip problem. I tried to set the ip to 0.0.0.0, but I couldn’t do it (in fact, I didn’t think it was likely, but I also tried it)
  4. Others said mongodb didn’t start … I don’t know how to say it, I can connect it with MongoDB Compass, which is the official one, and I can also operate data in the shell, which is the default, right?
  5. In the configuration file, I also specially wrote the port 27107
  6. I am using a desktop computer, not a network cable, but a wifi receiver, and the tplink in my home is relayed through a millet signal amplifier, isn’t it? I don’t know much about it either. Even if the signal in the room is weak, I used an amplifier to search for wifi
  7. The serious suspicion is that the network does not know that there is a problem in that link. Of course, it is also possible that I am inexperienced and have found nothing. Interesting bosses who want to solve the problem can put forward their own ideas or ask me for some detailed information. Thank you again and again! ! ! !

There was a bit of confusion, and I said little by little:

Visual inspection is quite normal. warning was raised, but after checking it, I probably don’t have a user name, password, local database or something.

Yes, this is just a warning, telling you that there are some operational risks that do not conform to best practices, but do not affect your use. It’s just that we should consider solving these problems before going to the production environment.

There is no problem with the configuration file. You have entered mongo shell in your screenshot and can start to execute mongo’s CRUD command (just follow up with mysql shell). However, you execute mongo in mongo shell, and you don’t understand what it means. Mongo.exe is a bash command. After running it, you just enter the mongo shell, and you are already in the shell. How did you get into this shell? Didn’t you just execute mongo?

Again, chrome connectionlocalhost:27017Many people make such mistakes, which may be misled by some tutorials. For example, MySQL listens by defaultlocalhost:3306, SQLServer default listeninglocalhost:1433, you will connect in the influence devicelocalhost:1433/localhost:3306Is it? What will be the effect after the connection? There will also be errors in the screenshots. There is no point in connecting like this. To connect port 27017, you should use the mongo shell, or mongo.exe:mongo.exe --host localhost:27017

Finally, I would like to talk about Python’s problems. You have already connected MongoDB through the shell, so there is no problem for the server to run. However, Python cannot be connected, possibly because there is a problem in the resolution of the domain name localhost. You can use:

MongoClient('127.0.0.1', '27017')

Try to see what the result is.