设为首页 收藏本站
查看: 1065|回复: 0

[经验分享] mongodb 以管理员登录并创建 database

[复制链接]

尚未签到

发表于 2015-7-7 10:30:55 | 显示全部楼层 |阅读模式
  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  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  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  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  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

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-84043-1-1.html 上篇帖子: mongoDB之C#and.net Driver 下篇帖子: Mongodb 安装 以及 问题解决-摘自网络
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表