【mongoDB】mongoDB的正确的连接方式

一、单机版

# systemctl status mongod

# cat /etc/mongod.conf

未开启认证

在admin库创建数据库超管用户 

# echo -e "use admin;\ndb.createUser({user:\"root\",pwd:\"Root@123\",roles:[ { role:\"userAdminAnyDatabase\",db:\"admin\"}]})" |  mongo  --host 127.0.0.1  --port  27017

开启认证,用超管用户创建其他用户

开启认证

# systemctl restart mongod

# mongo --host 127.0.0.1 -u root -p "Root@123"  --authenticationDatabase  admin

# echo -e "use admin;\n show tables;" | mongo --host 127.0.0.1 -u root -p "Root@123" --authenticationDatabase admin

二、复制集

获取集合(实例)名

$ echo -e "rs.status()['set']" |  /path/to/mongo --quiet "mongodb://${username}:${password}@${ip}:${port}"

$ echo -e "rs.status()['set'];" |  /path/to/mongo --quiet "mongodb://${username}:${password}@${ip}:${port}"

$ echo -e "rs.status()['set'];\n" |  /path/to/mongo --quiet "mongodb://${username}:${password}@${ip}:${port}"

${ip}:${port} 可以是主节点的IP和端口,也可以是从节点的IP和端口

注: --quiet  参数可以去掉相关数据库的连接信息输出

连接mongoDB复制集

$ echo -e "rs.status()"  |  /path/to/mongo  --quiet  "mongodb://${username}:${password}@${primary_ip}:${port}/?replicaSet=$(set_name)"

$ echo -e "rs.status()"  |  /path/to/mongo  --quiet  "mongodb://${username}:${password}@${primary_ip}:${port},${secondary_ip}:${port}/?replicaSet=$(set_name)"

$ echo -e "rs.status()"  |  /path/to/mongo  --quiet  "mongodb://${username}:${password}@${primary_ip}:${port},${secondary01_ip}:${port},${secondary02_ip}:${port}/?replicaSet=$(set_name)"

注:

1. 主动仲裁,写主从 ip port,用逗号隔开

2. 主从从,写主从从  ip port,用逗号隔开

3. set_name 为mongoDB 实例名

针对副本集mongoDB命令行登陆的2种方式:

方式一:如果要做数据变更操作,需要提前找到谁是主节点,再登陆主节点

mongo --host  192.168.1.101  --port  27018 -u "user" -p "passwd"  --authenticationDatabase  admin

方式二:通过mongo连接字符串登陆,会自动登陆主节点

1. 登录,不指定库,走admin库认证

mongo  "mongodb://${user}:${paswd}@192.168.1.101:27018,192.168.1.102:27018/?replicaSet=${instance}&authSource=admin"

2. 登录,指定库名,走admin认证

mongo  "mongodb://${user}:${paswd}@192.168.1.101:27018,192.168.1.102:27018/${db_name}?replicaSet=${instance}&authSource=admin"

3. 登录,指定库名,并且在指定库名做认证

mongo  "mongodb://${user}:${paswd}@192.168.1.101:27018,192.168.1.102:27018/${db_name}?replicaSet=${instance}&authSource=${db_name}"

注:replicaSet 为 mongoDB的实例名

mongo shell 的连接字符串跟 应用客户端的连接字符串还有点不一样:

mongo shell 连接副本集,必须指定replicaSet名称,库名指定是可选的,可指定,可不指定;

应用(如java应用)中jdbc连接mongoDB的字符串中一定要指定库名,如果是replicaSet只需要填写所有主从的地址,而不是填写replicaSet名称

如果登陆是从节点,那么无法做数据变更操作,因为mongoDB默认只有主节点可写

mongo --host  192.168.1.102  --port  27018 -u "user" -p "passwd"  --authenticationDatabase  admin

> rs.slaveOk()

注意:对于从mongo shell登陆从节点,需要先执行 rs.slaveOk() 后才能执行相关查询操作,对客户端应用则没有这个限制,默认可以读从,可以实现读写分离。

当客户端连接副本集时,如果以primary地址连接实例,当发生上述情况时,P节点降级为Secondary节点,客户端将无法继续执行写操作。

为了解决这一问题,连接线上数据时,最好是使用副本集方式连接副本集。

正确连接副本集,可以参考官网:  https://docs.mongodb.com/manual/reference/connection-string

该文档描述了官方MongoDB drivers连接MongoDB实例的URI格式。

URI形如:

mongodb://[username:password@]host1[:port1][,...hostN[:portN]]][/[database][?options]]

options都怎么写?

参考官方 Connection String URI Format — MongoDB Manual

mongo shell 连接常见报错:FailedToParse: Password must be URL Encoded for mongod

#   mongo     "mongodb://root:Root@[email protected]:27017,192.168.1.22:27017/test?authSource=admin&replicaSet=8e08b0f0-7db1-49aa-8274-05c3bca10e67&readPreference=secondaryPreferred"

FailedToParse: Password must be URL Encoded for mongodb:// URL: mongodb://root:Root@[email protected]:27017,192.168.1.22:27017/test?authSource=admin&replicaSet=8e08b0f0-7db1-49aa-8274-05c3bca10e67&readPreference=secondaryPreferredtry'mongo --help'formoreinformation

密码必须经过URL编码 

如果用户名或密码包含at符号@,冒号:,斜杠/或百分号%字符,请使用百分比编码

参考 Connection String URI Format — MongoDB Manual

在线转换工具:https://www.url-encode-decode.com

常用的options:

authSource= //认证用的数据库

replicaSet=//副本集名称

readPreference=secondaryPreferred //实现读写分离

tls=true //启用TLS加密传输(从MongoDB 4.2开始可用)

connectTimeoutMS  //超时之前尝试连接的时间(以毫秒为单位)。默认值是永不超时,尽管不同的驱动程序可能有所不同,请参阅驱动程序 文档。

socketTimeoutMS //尝试超时之前在套接字上尝试发送或接收的时间(以毫秒为单位)。默认值是永不超时,尽管不同的驱动程序可能有所不同。请参阅 驱动程序文档。

连接池选项

maxPoolSize  //连接池中的最大连接数。默认值为100。

minPoolSize //连接池中的最小连接数。默认值为0。 (注意minPoolSize并非所有驱动程序都支持该选项。有关驱动程序的信息,请参阅驱动程序文档。)

maxIdleTimeMS  //在删除和关闭连接之前,连接在池中可以保持空闲状态的最大毫秒数。 (并非所有驱动程序都支持此选项。)

waitQueueMultiple //驱动程序将maxPoolSize 值乘以的数字,以提供允许最大数量的线程从池中等待可用连接。有关默认值,请参见/ drivers 文档。

驱动程序: https://docs.mongodb.com/drivers

写选项

wtimeoutMS  // wtimeoutMS指定写关注的时间限制(以毫秒为单位)。如果wtimeoutMS是0(默认),写操作永远不会超时。有关更多信息,请参见wtimeout

连接字符串

主从仲裁:

mongo.url=mongodb://${user}:${passwd}@172.16.218.26:27017,172.16.218.27:27017/${ db_name}?authSource=${db_name}&maxpoolsize=200&waitqueuetimeoutms=10000&sockettimeoutms=5000&connecttimeoutms=5000&readPreference=secondaryPreferred

主从从:

mongo.url=mongodb://${user}:${passwd}@172.16.218.26:27017,172.16.218.27:27017,172.16.218.28:27017/${ db_name}?authSource=${db_name}&maxpoolsize=200&waitqueuetimeoutms=10000&sockettimeoutms=5000&connecttimeoutms=5000&readPreference=secondaryPreferred

三、分片集

应用客户端通过连接mongos到分片

mongodb://${user}:${passwd}@192.168.1.101:12345,192.168.1.102:12345192.168.1.103:123456/${ db_name}&authSource=${db_name}

##########################

用 mongo shell 连接分片1

mongodb://172.17.0.8:27018,172.17.0.11:27018,172.17.0.12:27018/test?replicaSet=rs_shardsvr0&authSource=${db_name}

用mongo shell 连接分片2

mongodb://172.17.0.13:27018172.17.0.14:27018,172.17.0.15:27018/test?replicaSet=rs_shardsvr1&authSource=${db_name}

用mongo shell,通过mongos登陆分片集群

# mongo --host  xx.xx.xx.xx    --port=12345    --username='user'  --password='passwd'  --authenticationDatabase=admin

使用分片集群时你需要知道:

1. 用户访问 mongos 跟访问单个 mongod 类似

2. 所有 mongos 是对等关系,用户访问分片集群可通过任意一个或多个mongos

3. mongos 本身是无状态的,可任意扩展,集群的服务能力为『Shard服务能力之和』与『mongos服务能力之和』的最小值。

4. 访问分片集群时,最好将应用负载均匀的分散到多个 mongos 上

通过上述方式连接分片集群时,客户端会自动将请求分散到多个mongos 上,以实现负载均衡;同时,当URI 里 mongos 数量在2个及以上时,当有mongos故障时,客户端能自动进行 failover,将请求都分散到状态正常的 mongos 上。

当 mongos 数量很多时,还可以按应用来将 mongos 进行分组,比如有2个应用A、B、有4个mongos,可以让应用A 访问 mongos 1-2(URI里只指定mongos 1-2 的地址), 应用B 来访问 mongos 3-4(URI里只指定mongos 3-4 的地址),根据这种方法来实现应用间的访问隔离(应用访问的mongos彼此隔离,但后端 Shard 仍然是共享的)。

四、参考

副本集正确的连接方式

https://www.cnblogs.com/zhangmingda/p/13183812.html

MongoDB Manual/Write Concern

https://docs.mongodb.com/manual/reference/write-concern/#write-concern

MongoDB连接标准格式

https://www.cnblogs.com/AlvinLee/p/6062167.html

MongoDB Driver使用正确的姿势连接复制集

https://blog.csdn.net/weixin_34258078/article/details/93186333

MongoDB 开发:使用正确的姿势连接分片集群

https://blog.csdn.net/weixin_30569153/article/details/94783916

MongoDB的连接字符串

https://www.cnblogs.com/imeiba/p/5702298.html

MongoDB的连接字符串

http://www.studyofnet.com/news/337.html

mongoDB集群和分片

https://www.freesion.com/article/7807126996

副本集状态参考

https://mongodb-documentation.readthedocs.io/en/latest/reference/replica-status.html

Connection String URI Format

https://www.bookstack.cn/read/mongodb-4.2-manual/68e775d51debcb2c.md

https://mongoing.com/docs/reference/connection-string.html

深入浅出MongoDB复制

https://mongoing.com/archives/5200

mongodb集群状态

https://blog.csdn.net/ZhongGuoZhiChuang/article/details/46646091

MongoDB Replica Set 安装部署

https://blog.51cto.com/hsbxxl/2149531

How to edit Mongo connection string?

https://stackoverflow.com/questions/59019193/how-to-edit-mongo-connection-string

MongoDB Manual/Security Reference

https://docs.mongodb.com/upcoming/reference/security/#security-methods-in-the-mongo-shell

おすすめ

転載: blog.csdn.net/michaelwoshi/article/details/121370805
おすすめ