Log on to MongoDB as an administrator and create a database

Source: Internet
Author: User
Tags mongodb server

Log on to MongoDB as an administrator and create a database

In a database with a user name, even if you create a user in the admin database, you still cannot access other databases after logging on, but you can log on to the database, although users can be created in the corresponding databaseProgramWhy?

 

It turns out that "(Admin)" must be added to the user name.

For example

// OK // Login Server = Login server. Create ("MongoDB: // root: 111@192.168.0.34: 27017 /? Connect = direct; slaveok = true "); // connect to localhost
Login Server = Login server. Create ("MongoDB: // root (Admin): 111@192.168.0.34: 27017 /? Connect = replicaset; slaveok = true "); // connect to localhost
The Baidu keyword "relational database getdatabase invalid credentials for Database" is found below"

Keyword error message from C:

"

An unhandled exception of Type 'mongodb. Driver. Your authenticationexception' occurred in MongoDB. Driver. dll

Additional information: invalid credentials for database 'demobaseaaa '.

"

I originally wanted to find out how to implement the use in mongodb.exe, and found that it calls JS .. after looking for a long time, I didn't find C # how to implement this first use admin and then use common database. It seems that the implementation of API and its shell tool is still different.

Http://groups.google.com/group/mongodb-user/browse_thread/thread/82132048f3ba1f6d

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


Creating a database from the 10gen C # driver
Option
   
      A total of 7 posts-Hide all
-
Translate all contentChinese (simplified)

 
& Lt; TD width = "100%" & gt;

ALH
View Personal Data
translate to Chinese (simplified)


more options

August 1, January 19, 12:05 A.M.
How do I create a database from using 10gen C # driver? I have tried
This using the getdatabase method in Login server with no luck. Also,
How wocould I do this if running in Authentication Mode? Is it any
Different then running within a replica set? Thanks.  
     

& Lt; TD width = "100%" & gt;

Robert stam
View Personal Data
translate to Chinese (simplified)


more options

August 1, January 19, 12:11 A.M.

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 login credentials ("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
Replica set members.

-Display referenced text-  
     

     

Alh

View Personal Data

TranslatedChinese (simplified)


 More options

January 27 08:22 P.M.

Hi Robert,

Thanks for the reply. I have ran into some issues with the suggested
Approach that I wowould 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 = Login server. 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
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 wocould in SQL Server? I am currently doing this from the Mongo
Shell in powershell using connect ("mynewdatabase"). I wocould be willing
To write a createdatabase method if you can provide guidance.

Invalid credentials for database 'sometestdatabase '.
At MongoDB. Driver. Internal. login connection. Authenticate (string
Databasename, credential credentials) in c: \ work \ 10gen \ MongoDB
\ Mongo-CSHARP-driver \ internal \ Alibaba connection. CS: Line 165
At
MongoDB. Driver. Internal. Protocol connection. checkauthentication (Relational Database
Database) in c: \ work \ 10gen \ MongoDB \ mongo-CSHARP-driver \ internal
\ Your connection. CS: Line 244
At
MongoDB. Driver. mongoserverinstance. acquireconnection (Relational Database
Database) in c: \ work \ 10gen \ MongoDB \ mongo-CSHARP-driver \ Core
\ Alibaba serverinstance. CS: Line 260
At MongoDB. Driver. multicast server. acquireconnection (Relational Database
Database, Boolean slaveok) in c: \ work \ 10gen \ MongoDB \ mongo-CSHARP-driver
\ Driver \ core \ audio server. CS: Line 1052
At MongoDB. Driver. Sort cursorenumerator '1. acquireconnection () in C:
\ Work \ 10gen \ MongoDB \ mongo-CSHARP-driver \ Core
\ Mongocursorenumerator. CS: Line 184
At MongoDB. Driver. cursorenumerator '1. getfirst () in c: \ work
\ 10gen \ MongoDB \ mongo-CSHARP-driver \ Core
\ Mongocursorenumerator. CS: Line 194
At MongoDB. Driver. cursorenumerator '1. movenext () in c: \ work
\ 10gen \ MongoDB \ mongo-CSHARP-driver \ Core
\ Mongocursorenumerator. CS: Line 126
At MongoDB. Driver. mongodatabase. getcollectionnames () in c: \ work
\ 10gen \ MongoDB \ mongo-CSHARP-driver \ core \ mongodatabase. CS: Line
662
At MongoDB. Driver. mongodatabase. collectionexists (string
Collectionname) in c: \ work \ 10gen \ MongoDB \ mongo-CSHARP-driver \ driver
\ Core \ Alibaba database. CS: Line 305

Thanks.

Best regards,
Relayhealth
Albert L. hives

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

-Display referenced text-  
     

& Lt; TD width = "100%" & gt;

Robert stam
View Personal Data
translate to Chinese (simplified)


more options

August 1, January 27, 11:07 P.M.
If you want the default credentials supplied in the URL to be
Authenticated against the admin database you put "(Admin)" after
Username in the URL, like this:

VaR url = "MongoDB ://
Someadminuser (Admin): someadminuserpassword @ localhost: 9001"

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

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

-Display referenced text-  
     

     

Alh

View Personal Data

TranslatedChinese (simplified)


 More options

January 28 12:58 A.M.

Hi Robert,

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

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

-Display referenced text-  
     

& Lt; TD width = "100%" & gt;

Robert stam
View Personal Data
translate to Chinese (simplified)


more options

August 1, January 28, 01:05 A.M.

It's kind of buried in there:

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

-Display referenced text-  
     

& Lt; TD width = "100%" & gt;

ALH
View Personal Data
translate to Chinese (simplified)


more options

August 1, January 28, 03:06 A.M.
You are correct. I see it now. Thanks again.

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

-Display referenced text-  
     

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

It indicates that the Help page is illustrated, and you can see it. That is to say, it is too obscure:

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 shocould 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.

Well, I was too bad at English. I didn't expect its string examples to be listed.

Http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-TheC%23Driver

There was a complete

// String from http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-Connectionstrings

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.