MongoDB叢集怎樣去訪問?

來源:互聯網
上載者:User

標籤:

上一章節簡單介紹了MONGODB的叢集搭建。相信大家都已經很熟悉了。叢集搭建完接下來應該考慮我們的程式應該怎樣去訪問他。

怎麼讀寫資料等操作。下面把我在工作中的一些用法列出來供大家作為參考。

官網的連結串格式如下:

mongodb://[username:[email protected]]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:[email protected]

    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:[email protected]. 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://administrator:13579%40$^*)@12.0.0.1:30000,12.0.0.2:30000,12.0.0.3:30000/admin?slaveOk=true;readPreference=secondary

使用者名稱:administrator

密碼:[email protected]$^*)

三個訪問入口 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叢集的?

針對上面問題。接下來會慢慢更新。



 

MongoDB叢集怎樣去訪問?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.