springboot系列-mongodb

mongodb 安装 参考 https://blog.csdn.net/wenwang3000/article/details/99319517
1.maven依赖

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>

2.创建数据库用户

db.createUser(
   {
     user: "liuli",
     pwd: "123456",
     
     roles: [{"role":"readWrite","db":"mongo"}],
    /* All built-in Roles 
     Database User Roles: read|readWrite
     Database Admin Roles: dbAdmin|dbOwner|userAdmin
     Cluster Admin Roles: clusterAdmin|clusterManager|clusterMonitor|hostManager
     Backup and Restoration Roles: backup|restore
     All-Database Roles: readAnyDatabase|readWriteAnyDatabase|userAdminAnyDatabase|dbAdminAnyDatabase
     Superuser Roles: root 
    */
    
    // authenticationRestrictions: [ {
    //     clientSource: ["192.168.0.0"],
    //     serverAddress: ["xxx.xxx.xxx.xxx"]
    //  } ],

    //mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ], 

    //passwordDigestor: "<server|client>"
   }
)

3.配置文件(application.yml)

spring:
  application:
    name: multiple-datasource-mongo
  data:
    mongodb:
      # 单机模式 (如数据库部署在内网环境,建议不使用密码)
      uri: mongodb://liuli:123456@localhost:27017/mongo
      # 无密码
      #  uri: mongodb://localhost:27017/mongo
      # 集群模式
      # mongodb://user:pwd@ip1:port1,ip2:port2/database  集群

  1. 单一数据源
    在这里插入图片描述
    参考:
    https://github.com/472732787/com-spring-multiple-datasource/tree/master/multiple-datasource-mongo

  2. 多数据源
    在这里插入图片描述
    参考:
    https://github.com/472732787/com-spring-multiple-datasource/tree/master/multiple-datasource-mongo-multiple



发布了52 篇原创文章 · 获赞 2 · 访问量 6374

猜你喜欢

转载自blog.csdn.net/wenwang3000/article/details/99676233