mongodb认证后的远程连接
来源:互联网 发布:小学生的网络安全教育 编辑:程序博客网 时间:2024/05/18 19:19
创建认证模式:
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<Entity>("entities"); var entity = new Entity(); entity.Mode = taskInfo; collection.Insert(entity); }
/// <summary> /// 非认证情况下的本地连接 /// </summary> /// <param name="taskInfo"></param> 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<Entity>("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 thesecurity and authentication docs page.
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.
- mongodb认证后的远程连接
- php远程连接MongoDB用户认证错误问题
- 使用MongoDB命令连接远程服务器的MongoDB数据库
- 使用MongoDB命令连接远程服务器的MongoDB数据库
- 在本地使用MongoDB命令连接远程服务器的MongoDB
- 使用MongoDB命令连接远程服务器的MongoDB数据库
- 连接远程mongodb
- mongodb 实现远程连接
- mongodb 设置远程连接
- mongodb远程连接
- mongodb 监听远程连接
- mongodb实现远程连接
- mongodb不能远程连接
- mongodb设置远程连接
- mongodb不能远程连接
- MongoDB配置远程连接
- mongodb安装后的配置:注册windows服务、添加认证
- 采用认证方式连接mongodb
- Transforms CTM, 转换矩阵
- linux awk 内置变量实例
- Android中scrollview的scrollto方法不起作用的办法
- svn 命令
- c++虚函数
- mongodb认证后的远程连接
- Hibernate的jar包功能说明
- 工作中用到的网址/工具/软件整理
- android多屏幕支持
- 运行jrockit的jrcmd常见问题
- 特殊符号回车换行
- Node.Js TLS(SSL) HTTPS双向验证
- 十种做不大的老板
- 关于本博客