简体   繁体   English

了解数据库主机和端口?

[英]Understand Database host and port?

I'm new to programming.我是编程新手。 I was trying to set up a database (MangoDB) for school project.我试图为学校项目建立一个数据库(MangoDB)。 The tutorial says:教程说:

*The first step when working with PyMongo is to create a MongoClient to the running mongod instance. *使用 PyMongo 的第一步是为正在运行的 mongod 实例创建一个 MongoClient。 Doing so is easy:这样做很容易:

from pymongo import MongoClient
client = MongoClient()

# The above code will connect on the default host and port. We can also specify the host and port explicitly, as follows:

client = MongoClient('localhost', 27017)

Or use the MongoDB URI format:或者使用 MongoDB URI 格式:

client = MongoClient('mongodb://localhost:27017/')

How do I understand the concept of "connecting default host and port" and what is the uri thing and why we should use it?我如何理解“连接默认主机和端口”的概念以及 uri 是什么以及我们为什么要使用它? Thank you.谢谢你。

You need to tell pymongo where is the database you want it to connect to.您需要告诉pymongo您希望它连接到的数据库在哪里。

Like many other ORMs/database-connectors pymongo uses a connection string.像许多其他 ORM/数据库连接器一样, pymongo使用连接字符串。 In Mongo's case it starts with mongodb:// (denoting the schema/protocol, just like http:// or ftp:// ).在 Mongo 的情况下,它以mongodb://开头(表示模式/协议,就像http://ftp:// )。

Then comes the host (either a hostname or an IP), which denotes the machine/server on which the database resides, and lastly the port the server listens on.然后是主机(主机名或 IP),它表示数据库所在的机器/服务器,最后是服务器侦听的端口。

If the database is on the same machine as the code, you should use 127.0.0.1 or localhost as the host.如果数据库与代码在同一台机器上,则应使用127.0.0.1localhost作为主机。

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

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