Mongodb - Centos7下yum安装mongodb服务

Mongodb - Centos7下yum安装mongodb服务

96 全栈运维 关注

2016.06.13 18:33* 字数 188 阅读 7326评论 1喜欢 8

mongodb.png

centos7下面安装MongoDB服务,可以采用 YUM 的方式或者 二进制解压缩的方式,这里采用 YUM 的方式

安装之前检查

  • 检查系统是多少位主机,以便于添加对应的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 源如下

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

安装之前可以先update packages(可选操作)

yum update

*** 安装 ***

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

配置,启动和使用 mongodb

Centos7 开始采用 systemd 来管理服务

*** 配置 ***

扫描二维码关注公众号,回复: 91955 查看本文章
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" : "声音萌软淑"
}
> 

猜你喜欢

转载自my.oschina.net/u/3367404/blog/1630835
今日推荐