启动mongodb 容器
[root@vultr ~]# docker run -d --name mymongo -p 27017:27017 --privileged=true docker.io/mongo --auth
创建用户并授权
[root@vultr ~]# docker exec -it mymongo mongo admin
> use admin
> db.createUser( {user: "admin",pwd: "admin",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
> db.auth("admin","admin")
> use nxxq
> db.createUser( {user: "test",pwd: "test",roles: [ { role: "readWrite", db: "testdb" } ]})
springboot 连接testdb数据库地址
方法1
spring.data.mongodb.database=testdb
spring.data.mongodb.host=192.168.10.111
spring.data.mongodb.port=27017
spring.data.mongodb.username=test
spring.data.mongodb.password=test
方法2
spring.data.mongodb.uri=mongodb://test:test@192.168.10.111:27017/testdb
参考网址
Docker 安装 MongoDB ,配置用户名和密码