mongodb认证后的远程连接

MongoDB4.0.0 远程连接及用户名密码认证登陆配置——windows 参考文章: https://docs.mongodb.com/manual/tutorial/enable-authentication/ https://docs.mongodb.com/manual/core/security-built-in-roles/    https://blog.youkuaiyun.com/Keith003/article/details/80897085   一、... 阅读详情

创建认证模式:

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 localhost
MongoServer 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.


robo pro 中文软件手册 慧鱼机器人编程软件窗口中有一个菜单栏和工具栏,上面有各种操作按钮,左面的窗口 里还有各种不同的编程模块。如果在左边出现了两个层叠的窗口,那么 ROBO Pro 没有设定在“级别 1”。 为了让 ROBO Pro 功能适应你知识的 增长,可以将 ROBO Pro 设定在级别 1 的初学者和级别 5 的专家级之间。 打开“级别(Level)”菜单看是否有标 立即下载

相关推荐

Abaqus 导出单元刚度矩阵和全局刚度矩阵

Abaqus 导出单元刚度矩阵和全局刚度矩阵

科技芯风 1万+

spring配置mongodb

参考:http://blog.youkuaiyun.com/zhouzhiwengang/article/details/51446784 参考:http://blog.youkuaiyun.com/congcong68/article/details/44545253 1、maven下载相关依赖 org.springframework.data spring-d

fuck487的博客 3620

别再只盯着ADC了!手把手教你拆解HLW8032模块的隔离与非隔离采样电路(附BOM清单)

本文深入解析HLW8032模块的隔离与非隔离采样电路设计,提供工程化决策指南。从安全间距到PCB布局,再到误差分析和BOM清单优化,手把手教你避开常见陷阱,确保产品级设计的可靠性和精度。附实用校准策略和关键元器件选型建议,助力硬件开发者提升设计水平。

weixin_29206669的博客 403

springboot使用正确的url、用户名、密码(密码为纯数字)连接mongodb出现认证失败问题

springboot连接mongodb认证失败: com.mongodb.MongoCommandException: Command failed with error 18: ‘Authentication failed.’ on server 119.29.97.99:27017. 解决方案:

weixin_43193439的博客 1137

解决Windows 10更新失败错误代码0x80070643

在Windows 10操作系统的更新过程中,用户时常会遇到各类错误代码,这些代码通常成为顺利完成更新的绊脚石。其中,错误代码0x80070643是一个较为常见的更新失败提示,它可能导致系统更新停滞不前。面对这一挑战,本文将详细介绍如何有效解决这一错误,帮助用户顺利完成Windows 10的更新。

nntxthml的博客 1828

OpenMV模块视觉测距(避障)+颜色识别+TFT-LCD显示(实现原理与代码分析)

上图所示代码实际运行的帧率比较低,主要原因有以下两个:1.硬件问题,博主使用的版本比较低,可以尝试使用高版本OpenMV2.图像滤波问题。之前加入图像滤波是想提高色块识别的准确性,但是相应的运算速度也会下降。

ediykk的博客 1万+

rpm 方式部署 MongoDB

开启远程登录后,没有认证就很不安全。需要开启auth认证。1️⃣ 安装之后默认是无认证登录方式。2️⃣ 远程连接方式未开启。下载所需的 rpm 包。

D的博客 2472

什么是论文的创新点?公式化描述算是创新点吗?

写科研论文时,挑战之一就是如何让你的工作“脱颖而出”,也就是要有创新性。不管实验多么精确、推导多么复杂,若没有真正的创新,论文很可能会被拒。关于如何创新问题上,我最近在看刘雪峰老师的《科技论文写作指南》,看到了很多关于写作的新论述,在这里和大家进行分享。

加百力 的技术博客 1926

Springboot动态读取MongoDB数据库地址并实现对目标数据库的操作

在使用springboot进行web项目的后端开发过程中,我们基本都是习惯了在配置文件application.properties中以 “ spring.data.mongodb.uri= xxxx ” 直接连接mongoDB的数据库。但在最近的项目开发过程中,遇到了需要根据前端用户输入地址来动态访问数据库的需求;而在配置文件中配置连接池虽然可以访问多个数据库,但并不能实现根据输入地址动态访问的需求。在调研过一些方法之后,我发现使用MongoClient连接池是一个比较便捷且实用的方法,在此...

qq_38736877的博客 2199

SIMPLIS仿真软件2.2-SIMPLIS快速入门2

SIMPLIS仿真软件2.2-SIMPLIS快速入门2导读 导读 本文承接上一篇文章。开始介绍SIMPLIS仿真部分。正如上一篇所讲的快速入门的定位,该文对SIMPLIS的三种分析模式不会很深入的讲解,只会记录下一些重要的设置和操作。对于SIMPLIS的三种分析模式的详细讲解,后面会专门撰写。如果读者还没有搭建上一篇文章中讲述的电路,那你可以点击这个链接免费下载所需的电路SIMPLIS_tutorial_buck_converter ,然后在这个电路的基础上继续进行以下操作。 ...

qq_44677482的博客 2万+

最小二乘(MLS)求点云法向量

MLS算法的基本思想是通过在点云的局部邻域内拟合一个光滑曲面,如平面或多项式曲面,以此来重建和处理点云数据。对于法向量估计,MLS通常在每个点的邻域内拟合局部平面,然后从该平面的法向量作为点的法向量。

twnkie的博客 744

MongoDB连接与开启用户验证

下载Robo 3T 这里用Robo 3T做为MongoDB的客户端来连接,Robo 3T即Robomongo,去官网https://robomongo.org/download 下载适合自己的版本: 这里下载的是zip压缩文件免安装版,下载完成,解压到合适的目录,双击robo3t.exe即可打开: 通过Robo 3T连接 如下图,新建一个连接并保存: 点击Connect开始连接: 连接成功: ...

凉茶微凉 4841

MTK(camera篇)Camera整体点亮

本文介绍了在MTK平台上添加新Camera Sensor的驱动开发流程。主要内容包括:1)MTK Camera采用HAL3架构,驱动层主要负责sensor电源控制和寄存器操作;2)详细说明了添加新sensor需要在多个文件中进行的修改,包括设备树配置、内核头文件、驱动列表、电源序列等;3)特别指出需要配置I2C地址表,并导入tuning文件和metadata文件以完善ISP处理流程。文章提供了完整的文件修改清单,涵盖了从内核层到HAL层的完整开发过程,为MTK平台Camera Sensor驱动开发提供了具体

weixin_49303682的博客 361

RPM方式安装MongoDB,开启远程连接,开启认证,开启oplog

rpm方式安装mongodb,开启远程登录方式,开始auth认证,开启oplog

温客行的博客 2395

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

php远程连接MongoDB用户认证错误问题

php mongo扩展安装好了之后,本地调试没问题,结果远程连接别的电脑上mongodb数据库时候,始终出现问题 try{ $m = new MongoClient("10.100.0.159:27017"); $db = $m->smaap; $db-> authenticate("xxxx","xxxx"); } catch (Exception $e){

1965

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

连接远程mongodb_Yum安装mongodb并开启用户认证及远程登录

Yum安装mongodb及开启用户认证远程登录1、 配置yum源这里我们以阿里云的源为例,内容如下:#Yum软件仓库唯一标识符[mongodb-org]#Yum软件仓库的名称描述name=MongoDB Repository#FTP(ftp://..)、HTTP(http://..)、本地(file:///..)baseurl=http://mirrors.aliyun.com/mongodb/y...

weixin_30958745的博客 278

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

mongodb3.0远程连接认证失败

最近用到mongodb,在阿里云服务器上面直接wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz下载安装包,安装的linux-64bit,version:3.0.6安装完成之后,根据网上设置了用户和密码dbpath=/usr/mongodb/datalogpath=/usr/mongodb...

weixin_33734785的博客 380

mongoDB 远程连接设置 与 认证 以及导入导出

安装环境: win10 monggdb4.2 1.找到安装路径 Program Files\MongoDB\Server\4.2\bin 拷贝mongod.cfg 到桌面 打开 修改bindIp 为0.0.0.0 保存 然后覆盖原来的mongod.cfg 最后重启monggdb 就可远程连接 2.用户认证 修改mongod.cfg ...

u013236043的博客 882

mongodb 3.4下远程连接认证失败的解决方法

原创地址:https://www.jb51.net/article/116859.htm 前言 mongodb开启或者关闭授权功能时还是挺麻烦的,需要新建服务键入mongod --auth。为了方便,我这里是建了两个服务,用到哪个就切换至哪个服务。 -- 需要授权 mongod --logpath "D:\data\log\mongodb.log" --logappend --dbpath "D...

HyEidolon的专栏 1312

MongoDB认证——从MONGODB-CR升级为SCRAM-SHA-

MongoDB数据库用Robomongo可以远程连接,但是用java代码行不行,无法认证,报下面日志的错误: 发现Robomongo的认证中有一个选项 Mechanism: 因为我使用的mongoDB版本是2.6.7,所以Mechanism只能使用MONGODB-CR而不是SCRAM-SHA 所以需要修改代码 //原来的 MongoCredential credential = Mong...

一个标题 3690

MySQL高并发读写延迟优化与实战解析

数据库系统的并发控制机制是保障ACID特性的核心技术,其核心矛盾在于事务隔离与系统性能的平衡。InnoDB存储引擎通过行锁、MVCC等机制实现并发控制,但在高并发场景下容易出现锁等待、IO堆积等问题。通过合理设置事务隔离级别、优化索引设计、拆分大事务等技术手段,可以显著提升MySQL的并发处理能力。特别是在电商秒杀、支付系统等高并发场景中,针对热点行采用乐观锁、减少锁范围等优化策略,配合读写分离、分库分表等架构方案,能有效解决吞吐量瓶颈。本文结合真实生产案例,详细分析锁监控、慢查询优化等实战经验,并探讨PM

weixin_34239592的博客 455

mongodb3.4 远程连接认证失败

mongodb开启或者关闭授权功能时还是挺麻烦的,需要新建服务键入mongod --auth。为了方便,我这里是建了两个服务,用到哪个就切换至哪个服务。 --需要授权 mongod --logpath "D:\data\log\mongodb.log" --logappend --dbpath "D:\data\db" --auth --serviceName "MongoDBServ...

weixin_30470857的博客 244

ubuntu18.04.2安装MongoDB

第1步: 导入公钥 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5 第2步:创建源列表文件MongoDB echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-o...

1512
上一篇: MongoDB 访问控制
下一篇: Mongodb CSharp Driver Tutorial
ArvinStudy
博客等级 码龄15年 83粉丝 193原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值