mongodb.png
centos7下面安裝MongoDB服務(wù),可以采用 YUM 的方式或者 二進(jìn)制解壓縮的方式,這里采用 YUM 的方式
安裝之前檢查
- 檢查系統(tǒng)是多少位主機(jī),以便于添加對應(yīng)的yum源
- 確保 SELINUX 是disable狀態(tài)
## 檢查系統(tǒng)
root@pts/1 $ uname -a
Linux milian-mysql-slave 3.10.0-229.4.2.el7.x86_64 #1 SMP Wed May 13 10:06:09 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
## 禁止 selinux
root@pts/1 $ cat /etc/selinux/config |grep -v '#' |grep -i selinux
SELINUX=disabled
SELINUXTYPE=targeted
## 如果沒有則需要修改配置,并且重啟
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
添加Yum源
根據(jù) mongodb 官網(wǎng)提供的信息,添加 yum 源如下
cat /etc/yum.repos.d/mongodb-3.2.repos
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
*** 這里可以修改 gpgcheck=0
, 省去gpg驗(yàn)證 ***
安裝mongodb
安裝之前可以先update packages(可選操作)
yum update
*** 安裝 ***
yum -y install mongodb-org mongodb-org-server
配置,啟動(dòng)和使用 mongodb
Centos7 開始采用
systemd
來管理服務(wù)
*** 配置 ***
fork=true ## 允許程序在后臺運(yùn)行
#auth=true ## 開始認(rèn)證
logpath=/data/db/mongodb/logs/mongodb.log
logappend=true # 寫日志的模式:設(shè)置為true為追加。默認(rèn)是覆蓋
dbpath=/data/db/mongodb/data/ ## 數(shù)據(jù)存放目錄
pidfilepath=/data/db/mongodb/logs/mongodb.pid # 進(jìn)程ID,沒有指定則啟動(dòng)時(shí)候就沒有PID文件。默認(rèn)缺省。
port=27017
#bind_ip=192.168.2.73 # 綁定地址。默認(rèn)127.0.0.1,只能通過本地連接
# 設(shè)置為true,修改數(shù)據(jù)目錄存儲模式,每個(gè)數(shù)據(jù)庫的文件存儲在DBPATH指定目錄的不同的文件夾中。
# 使用此選項(xiàng),可以配置的MongoDB將數(shù)據(jù)存儲在不同的磁盤設(shè)備上,以提高寫入吞吐量或磁盤容量。默認(rèn)為false。
# 建議一開始就配置次選項(xiàng)
directoryperdb=true
# 禁止日志
# 對應(yīng) journal 啟用操作日志,以確保寫入持久性和數(shù)據(jù)的一致性,會(huì)在dbpath目錄下創(chuàng)建journal目錄
nojournal = true
## max connections
# 最大連接數(shù)。默認(rèn)值:取決于系統(tǒng)(即的ulimit和文件描述符)限制。
# MongoDB中不會(huì)限制其自身的連接。當(dāng)設(shè)置大于系統(tǒng)的限制,則無效,以系統(tǒng)限制為準(zhǔn)。
# 設(shè)置該值的高于連接池和總連接數(shù)的大小,以防止尖峰時(shí)候的連接。
# 注意:不能設(shè)置該值大于20000。
maxConns=1024
但是實(shí)際配置中配置1024在系統(tǒng)的中告警 --maxConns too high, can only handle 819
, 暫時(shí)找到是什么原因
*** 啟動(dòng) Mongo ***
## 檢查 mongodb 是否允許系統(tǒng)啟動(dòng)
systemctl is-enabled mongodb
## 使 mongodb 系統(tǒng)啟動(dòng)
systemctl enable mongodb
## 啟動(dòng)
systemctl start mongodb
## 查看啟動(dòng)狀態(tài)
systemctl status mongodb
## 停止
systemctl stop mongodb
*** 使用 Mongo ***
root@pts/4 $ mongo --help
MongoDB shell version: 3.0.6
usage: mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
Options:
--shell run the shell after executing files
--nodb don't connect to mongod on startup - no
'db address' arg expected
--norc will not run the ".mongorc.js" file on
start up
--quiet be less chatty
--port arg port to connect to
--host arg server to connect to
--eval arg evaluate javascript
-h [ --help ] show this usage information
--version show version information
--verbose increase verbosity
--ipv6 enable IPv6 support (disabled by default)
--ssl use SSL for all connections
--sslCAFile arg Certificate Authority file for SSL
--sslPEMKeyFile arg PEM certificate/key file for SSL
--sslPEMKeyPassword arg password for key in PEM file for SSL
--sslCRLFile arg Certificate Revocation List file for SSL
--sslAllowInvalidHostnames allow connections to servers with
non-matching hostnames
--sslAllowInvalidCertificates allow connections to servers with invalid
certificates
--sslFIPSMode activate FIPS 140-2 mode at startup
Authentication Options:
-u [ --username ] arg username for authentication
-p [ --password ] arg password for authentication
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication
file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
*** 例子 ***
root@pts/2 $ mongo localhost/test
MongoDB shell version: 3.0.6
connecting to: localhost/test
Server has startup warnings:
2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten]
2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten]
2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-09-08T14:29:50.163+0800 I CONTROL [initandlisten]
> show dbs
local 0.078GB
weic 0.203GB
weictest 0.078GB
> use weic
switched to db weic
> show collections
actorAudio
attention
audio
system.indexes
tagInfo
> db.tagInfo.findOne()
{
"_id" : ObjectId("563c871f8105044098c6c761"),
"id" : NumberLong(1),
"name" : "聲音萌軟淑"
}
>