博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongodb认证后的远程连接
阅读量:2286 次
发布时间:2019-05-09

本文共 4654 字,大约阅读时间需要 15 分钟。

创建认证模式:

mongod --auth -dbpath "D:\mongodb\data\db" -logpath "D:\mongodb\data\log"
添加用户:
db.addUser("admin", ",%F23_kj~00Opoo0+\/")
建立连接:

认证连接代码:

//认证情况下的远程连接        public static void insert(TaskMode taskInfo)        {            var connectionString = "mongodb://192.168.0.1:27017";            var client = new MongoClient(connectionString);            var server = client.GetServer();            MongoCredentials credentials = new MongoCredentials("admin", "admin");            var database = server.GetDatabase("test", credentials);            var collection = database.GetCollection
("entities"); var entity = new Entity(); entity.Mode = taskInfo; collection.Insert(entity); }
///         /// 非认证情况下的本地连接        ///         ///         public static void insert(TaskMode taskInfo)        {            var connectionString = "mongodb://localhost";            var client = new MongoClient(connectionString);            var server = client.GetServer();            var database = server.GetDatabase("test");            var collection = database.GetCollection
("entities"); var entity = new Entity(); entity.Mode = taskInfo; collection.Insert(entity); }

参考:http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial

Authentication

MongoDB supports a simple and straightforward authentication mechanism. You can read about it on the.

The C# driver supports authentication in a couple of ways. As noted above in connection strings, you can specify default credentials on the connection string. The default credentials are always used as a fallback if no other credentials are supplied.

Supplying credentials can be done in two ways. First, they can be supplied to certain methods at runtime. These credentials will then be used to execute the desired functionality. The other, and more robust way, is to store credentials in a MongoCredentialsStore. MongoCredentials in the store are keyed by database, so if different databases require different users, then the credentials store is consulted first and, upon a miss, will fallback to the default credentials supplied on the connection string if they exist.

The example below uses the credential store to define admin credentials and credentials for the "foo" database. Access to databases other than "admin" or "foo" will use the connection string supplied default credentials "test".

var url = new MongoUrl("mongodb://test:user@localhost:27017");var settings = MongoClientSettings.FromUrl(url);var adminCredentials = new MongoCredentials("admin", "user", true);settings.CredentialsStore.Add("admin", adminCredentials);var fooCredentials = new MongoCredentials("foo", "user", false);settings.CredentialsStore.Add("foo", fooCredentials);var client = new MongoClient(settings);

GetServer method

You can navigate from an instance of a MongoClient to an instance of MongoServer by using the GetServer method.

MongoServer class

The MongoServer class is used to provide more control over the driver. It contains advanced ways of getting a database and pushing a sequence of operations through a single socket in order to guarantee consistency.

GetDatabase method

You can navigate from an instance of MongoServer to an instance of MongoDatabase (see next section) using one of the following GetDatabase methods or indexers:

  • MongoDatabase GetDatabase(MongoDatabaseSettings settings)
  • MongoDatabase GetDatabase(string databaseName)
  • MongoDatabase GetDatabase(string databaseName, MongoCredentials credentials)
  • MongoDatabase GetDatabase(string databaseName, MongoCredentials credentials, WriteConcern writeConcern)
  • MongoDatabase GetDatabase(string databaseName, WriteConcern writeConcern)

Sample code:

MongoClient client = new MongoClient(); // connect to localhostMongoServer server = client.GetServer();MongoDatabase test = server.GetDatabase("test");MongoCredentials credentials = new MongoCredentials("username", "password");MongoDatabase salaries = server.GetDatabase("salaries", credentials);

Most of the database settings are inherited from the server object, and the provided overloads of GetDatabase let you override a few of the most commonly used settings. To override other settings, call CreateDatabaseSettings and change any settings you want before calling GetDatabase, like this:

var databaseSettings = server.CreateDatabaseSettings("test");databaseSettings.SlaveOk = true;var database = server.GetDatabase(databaseSettings);

GetDatabase maintains a table of MongoDatabase instances it has returned before, so if you call GetDatabase again with the same parameters you get the same instance back again.

转载地址:http://zlunb.baihongyu.com/

你可能感兴趣的文章
KVM和Qemu的区别
查看>>
KVM
查看>>
NoSQL分类
查看>>
MongoDB安装
查看>>
MongoDB基础操作
查看>>
MongoDB用户权限管理
查看>>
Zabbix 安装配置
查看>>
zabbix自定义监控项
查看>>
zabbix报警功能(邮件报警)---触发器/动作
查看>>
zabbix微信报警
查看>>
微信报警脚本
查看>>
Gitlab源
查看>>
MySQL二进制安装5.7.26
查看>>
MySQL中mysqld服务器进程结构
查看>>
MySLQ存储结构 innodb 段 区 页
查看>>
踢掉一个远程登录用户
查看>>
MySQL group by的sql_mode报错
查看>>
联合索引应用细节
查看>>
Nginx地址重定向 return rewrite if
查看>>
PHP安装Redis模块
查看>>