mongodb 以管理員登入並建立 database

來源:互聯網
上載者:User

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

--------------------------------------------------


Creating a database from the 10gen C# driver
選項
   
      共 7 個文章 - 全部摺疊
 - 
將所有內容翻譯成中文(簡體)

 
     

ALH
 
查看設定檔
 
翻譯成中文(簡體)


 更多選項

1月19日, 上午12時05分

How do I create a database from using 10gen C# driver? I have tried
this using the GetDatabase method in MongoServer with no luck. Also,
how would I do this if running in authentication mode? Is it any
different then running within a replica set? Thanks. 
     

     

Robert Stam
 
查看設定檔
 
翻譯成中文(簡體)


 更多選項

1月19日, 上午12時11分

The database will be created automatically when you insert the first
document.

When running in authentication mode you must provide the credentials to use
for that database, like this:

var credentials = new MongoCredentials("username", "password");
var database = server.GetDatabase("databaseName", credentials);

The only thing different about running with a replica set is that your
connection string must have a seed list containing all or some of the
replica set members.

- 顯示引用的文字 - 
     

     

ALH
 
查看設定檔
 
翻譯成中文(簡體)


 更多選項

1月27日, 下午8時22分

Hi Robert,

Thanks for the reply. I have ran into some issues with the suggested
approach that I would like to share with you. So, just to recap:

I want to create a new database and then a new collection in that
database.

Here is what I am doing in code to achieve the above:

// Connect to server
var url = "mongodb://someAdminUser:someAdminUserPassword@localhost:
9001/admin"
var server = MongoServer.Create(url);

// Create my new database
var db = server.GetDatabase("SomeNewDatabase");

// Create my new collection
var collection = db.CreateCollection("MyNewCollection");

Observations:

I am running in authenticated mode but the database has not been
create yet, therefore, has no user credentials to authenticate against

Questions:

1. Given that I am running in authenticated mode what is the proper
way to connect to the server so that I can create a database that at
creation will not have any credentials? (please see the below error
message I am getting when executing the above code sample)
2. Is there a way to create a database without writing any data like
you would in SQL Server? I am currently doing this from the mongo
shell in PowerShell using connect("MyNewDatabase"). I would be willing
to write a CreateDatabase method if you can provide guidance.

Invalid credentials for database 'SomeTestDatabase'.
   at MongoDB.Driver.Internal.MongoConnection.Authenticate(String
databaseName, MongoCredentials credentials) in C:\work\10gen\mongodb
\mongo-csharp-driver\Driver\Internal\MongoConnection.cs:line 165
   at
MongoDB.Driver.Internal.MongoConnection.CheckAuthentication(MongoDatabase
database) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal
\MongoConnection.cs:line 244
   at
MongoDB.Driver.MongoServerInstance.AcquireConnection(MongoDatabase
database) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core
\MongoServerInstance.cs:line 260
   at MongoDB.Driver.MongoServer.AcquireConnection(MongoDatabase
database, Boolean slaveOk) in C:\work\10gen\mongodb\mongo-csharp-driver
\Driver\Core\MongoServer.cs:line 1052
   at MongoDB.Driver.MongoCursorEnumerator`1.AcquireConnection() in C:
\work\10gen\mongodb\mongo-csharp-driver\Driver\Core
\MongoCursorEnumerator.cs:line 184
   at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\Core
\MongoCursorEnumerator.cs:line 194
   at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\Core
\MongoCursorEnumerator.cs:line 126
   at MongoDB.Driver.MongoDatabase.GetCollectionNames() in C:\work
\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoDatabase.cs:line
662
   at MongoDB.Driver.MongoDatabase.CollectionExists(String
collectionName) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver
\Core\MongoDatabase.cs:line 305

Thanks.

Best Regards,
RelayHealth
Albert L. Hives

On Jan 18, 8:11 am, Robert Stam <rob...@10gen.com> wrote:

- 顯示引用的文字 - 
     

     

Robert Stam
 
查看設定檔
 
翻譯成中文(簡體)


 更多選項

1月27日, 下午11時07分

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://
someAdminUser(admin):someAdminUserPassword@localhost:9001"

When you authenticate against the admin database you gain access to
all databases at once (including the new one you are about to create).

On Jan 27, 7:22 am, ALH <ahi...@gmail.com> wrote:

- 顯示引用的文字 - 
     

     

ALH
 
查看設定檔
 
翻譯成中文(簡體)


 更多選項

1月28日, 上午12時58分

Hi Robert,

This totally worked though I could not find this connection string on
the driver docs @ http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriv...

On Jan 27, 7:07 am, Robert Stam <rob...@10gen.com> wrote:

- 顯示引用的文字 - 
     

     

Robert Stam
 
查看設定檔
 
翻譯成中文(簡體)


 更多選項

1月28日, 上午1時05分

It's kind of buried in there:

http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriv...

- 顯示引用的文字 - 
     

     

ALH
 
查看設定檔
 
翻譯成中文(簡體)


 更多選項

1月28日, 上午3時06分

You are correct. I see it now. Thanks again.

On Jan 27, 9:05 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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.