?注:spring-boot-starter-data-mongodb 2.7.5;jdk 1.8? 阿里云MongoDB是副本集實例的
在網(wǎng)上查找了一番,大多數(shù)都是教連接本地mongodb或者linux上的mongodb
阿里云上有java版連接教程,但它不是SpringBoot方法配置的,是手動寫死的很不方便。
通過程序代碼連接MongoDB副本集實例
下面進行配置,大家根據(jù)實際情況參考修改
Maven
<!-- springboot-mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
注:springboot項目,test以及web maven依賴沒有加進去,自行添加好了
yml
spring:
data:
mongodb:
# 方式一
# uri: mongodb://root:****@dds-0jld06e0ffb****.mongodb.rds.aliyuncs.com:3717/test?authSource=admin
# 方式二
host: dds-0jld06e0ffb****.mongodb.rds.aliyuncs.com
port: 3717
username: root
password: ****
database: ****
authentication-database: admin
- password:換成自己的
- database:想要連接的數(shù)據(jù)庫
- authenticationDatabase:?需要配置去認證的庫,也是需要加(因為我的賬號密碼信息在admin庫里)
- authSource:authenticationDatabase
- host:換成自己的host
注:經(jīng)我測試,url方式連接是密碼有特殊字符的需要轉(zhuǎn)譯。單獨配置password的方式不需要
網(wǎng)上有些教程說password需要加單引號,我這里連接沒問題就沒有加
如何解決連接串中賬號密碼包含特殊字符導致連接失敗的問題?
連接失敗的報錯
1、org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1.....}文章來源:http://www.zghlxwxcb.cn/news/detail-620647.html
檢查password是否正確(url方式需要轉(zhuǎn)譯的),authenticationDatabase配置了沒,url方式是在連接參數(shù)后面拼接authSource文章來源地址http://www.zghlxwxcb.cn/news/detail-620647.html
到了這里,關于SpringBoot 整合 MongoDB 連接 阿里云MongoDB的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!