centos7.3yum安装mongodb3.4

  1. 云栖社区>
  2. 博客>
  3. 正文

centos7.3yum安装mongodb3.4

paascloud 2017-09-07 20:13:01 浏览660

最小化安装centos7.3

服务器规划(说明: 配置静态IP请查看关于上篇环境配置的博客)

192.168.241.31 paascloud-mongo-001

安装mongodb3.x

创建yum源

[root@paascloud-mongo-001 ~]# touch  /etc/yum.repos.d/mongodb-org-3.4.repo

修改yum源

[root@paascloud-mongo-001 ~]# vi /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

安装MongoDB软件包和相关工具

[root@paascloud-mongo-001 ~]# yum install -y mongodb-org
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                       127 kB/s |  91 MB  00:12:14     
从 https://www.mongodb.org/static/pgp/server-3.4.asc 检索密钥
导入 GPG key 0xA15703C6:
 用户ID     : "MongoDB 3.4 Release Signing Key <packaging@mongodb.com>"
 指纹       : 0c49 f373 0359 a145 1858 5931 bc71 1f9b a157 03c6
 来自       : https://www.mongodb.org/static/pgp/server-3.4.asc
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mongodb-org-server-3.4.8-1.el7.x86_64                                                                                                                  1/5 
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
  正在安装    : mongodb-org-tools-3.4.8-1.el7.x86_64                                                                                                                   2/5 
  正在安装    : mongodb-org-mongos-3.4.8-1.el7.x86_64                                                                                                                  3/5 
  正在安装    : mongodb-org-shell-3.4.8-1.el7.x86_64                                                                                                                   4/5 
  正在安装    : mongodb-org-3.4.8-1.el7.x86_64                                                                                                                         5/5 
  验证中      : mongodb-org-shell-3.4.8-1.el7.x86_64                                                                                                                   1/5 
  验证中      : mongodb-org-mongos-3.4.8-1.el7.x86_64                                                                                                                  2/5 
  验证中      : mongodb-org-tools-3.4.8-1.el7.x86_64                                                                                                                   3/5 
  验证中      : mongodb-org-server-3.4.8-1.el7.x86_64                                                                                                                  4/5 
  验证中      : mongodb-org-3.4.8-1.el7.x86_64                                                                                                                         5/5 

已安装:
  mongodb-org.x86_64 0:3.4.8-1.el7                                                                                                                                         

作为依赖被安装:
  mongodb-org-mongos.x86_64 0:3.4.8-1.el7    mongodb-org-server.x86_64 0:3.4.8-1.el7    mongodb-org-shell.x86_64 0:3.4.8-1.el7    mongodb-org-tools.x86_64 0:3.4.8-1.el7   

完毕!
[root@paascloud-mongo-001 ~]# 

网速较慢, 比较耗时, 推荐tar包安装, 下载地址

禁用SELinux

[root@paascloud-mongo-001 ~]# vi /etc/selinux/config
SELINUX=disabled

启动MongoDB

## 检查 mongodb 是否允许系统启动
systemctl is-enabled mongodb

## 使 mongodb 系统启动
systemctl enable mongodb

## 启动
systemctl start mongodb

## 查看启动状态
systemctl status mongodb 

## 停止
systemctl stop mongodb

验证MongoDB已经成功启动

[root@paascloud-mongo-001 ~]# tail -f /var/log/mongodb/mongod.log
2017-09-07T20:00:27.166+0800 I CONTROL  [initandlisten] 
2017-09-07T20:00:27.166+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-09-07T20:00:27.166+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-09-07T20:00:27.166+0800 I CONTROL  [initandlisten] 
2017-09-07T20:00:27.173+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongo/diagnostic.data'
2017-09-07T20:00:27.181+0800 I INDEX    [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2017-09-07T20:00:27.181+0800 I INDEX    [initandlisten]      building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2017-09-07T20:00:27.182+0800 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
2017-09-07T20:00:27.183+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
2017-09-07T20:00:27.188+0800 I NETWORK  [thread1] waiting for connections on port 27017

设置开机启动

[root@paascloud-mongo-001 ~]# systemctl enable mongod.service

卸载MongoDB社区版

Stop MongoDB

[root@paascloud-mongo-001 ~]# systemctl stop mongod.service

Remove Packages

[root@paascloud-mongo-001 ~]# yum erase $(rpm -qa | grep mongodb-org)

Remove Data Directories

[root@paascloud-mongo-001 ~]# rm -r /var/log/mongodb
[root@paascloud-mongo-001 ~]# rm -r /var/lib/mongo

配置mongodb

开启远程访问

vim /etc/mongod.conf
#  bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.

创建用户

[root@paascloud-mongo-001 bin]# mongo localhost/admin
> use admin
switched to db admin
> db.createUser(
  {
    user: "admin",
    pwd: "paascloud",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

v2.6 docker 配置用户信息

公司使用的是2.6,默认只有admin用户admin库权限

# docker exec -it mongodb27017 /bin/bash
# cd bin
# mongo --port 27017 -u "admin" -p "password" --authenticationDatabase "admin"
> use tfc_db
> show users
> show roles
> db.addUser('tfc','password')
> db.auth('tfc','password')
> show collections
> db.createUser(
  {
    user: "xescm-dev",
    pwd: "paascloud",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

db.grantRolesToUser(
    "xescm-dev",
    [
      { role: "readWriteAnyDatabase", db: "admin" }
    ]
)

参考文档

mongodb官网