【必赢437登录】mongodb 以管理员登录并创建 data

日期:2019-11-26编辑作者:动漫动画

mongodb 以管理员登录并创建 database

创建2个服务,一个是需要验证 一个不需要

在一个有了用户名的数据库集中,即使在 admin 数据库中创建了用户,登录上去后还是不能访问其他数据库的,但是用   登录是可以的呀,虽然可以在相应数据库中再建立用户,但别的程序都不用是怎么回事?

mongod --auth --dbpath "D:MongoDBdata" --logpath "D:MongoDBlogmongod.log" --logappend --install --serviceName "MongoDBWithAuth" --serviceDisplayName "MongoDBWithAuth"

mongod  --dbpath "D:MongoDBdata" --logpath "D:MongoDBlogmongod.log" --logappend --install --serviceName "MongoDBNoAuth" --serviceDisplayName "MongoDBNoAuth"

 

2018年2月6日 更新: 如果升级到mongodb 3.6 需要加上 --bind_ip_all 不然没法通过ip远程访问

原来是要在用户名后加上 "(admin)" 标识.

删除服务命令

例如

sc delete MongoDBWithAuth

            //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# 的错误提示:

use admin
db.createUser({user:"admin",pwd:"pwd",roles:[{
    role:"root",db:"admin"
    }]})

"

创建用户角色管理员 ,可以添加用户

An unhandled exception of type 'MongoDB.Driver.MongoAuthenticationException' occurred in MongoDB.Driver.dll

use admin
db.createUser({user:"admin",pwd:"pwd",roles:[{
    role:"userAdminAnyDatabase",db:"admin"
    }]})

Additional information: Invalid credentials for database 'demoBaseaaa'.

创建数据库用户 读写权限

"

use xxx
db.createUser({user: 'test', pwd: 'testpwd', roles: [{role: 'readWrite', db: 'xxx'}]})

本来想查找 mongodb.exe 中是怎么实现的 use,结果发现它调用 js..  找了半天也没找到 C# 如何实现这样的先 use admin 再 use 普通 database 的,看来 API 和它的 shell tool 实现还是有差异.

删除用户

db.system.users.remove({"user" : "Aibol"})

补充 创建完成以后 确认一下 是否正确

Creating a database from the 10gen C# driver

必赢437登录 1

选项

image.png

   

c# 配置链接字符串

      共 7 个帖子 - 全部折叠  -  将所有内容翻译成中文(简体)  
<add name="Workflow" connectionString="url=mongodb://test:testpwd@192.168.199.100:27017/test;database=test" />

必赢437登录 2

必须要用 /test 来标识出是哪个数据库的验证

 

 

 

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.

 

     

必赢437登录 3

 

 

 

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.

- 显示引用的文字 -

 

     

必赢437登录 4

 

 

 

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:work10genmongodb
mongo-csharp-driverDriverInternalMongoConnection.cs:line 165
   at
MongoDB.Driver.Internal.MongoConnection.CheckAuthentication(MongoDatabase
database) in C:work10genmongodbmongo-csharp-driverDriverInternal
MongoConnection.cs:line 244
   at
MongoDB.Driver.MongoServerInstance.AcquireConnection(MongoDatabase
database) in C:work10genmongodbmongo-csharp-driverDriverCore
MongoServerInstance.cs:line 260
   at MongoDB.Driver.MongoServer.AcquireConnection(MongoDatabase
database, Boolean slaveOk) in C:work10genmongodbmongo-csharp-driver
DriverCoreMongoServer.cs:line 1052
   at MongoDB.Driver.MongoCursorEnumerator`1.AcquireConnection() in C:
work10genmongodbmongo-csharp-driverDriverCore
MongoCursorEnumerator.cs:line 184
   at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in C:work
10genmongodbmongo-csharp-driverDriverCore
MongoCursorEnumerator.cs:line 194
   at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in C:work
10genmongodbmongo-csharp-driverDriverCore
MongoCursorEnumerator.cs:line 126
   at MongoDB.Driver.MongoDatabase.GetCollectionNames() in C:work
10genmongodbmongo-csharp-driverDriverCoreMongoDatabase.cs:line
662
   at MongoDB.Driver.MongoDatabase.CollectionExists(String
collectionName) in C:work10genmongodbmongo-csharp-driverDriver
CoreMongoDatabase.cs:line 305

Thanks.

Best Regards,
RelayHealth
Albert L. Hives

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

- 显示引用的文字 -

 

     

必赢437登录 5

 

 

 

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:

- 显示引用的文字 -

 

     

必赢437登录 6

 

 

 

ALH  

查看个人资料   翻译成中文(简体)

 更多选项 1月28日, 上午12时58分

Hi Robert,

This totally worked though I could not find this connection string on
【必赢437登录】mongodb 以管理员登录并创建 database。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:

- 显示引用的文字 -

 

     

必赢437登录 7

 

 

 

Robert Stam  

查看个人资料   翻译成中文(简体)

 更多选项 1月28日, 上午1时05分

It's kind of buried in there:

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

- 显示引用的文字 -

 

     

必赢437登录 8

 

 

 

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.

嗯,主要是我英文太烂,又没想到它的字符串示例没列出来完.

原来有一个完整的

            //字符串来自 

 

本文由必赢437登录发布于动漫动画,转载请注明出处:【必赢437登录】mongodb 以管理员登录并创建 data

关键词:

3、ABPZero连串教程之拼多多商家工具 项目改正及

本篇内容杂而简单,不需要多租户、不需要多语言、使用MPA(多页面)、页面加载速度提升…… 刚登录系统会看到如下...

详细>>

Ali电视体验记

移动硬盘上 天猫百货店魔盒后边有贰个USB接口,把移动硬盘接入,运营暴风影音,选拔播放本地摄像,果然不负职务...

详细>>

Windows下一步步搭建本人的单身博客——使用 Gi

-此教程不仅面向计算机专业的同学,任何想搭建个人独立博客的同学,都可以来看看- --By Y.R.H -此教程不仅面向计算...

详细>>

【必赢437登录】Objective-C开发之CodeBlocks实例操作

必赢437登录,之后,点击Toolchain executables选项卡,将Compiler’s installationdirectory选择为C:GNUstepmingwbin     -fconstant-string-...

详细>>