我在自己的Ubuntu服务器上安装mongodb软件,我得到的mongo字符串就像这样mongodb://xxx:[email protected] /cdn-cgi/l/email-protection:27017
,当我在本地终端中输入此字符串并使用mongosh mongodb://xxx:[email protected] /cdn-cgi/l/email-protection:27017
,工作正常,连接成功。但是当我在 Nestjs 项目中使用这个 mongodb 字符串时,当我运行npm run start
,终端输出MongoServerError: Authentication failed.
应用程序模块.ts
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const username = configService.get('MONGO_USER');
const password = configService.get('MONGO_PASS');
const database = configService.get('MONGO_DATABASE');
const host = configService.get('MONGO_HOST');
const port = configService.get('MONGO_PORT');
return {
type: 'mongodb',
host,
port,
username,
password,
database,
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
};
},
mongodb版本是4.1.3,TypeOrm版本是0.2.38。
有谁知道问题是什么以及如何解决?感谢你。
我已经弄清楚问题出在哪里了,因为我的mongodb的authSource与保存数据的数据库不是同一个。所以我应该将 authSource 选项设置为 admin,然后它就可以工作了。
return {
type: 'mongodb',
host,
port,
username,
password,
database,
authSource: 'admin',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)