MongoDB集群怎样去访问?

上一章节简单介绍了MONGODB的集群搭建。相信大家都已经很熟悉了。集群搭建完接下来应该考虑我们的程序应该怎样去访问他。

怎么读写数据等操作。下面把我在工作中的一些用法列出来供大家作为参考。

官网的链接串格式如下:

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

每一部分组成如下:
  1. mongodb://

    A required prefix to identify that this is a string in the standard connection format.

  2. username:password@

    Optional. If specified, the client will attempt to log in to the specific database using these credentials after connecting to the mongod instance. (如果设置了开启了安全验证需要加入用户名密码部分。如果没有开启auth=true。则不需要配置用户名和密码) 注意:在密码后面跟着一个@符号。可能是为了区分密码和后面的部分的分割。所以设置密码时候最好不要包含@。这样会导致mongodb默认读取第一个@符号之前的当作密码。 如果在密码中包含了密码该怎么处理呢。 在c#中只需要把密码中的@符号改成%40,在c#中自动转换为@了。

  3. host1

    This the only required part of the URI. It identifies a server address to connect to. It identifies either a hostname, IP address, or UNIX domain socket.

  4. :port1

    Optional. The default value is :27017 if not specified.

  5. hostX

    Optional. You can specify as many hosts as necessary. You would specify multiple hosts, for example, for connections to replica sets.

  6. :portX

    Optional. The default value is :27017 if not specified.   

  7. /database

    Optional. The name of the database to authenticate if the connection string includes authentication credentials in the form of username:password@. If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.   如果是集群。 database 应该为admin. 因为密码验证是在admin库中查询并验证的。 如果你设置的是你存储数据的数据库。 可能会出现用户不存在的错误。 这里需要注意下。

  8. ?options

    Connection specific options. See Connection String Options for a full description of these options.

    If the connection string does not specify a database/ you must specify a slash (i.e. /) between the last hostN and the question mark that begins the string of options.

 

实例:

mongodb://admin:212313@12.0.0.1:30000,12.0.0.2:30000,12.0.0.3:30000/admin?slaveOk=true;readPreference=secondary

用户名:administrator

密码:13579@$^*)

三个访问入口 mongos地址 : 12.0.0.1:30000,12.0.0.2:30000,12.0.0.3:30000

slaveOK=ture : 开启从库可读。(默认从库不可读写)

readPreference=secondary: 偏好读取从节点。  开启读写分离。 其他配置请参考官网。

 

 

更多内容:

1.Windows 集群搭建

2.用户权限怎么添加?

3.如何监控集群环境是否工作正常。

4.如何做到数据分布式存储

5.片键如何选择?

6.索引建立

7.curd基本操作。

8.读写分离如何控制。

9.c# php java js 等如何使用Mongod集群的?

针对上面问题。接下来会慢慢更新。



 

posted @ 2015-09-28 14:48  Mountains  阅读(1559)  评论(0编辑  收藏  举报