mongodb 以管理員登錄並創建 database
在一個有了用戶名的數據庫集中,即使在 admin 數據庫中創建了用戶,登錄上去后還是不能訪問其他數據庫的,但是用 登錄是可以的呀,雖然可以在相應數據庫中再建立用戶,但別的程序都不用是怎么回事?
原來是要在用戶名后加上 "(admin)" 標識.
例如
//ok//MongoServer server = MongoServer.Create("mongodb://root:111@192.168.0.34:27017/?connect=direct;slaveOk=true"); // connect to localhost
MongoServer server = MongoServer.Create("mongodb://root(admin):111@192.168.0.34:27017/?connect=ReplicaSet;slaveOk=true"); // connect to localhost
是在以下找到的,用的 baidu 關鍵字 "MongoDatabase GetDatabase Invalid credentials for database"
關鍵字來源於 C# 的錯誤提示:
"
An unhandled exception of type 'MongoDB.Driver.MongoAuthenticationException' occurred in MongoDB.Driver.dll
Additional information: Invalid credentials for database 'demoBaseaaa'.
"
本來想查找 mongodb.exe 中是怎么實現的 use,結果發現它調用 js.. 找了半天也沒找到 C# 如何實現這樣的先 use admin 再 use 普通 database 的,看來 API 和它的 shell tool 實現還是有差異.
http://groups.google.com/group/mongodb-user/browse_thread/thread/82132048f3ba1f6d
--------------------------------------------------
![]() |
![]() |
共 7 個帖子 - 全部折疊 - 將所有內容翻譯成中文(簡體) |
![]() |
![]() |
The database will be created automatically when you insert the first When running in authentication mode you must provide the credentials to use var credentials = new MongoCredentials("username", "password"); The only thing different about running with a replica set is that your |
![]() |
Hi Robert,
Thanks for the reply. I have ran into some issues with the suggested I want to create a new database and then a new collection in that Here is what I am doing in code to achieve the above: // Connect to server // Create my new database // Create my new collection Observations: I am running in authenticated mode but the database has not been Questions: 1. Given that I am running in authenticated mode what is the proper Invalid credentials for database 'SomeTestDatabase'. Thanks. Best Regards, On Jan 18, 8:11 am, Robert Stam <rob...@10gen.com> wrote: |
![]() |
If you want the default credentials supplied in the URL to be
authenticated against the admin database you put "(admin)" after the username in the URL, like this: var url = "mongodb:// When you authenticate against the admin database you gain access to On Jan 27, 7:22 am, ALH <ahi...@gmail.com> wrote: |
![]() |
Hi Robert,
This totally worked though I could not find this connection string on On Jan 27, 7:07 am, Robert Stam <rob...@10gen.com> wrote: |
![]() |
![]() |
--------------------------------------------------
里面說幫助頁面上有說明的,看了一下的確有..就是說得太隱晦了:
Connection strings
The easiest way to connect to a MongoDB server is to use a connection string. The standard connection string format is:
mongodb://[username:password@]hostname[:port][/[database][?options]]
The username and password should only be present if you are using authentication on the MongoDB server. These credentials will be the default credentials for all databases. To authenticate against the admin database append "(admin)" to the username. If you are using different credentials with different databases pass the appropriate credentials to the GetDatabase method.
嗯,主要是我英文太爛,又沒想到它的字符串示例沒列出來完.
http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-TheC%23Driver
原來有一個完整的
//字符串來自 http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-Connectionstrings