mongo 配置文件 -> https://www.cnblogs.com/xibuhaohao/p/12580331.html
docker-compose 配置文件
1 2 3 4 5 6 7 8 9
| mongo: image: mongo:4.4.7 ports: - 27017:27017 volumes: - ./mongodb/data/db:/data/db - ./mongodb/data/log:/var/log/mongodb - ./mongodb/data/config:/etc/mongo
|
按照上面👆配置文件设置目录
/data/db/mongodb/data
ls -l
config db log
mongo 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| storage: dbPath: /data/db/mongodb/data/db journal: enabled: true
systemLog: destination: file logAppend: true path: /data/db/mongodb/data/log
net: port: 27017 bindIp: 0.0.0.0
processManagement: timeZoneInfo: /usr/share/zoneinfo
|
bindIp: 0.0.0.0
允许远程访问
docker-compose启动mongo
docker-compose up -d
docker ps
进入docker
docker ps
docker exec -it xxxxxxxxxx bash
mongo创建数据库
登录
mongo
查看数据库
show dbs
创建数据库
use wechat_spider
然后 db
查看
创建用户
1 2 3 4 5 6 7
| db.createUser( { user:"wechat", pwd:"123456", roles:[{role:"readWrite",db:"wechat_spider"}] } )
|
Java客户端链接
配置mvn
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>pring-boot-starter-data-mongodb</artifactId> </dependency>
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.0.2</version> </dependency>
|
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| spring: data: mongodb: username: 'wechat' password: '123456'
port: 27017 database: wechat_spider
host: 39.107.117.232 repositories: type: auto
|
domain
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "passenger") public class Passenger {
private String name; private String password;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; } }
|
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @RestController public class TestContoller {
@Autowired private MongoTemplate mongoTemplate;
@RequestMapping("/insert") public String insert() { Passenger passenger = new Passenger(); passenger.setName("hello"); passenger.setPassword("world1"); passenger = mongoTemplate.insert(passenger); if (passenger != null) { return "success"; } else { return "false"; } } }
|