美文网首页
Centos7.2 安装Mongodb

Centos7.2 安装Mongodb

作者: pokerface_max | 来源:发表于2019-10-09 16:05 被阅读0次

前言,以下是本人安装mongodb4.0.5版本的操作 centos7.2系统
1、下载安装包

wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-server-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-shell-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-tools-4.0.5-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/RPMS/mongodb-org-mongos-4.0.5-1.el7.x86_64.rpm

2、安装程序
安装mongodb 服务端

rpm -ivh mongodb-org-server-4.0.5-1.el7.x86_64.rpm

如果还需要用命令行连接 mongoDB,则需要安装 shell 程序包

rpm -ivh mongodb-org-shell-4.0.5-1.el7.x86_64.rpm 

如果需要一些附加工具,例如数据导入导出,则需要安装 tool 程序包

rpm -ivh mongodb-org-tools-4.0.5-1.el7.x86_64.rpm

如果需要集群则需要安装mongos

rpm -ivh mongodb-org-mongos-4.0.5-1.el7.x86_64.rpm 

3、配置文件
安装好了以后,会生成一个 /etc/mongod.conf 的配置文件,配置了 mogoDB 默认的配置。默认的配置文件如下所示

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

4、启动Mongodb

mongod -f /etc/mongod.conf
# 权限验证模式,只有权限验证通过才能访问
mongod -f /etc/mongod.conf --auth

5、命令行使用
使用mongo命令连接显示以下信息就连接成功了

# 连接
[root@zzapp-server-gp002 mongo]# mongo
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0301bb2a-0a7d-4a16-923d-3ec2946d08c6") }
MongoDB server version: 4.0.5
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-10-09T16:04:19.498+0800 I STORAGE  [initandlisten]
2019-10-09T16:04:19.498+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-10-09T16:04:19.498+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten]
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten]
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten]
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten]
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-10-09T16:04:20.039+0800 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> 
# 全部参数的连接方法:
mongo ip:port/dbname -u 用户名 -p 密码
# 切换到相应的数据库,没有则会创建新的
> use DBNAME  
# 创建用户并设置权限
> db.createUser( {user: "username",pwd: "password",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})
Successfully added user: {
    "user" : "report_dev",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}

说明:
user:用户名
pwd:密码
roles:指定用户的角色,可以用一个空数组给新用户设定空角色;在roles字段,可以指定内置角色和用户定义的角色。role里的角色可以选:
Built-In Roles(内置角色):
1. 数据库用户角色:read、readWrite;
2. 数据库管理角色:dbAdmin、dbOwner、userAdmin;
3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
4. 备份恢复角色:backup、restore;
5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
6. 超级用户角色:root
// 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
7. 内部角色:__system

具体角色:
Read:允许用户读取指定数据库
readWrite:允许用户读写指定数据库
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root:只在admin数据库中可用。超级账号,超级权限。

相关文章

网友评论

      本文标题:Centos7.2 安装Mongodb

      本文链接:https://www.haomeiwen.com/subject/yakgpctx.html