Mongodb - Centos7下yum安裝mongodb服務

Mongodb - Centos7下yum安裝mongodb服務

96 全棧運維 關注javascript

2016.06.13 18:33* 字數 188 閱讀 7326評論 1喜歡 8java

mongodb.pngmysql

centos7下面安裝MongoDB服務,能夠採用 YUM 的方式或者 二進制解壓縮的方式,這裏採用 YUM 的方式linux

安裝以前檢查

  • 檢查系統是多少位主機,以便於添加對應的yum源
  • 確保 SELINUX 是disable狀態
## 檢查系統
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源

根據 mongodb 官網提供的信息,添加 yum 源以下sql

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驗證 ***mongodb

安裝mongodb

安裝以前能夠先update packages(可選操做)shell

yum update

*** 安裝 ***數據庫

yum -y install mongodb-org mongodb-org-server

配置,啓動和使用 mongodb

Centos7 開始採用 systemd 來管理服務centos

*** 配置 ***api

fork=true   ## 容許程序在後臺運行
#auth=true  ## 開始認證
logpath=/data/db/mongodb/logs/mongodb.log   
logappend=true      # 寫日誌的模式:設置爲true爲追加。默認是覆蓋
dbpath=/data/db/mongodb/data/    ## 數據存放目錄
pidfilepath=/data/db/mongodb/logs/mongodb.pid    # 進程ID,沒有指定則啓動時候就沒有PID文件。默認缺省。
port=27017
#bind_ip=192.168.2.73   # 綁定地址。默認127.0.0.1,只能經過本地鏈接
# 設置爲true,修改數據目錄存儲模式,每一個數據庫的文件存儲在DBPATH指定目錄的不一樣的文件夾中。
# 使用此選項,能夠配置的MongoDB將數據存儲在不一樣的磁盤設備上,以提升寫入吞吐量或磁盤容量。默認爲false。
# 建議一開始就配置次選項
directoryperdb=true

# 禁止日誌 
# 對應 journal 啓用操做日誌,以確保寫入持久性和數據的一致性,會在dbpath目錄下建立journal目錄
nojournal = true   

## max connections
# 最大鏈接數。默認值:取決於系統(即的ulimit和文件描述符)限制。
# MongoDB中不會限制其自身的鏈接。當設置大於系統的限制,則無效,以系統限制爲準。
# 設置該值的高於鏈接池和總鏈接數的大小,以防止尖峯時候的鏈接。
# 注意:不能設置該值大於20000。
maxConns=1024

可是實際配置中配置1024在系統的中告警 --maxConns too high, can only handle 819, 暫時找到是什麼緣由

*** 啓動 Mongo ***

## 檢查 mongodb 是否容許系統啓動
systemctl is-enabled mongodb

## 使 mongodb 系統啓動
systemctl enable mongodb

## 啓動
systemctl start mongodb

## 查看啓動狀態
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" : "聲音萌軟淑"
}
>
相關文章
相關標籤/搜索