MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
# vim /etc/yum.repos.d/mongodb-org-3.4.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
# sudo yum install -y mongodb-org
有3种选择:
如果SELinux处于enforcing模式:
# mongodb的端口号为27017的情况,请根据实际情况修改
semanage port -a -t mongod_port_t -p tcp 27017
关闭SELinux:
# vim /etc/selinux/config
SELINUX=disabled
设置SELinux处于permissive 模式:
# vim /etc/selinux/config
SELINUX=permissive
# vim /etc/mongod.conf
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
net:
# 按照自己的需求修改
port: 27017
operationProfiling:
slowOpThresholdMs: 200
mode: slowOp
replication:
oplogSizeMB: 2048
replSetName: catalog
systemctl restart mongod
先来看一下初始化用的config,进入db终端后拷贝输入即可。
config ={
"_id" : "catalog",
"members": [
{
"_id": 0,
"host" : "node1:27017"
},
{
"_id": 1,
"host" : "node2:27018"
}
]
}
此处的27017
和27018
是对应配置文件中指定的端口号,node1
和node2
需要修改成自己的服务器的host名或ip。"_id" : "catalog"
中的catalog是配置文件中replSetName的值。
进入MongoDB操作命令行,依次输入:
rs.status()
# mongo -host 127.0.0.1 -port <配置文件指定的port>
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings:
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten]
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten]
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten]
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten]
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-10-20T17:58:46.781+0800 I CONTROL [initandlisten]
>
>
>
> config ={
... "_id" : "catalog",
... "members": [
... {
... "_id": 0,
... "host" : "node1:27017"
... },
... {
... "_id": 1,
... "host" : "node2:27017"
... }
... ]
... }
{
"_id" : "catalog",
"members" : [
{
"_id" : 0,
"host" : "node1:27017"
},
{
"_id" : 1,
"host" : "node2:27017"
}
]
}
>
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
>
catalog:PRIMARY>
catalog:PRIMARY>
catalog:PRIMARY>
catalog:PRIMARY> rs.status()
{
"set" : "catalog",
"date" : ISODate("2017-10-20T10:20:53.549Z"),
"myState" : 1,
"term" : NumberLong(4),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1508494845, 1),
"t" : NumberLong(4)
},
"appliedOpTime" : {
"ts" : Timestamp(1508494845, 1),
"t" : NumberLong(4)
},
"durableOpTime" : {
"ts" : Timestamp(1508494845, 1),
"t" : NumberLong(4)
}
},
"members" : [
{
"_id" : 0,
"name" : "node1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1327,
"optime" : {
"ts" : Timestamp(1508494845, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2017-10-20T10:20:45Z"),
"electionTime" : Timestamp(1508493663, 1),
"electionDate" : ISODate("2017-10-20T10:01:03Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "node2:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1196,
"optime" : {
"ts" : Timestamp(1508494845, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1508494845, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2017-10-20T10:20:45Z"),
"optimeDurableDate" : ISODate("2017-10-20T10:20:45Z"),
"lastHeartbeat" : ISODate("2017-10-20T10:20:51.727Z"),
"lastHeartbeatRecv" : ISODate("2017-10-20T10:20:52.810Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "node1:27017",
"configVersion" : 1
}
],
"ok" : 1
}
可以通过rs.add(host:port)来增加member,rs.remove(host:port)来删除member