一、mongodb安装部署
[root@mongodb ~]# iptables -F
[root@mongodb ~]# setenforce 0
[root@mongodb ~]# systemctl stop firewalld
2.指定一个进程同一时间最多可开启的文件数
[root@mongodb ~]# ulimit -n
1024
[root@mongodb ~]# ulimit -n 65535
[root@mongodb ~]# ulimit -n
65535
3.用户最多可开启的进程数目
[root@mongodb ~]# ulimit -u
7758
[root@mongodb ~]# ulimit -u 65535
[root@mongodb ~]# ulimit -u
65535
[root@mongodb ~]#cat /etc/rc.local
ulimit –SHn 102400
4.安装版本下载地址:
[root@mongodb ~]#
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.6.tgz
[root@mongodb ~]# tar xf mongodb-linux-x86_64-rhel70-4.0.6.tgz
[root@mongodb ~]# mv mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb
[root@mongodb ~]# ln -s /usr/local/mongodb/bin/* /bin/
5.创建数据目录,日志文件及目录并创建相应配置文件
[root@mongodb ~]# mkdir -p /data/mongodb1 //数据目录
[root@mongodb ~]# mkdir -p /data/logs/mongodb //日志目录
[root@mongodb ~]# touch /data/logs/mongodb/mongodb1.log //日志文件
[root@mongodb ~]# cd /usr/local/mongodb/
[root@mongodb mongodb]# mkdir conf //配置文件目录
[root@mongodb mongodb]# vim conf/mongodb1.conf //配置文件
port=27017 //监听端口
dbpath=/data/mongodb1 //指定数据目录
logpath=/data/logs/mongodb/mongodb1.log //指定日志文件路径
logappend=true //允许写入日志
fork=true //允许创建子进程
maxConns=5000 //最大连接数
storageEngine=mmapv1 //存储引擎
6.启动MongoDB数据库,-f指定配置文件
[root@mongodb mongodb]#
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1825
child process started successfully, parent exiting
[root@mongodb ~]# netstat -lnpt | grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN
1874/mongod
[root@mongodb ~]# ps aux | grep mongod | grep -v grep
root 1874 4.7 9.3 1502244 93728 ? Sl 11:55 0:02
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
7、设置开机自动启动
[root@mongodb mongodb]# vim /etc/rc.local
rm -f /data/mongodb1/mongod.lock
mongod -f /usr/local/mongodb/conf/mongodb1.conf
[root@mongodb ~]# mongo
> show dbs
admin 0.078GB
config 0.078GB
local 0.078GB
> exit
bye
关闭MongoDB的方式:
方法一:
[root@mongodb ~]# mongo
> use admin
switched to db admin
> db.shutdownServer(); //关闭服务
> exit
bye
[root@mongodb ~]# netstat -anpt |grep mongod
方法二:
[root@mongodb ~]#
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2123
child process started successfully, parent exiting
[root@mongodb ~]# netstat -lnpt | grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN
2123/mongod
[root@mongodb ~]#
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf --shutdown
killing process with pid: 2123
[root@mongodb ~]# netstat -lnpt | grep mongod
方法三:
[root@mongodb ~]# /usr/local/mongodb/bin/mongod -f
/usr/local/mongodb/conf/mongodb1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2159
child process started successfully, parent exiting
[root@mongodb ~]# ps aux | grep mongod
root 2159 1.5 8.6 1500184 86340 ? Sl 12:14 0:00
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
root 2179 0.0 0.0 112720 984 pts/0 R+ 12:14 0:00 grep --color=auto
mongod
[root@mongodb ~]# kill -9 2159 //杀死进程号
[root@mongodb ~]# netstat -lnpt | grep mongod
Mongodb多实例配置:
[root@mongodb ~]# cd /usr/local/mongodb/conf/
[root@mongodb conf]# cp mongodb{1,2}.conf
[root@mongodb conf]# vim mongodb2.conf
port=27018 #每个端口号不能相同不然启不来服务
dbpath=/data/mongodb2 #这里要修改
logpath=/data/logs/mongodb/mongodb2.log #这里也要根据情况修改
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
[root@mongodb conf]# mkdir /data/mongodb2
[root@mongodb conf]# touch /data/logs/mongodb/mongodb2.log
[root@mongodb ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 63890
child process started successfully, parent exiting
[root@mongodb ~]# netstat -lnpt | grep mongod
cp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN
2159/mongod
tcp 0 0 127.0.0.1:27018 0.0.0.0:* LISTEN
63890/mongod
编写启停脚本
[root@mongodb ~]# vim /etc/init.d/mongodb
#!/bin/bash
INSTANCE=$1
ACTION=$2
case "$ACTION" in
'start')
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf;;
'stop')
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf --shutdown;;
'restart')
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf --shutdown
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf;;
esac
[root@mongodb ~]# chmod +x /etc/init.d/mongodb #赋权
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 stop
killing process with pid: 2159
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 stop
killing process with pid: 63890
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 start
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 start
[root@mongodb ~]# netstat -lnpt | grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN
64008/mongod
tcp 0 0 127.0.0.1:27018 0.0.0.0:* LISTEN
64030/mongod
构建MongoDB复制集集群
删除之前的实例
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 stop
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 stop (将之前所有mongodb服务都关掉,再进行下列操作,不然容易造成主宕机后不会切换从)
[root@mongodb ~]# rm -rf /data/
配置4个MongoDB实例
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb1.conf
port=27017
dbpath=/data/mongodb1
logpath=/data/logs/mongodb/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
slowms=1
profile=1
replSet=crushlinux (集群名字) //名字随意取
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb2.conf
port=27018
dbpath=/data/mongodb2
logpath=/data/logs/mongodb/mongodb2.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
slowms=1
profile=1
replSet=crushlinux
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb3.conf
port=27019
dbpath=/data/mongodb3
logpath=/data/logs/mongodb/mongodb3.log
logappend=true
maxConns=5000
storageEngine=mmapv1
slowms=1
profile=1
replSet=crushlinux
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb4.conf
port=27020
dbpath=/data/mongodb4
logpath=/data/logs/mongodb/mongodb4.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
slowms=1
profile=1
replSet=crushlinux
[root@mongodb ~]# mkdir /data/mongodb{1..4} -p
[root@mongodb ~]# mkdir /data/logs/mongodb -p
[root@mongodb ~]# touch /data/logs/mongodb/mongodb{1..4}.log
[root@mongodb ~]# chmod 777 /data/logs/mongodb/mongodb*
[root@mongodb ~]# ll /data/logs/mongodb/mongodb*
-rwxrwxrwx 1 root root 0 1月 7 16:38 /data/logs/mongodb/mongodb1.log
-rwxrwxrwx 1 root root 0 1月 7 16:38 /data/logs/mongodb/mongodb2.log
-rwxrwxrwx 1 root root 0 1月 7 16:38 /data/logs/mongodb/mongodb3.log
-rwxrwxrwx 1 root root 0 1月 7 16:38 /data/logs/mongodb/mongodb4.log
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 start
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 start
[root@mongodb ~]# /etc/init.d/mongodb mongodb3 start
[root@mongodb ~]# /etc/init.d/mongodb mongodb4 start
[root@mongodb ~]# netstat -lnpt | grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 67461/mongod
tcp 0 0 127.0.0.1:27018 0.0.0.0:* LISTEN 67487/mongod
tcp 0 0 127.0.0.1:27019 0.0.0.0:* LISTEN 67513/mongod
tcp 0 0 127.0.0.1:27020 0.0.0.0:* LISTEN 67539/mongod
[root@mongodb ~]# mongo
crushlinux:PRIMARY> rs.help() //查看复制集帮助指令
> rs.status()
{
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA
="), "keyId" : NumberLong(0)
}
}
}
> cfg={"_id":"crushlinux","members":[{"_id":0,"host":"127.0.0.1:27017"},{"_id":1,"host":"127.0.0.1:27018"},{"_id":2,"host":"127.0.0.1:27019"}]}
{
"_id" : "crushlinux",
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:27017"
},
{
"_id" : 1,
"host" : "127.0.0.1:27018"
},
{
"_id" : 2,
"host" : "127.0.0.1:27019"
}
]
}
> rs.initiate(cfg)
{
"ok" : 1,
"operationTime" : Timestamp(1578386438, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386438, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T08:41:04.997Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1, //1为健康,0为宕机
"state" : 1, //1为主,2为从
"stateStr" : "PRIMARY",
"uptime" : 125,
"optime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:40:52Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578386449, 1),
"electionDate" : ISODate("2020-01-07T08:40:49Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 26,
"optime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:40:52Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:40:52Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:41:03.888Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:41:04.492Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 26,
"optime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:40:52Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:40:52Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:41:03.888Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:41:04.441Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1578386452, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386452, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.add("127.0.0.1:27020") //增加一个节点
{
"ok" : 1,
"operationTime" : Timestamp(1578386643, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386643, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T08:44:25.733Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 326,
"optime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:44:21Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1578386449, 1),
"electionDate" : ISODate("2020-01-07T08:40:49Z"),
"configVersion" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 227,
"optime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:44:21Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:44:21Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:44:25.210Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:44:24.254Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27020",
"syncSourceHost" : "127.0.0.1:27020",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 227,
"optime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:44:21Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:44:21Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:44:25.210Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:44:24.255Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27020",
"syncSourceHost" : "127.0.0.1:27020",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 22,
"optime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:44:21Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:44:21Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:44:25.229Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:44:25.732Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 2
}
],
"ok" : 1,
"operationTime" : Timestamp(1578386661, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386661, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.remove("127.0.0.1:27020") //删除节点
{
"ok" : 1,
"operationTime" : Timestamp(1578386690, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386690, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}