1.先吧mongdb压缩包放到/usr/local 目录下
2.
解压命令
tar zxvf
解压三次
3.
分别在3个mongodb文件夹下创建文件夹mongodata
4.
/usr/local/mongodb1/bin/mongod --dbpath /usr/local/mongodb1/mongodata --port 27017 --replSet repl
/usr/local/mongodb2/bin/mongod --dbpath /usr/local/mongodb2/mongodata --port 27018 --replSet repl
/usr/local/mongodb3/bin/mongod --dbpath /usr/local/mongodb3/mongodata --port 27019 --replSet repl
config = { _id:"repl", members:[{_id:0,host:"192.168.126.153:27017"},{_id:1,host:"192.168.126.153:27018"},
{_id:2,host:"192.168.126.153:27019",arbiterOnly:true}]}
[root@localhost ~]# cd ../
[root@localhost /]# cd usr
[root@localhost usr]# cd local
[root@localhost local]# ll
total 37440
drwxr-xr-x. 2 root root 4096 Sep 23 2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 2 root root 4096 Sep 23 2011 include
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec
drwxr-xr-x. 4 root root 4096 May 10 02:26 mongodb1
drwxr-xr-x. 4 root root 4096 May 10 02:27 mongodb2
drwxr-xr-x. 4 root root 4096 May 10 02:27 mongodb3
-rw-r--r--. 1 root root 38288414 May 10 02:24 mongodb-linux-i686-3.0.5.tgz
drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin
drwxr-xr-x. 5 root root 4096 May 10 2019 share
drwxr-xr-x. 2 root root 4096 Sep 23 2011 src
[root@localhost local]# cd mongodb1
[root@localhost mongodb1]# cd bin
[root@localhost bin]# ./mongo
MongoDB shell version: 3.0.5
connecting to: test
Server has startup warnings:
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten]
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten]
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten] ** See http://dochub.mongodb.org/core/32bit
2019-05-10T04:40:49.801-0700 I CONTROL [initandlisten]
> config = { _id:"repl", members:[{_id:0,host:"192.168.126.159:27017"},{_id:1,host:"192.168.126.159:27018"},
... {_id:2,host:"192.168.126.159:27019",arbiterOnly:true}]}
{
"_id" : "repl",
"members" : [
{
"_id" : 0,
"host" : "192.168.126.159:27017"
},
{
"_id" : 1,
"host" : "192.168.126.159:27018"
},
{
"_id" : 2,
"host" : "192.168.126.159:27019",
"arbiterOnly" : true
}
]
}
>
> rs.initiate(config)
{ "ok" : 1 }
repl:OTHER> rs.status()
{
"set" : "repl",
"date" : ISODate("2019-05-10T12:11:10.401Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.126.159:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1821,
"optime" : Timestamp(1557489733, 2),
"optimeDate" : ISODate("2019-05-10T12:02:13Z"),
"electionTime" : Timestamp(1557488573, 1),
"electionDate" : ISODate("2019-05-10T11:42:53Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.126.159:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1698,
"optime" : Timestamp(1557489733, 2),
"optimeDate" : ISODate("2019-05-10T12:02:13Z"),
"lastHeartbeat" : ISODate("2019-05-10T12:11:09.335Z"),
"lastHeartbeatRecv" : ISODate("2019-05-10T12:11:09.374Z"),
"pingMs" : 0,
"syncingTo" : "192.168.126.159:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.126.159:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1698,
"lastHeartbeat" : ISODate("2019-05-10T12:11:09.334Z"),
"lastHeartbeatRecv" : ISODate("2019-05-10T12:11:09.391Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}
6.初始化
rs.initiate(config)
org.springframework.boot
spring-boot-starter-data-mongodb
2.application.yml中配置
# MongoDB URI配置 重要,添加了用户名和密码验证
spring:
data:
mongodb:
uri: mongodb://192.168.126.153:27017,192.168.126.153:27018,192.168.126.153:27019/testu?slaveOk=true&replicaSet=repl&write=1&readPreference=secondaryPreferred&connectTimeoutMS=300000
#每个主机的连接数
connections-per-host: 50
#线程队列数,它以上面connectionsPerHost值相乘的结果就是线程队列最大值
threads-allowed-to-block-for-connection-multiplier: 50
connect-timeout: 5000
socket-timeout: 3000
max-wait-time: 1500
#控制是否在一个连接时,系统会自动重试
auto-connect-retry: true
socket-keep-alive: true
3.在controler中
package com.jk.controller;
import com.jk.model.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author :mmzs
* @date :Created in 2019/5/7 21:18
* @description:
* @modified By:
* @version: $
*/
@Controller
public class usecontroller {
@Autowired
private MongoTemplate mongoTemplate;
@RequestMapping("finduse")
@ResponseBody
public int finduse() {
List users = mongoTemplate.find(new Query(), user.class);
System.out.println(users);
return 1;
}
@RequestMapping("addUser")
@ResponseBody
public int addUser() {
user user = new user();
user.setId(1);
user.setName("222");
mongoTemplate.save(user);
return 2;
}
}
model层
package com.jk.model;
import java.io.Serializable;
/**
* @author :mmzs
* @date :Created in 2019/5/7 21:17
* @description:
* @modified By:
* @version: $
*/
public class user implements Serializable {
private static final long serialVersionUID = 9147550436076328527L;
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
最后访问
http://127.0.0.1:8080/addUser
http://127.0.0.1:8080/finduse