Centos下安装MongoDB4.2.3

Posted zjs55

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos下安装MongoDB4.2.3相关的知识,希望对你有一定的参考价值。

1、安装MongoDB

yum install mongodb-org-server-4.2.3-1.el7.x86_64.rpm 
yum install mongodb-org-tools-4.2.3-1.el7.x86_64.rpm mongodb-org-shell-4.2.3-1.el7.x86_64.rpm 

2、根目录下创建一个mongodb的文件夹,进入后再分别创建data和log文件夹,进入log文件夹创建mongo.log的文件,并且修改权限

[root@mongodb /]# cd mongodb/
[root@mongodb mongodb]# ll
total 0
[root@mongodb mongodb]# mkdir data
[root@mongodb mongodb]# mkdir log
[root@mongodb mongodb]# ll
total 0
drwxr-xr-x. 2 root root 6 Mar 11 16:36 data
drwxr-xr-x. 2 root root 6 Mar 11 16:36 log
[root@mongodb mongodb]# cd log/
[root@mongodb log]# touch mongo.log
[root@mongodb log]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 11 16:38 mongo.log
[root@mongodb /]# chown -R mongod:mongod /mongodb

3、修改配置 文件

[root@mongodb etc]# cat mongod.conf 
# 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: /mongodb/log/mongo.log          //日志路径

# Where and how to store data.
storage:
  dbPath: /mongodb/data      //数据库文件存放路径
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /mongodb/data/mongod.pid  # location of pidfile     //修改为新的路径
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 28017       //端口
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


security:
  authorization: enabled   //开启验证
#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

修改/usr/lib/systemd/system/mongod.service文件

[root@mongodb /]# cat /usr/lib/systemd/system/mongod.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/mongodb/data/mongod.pid              //路径修改为和mongod.conf一样
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target

4、开启端口

firewall-cmd --zone=public --add-port=28017/tcp --permanent
    命令含义:

    –zone #作用域

    –add-port=80/tcp #添加端口,格式为:端口/通讯协议

    –permanent #永久生效,没有此参数重启后失效

    重启防火墙
    firewall-cmd --reload #重启firewall
    systemctl stop firewalld.service #停止firewall
    systemctl disable firewalld.service #禁止firewall开机启动
    firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
    
    查看已经开放的端口:
    firewall-cmd --list-ports

5、启动服务:

systemctl daemon-reload
systemctl start mongod

6、登陆mongodb

[root@mongodb /]# mongo --port 28017
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:28017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ced6be6c-0a7f-48c5-86d9-cf19374eca62") }
MongoDB server version: 4.2.3
> use admin
switched to db admin
> db.createUser({user:"admin", pwd:"admin", roles:[{role:"root", db:"admin"}]})
//描述:使用密码admin创建用户管理员,并将权限/角色作为root用户,数据库为admin。
Successfully added user: {
    "user" : "admin",
    "roles" : [
        {
            "role" : "root",
            "db" : "admin"
        }
    ]
}
> 

7、开启验证

打开/lib/systemd/system/mongod.service文件并在’ExecStart’中,添加新选项’–auth’。

ExecStart=/usr/bin/mongod –auth –config /etc/mongod.conf

重新加载systemd服务:

[root@mongodb /]# systemctl daemon-reload
[root@mongodb /]# systemctl stop mongod
[root@mongodb /]# systemctl start mongod
[root@mongodb /]# systemctl status mongod
â?? mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-03-12 11:06:15 CST; 5s ago
     Docs: https://docs.mongodb.org/manual
  Process: 9703 ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf (code=exited, status=0/SUCCESS)
  Process: 9700 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 9695 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 9692 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 9705 (mongod)
    Tasks: 32
   CGroup: /system.slice/mongod.service
           â??â??9705 /usr/bin/mongod --auth --config /etc/mongod.conf

8、连接数据库

[root@mongodb /]# mongo --port 28017 -u admin -p admin --authenticationDatabase admin
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:28017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("035c66ec-394c-4bbe-aecf-3af87710287e") }
MongoDB server version: 4.2.3
Server has startup warnings: 
2020-03-12T11:06:15.952+0800 I  CONTROL  [initandlisten] 
2020-03-12T11:06:15.952+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is always.
2020-03-12T11:06:15.952+0800 I  CONTROL  [initandlisten] **        We suggest setting it to never
2020-03-12T11:06:15.953+0800 I  CONTROL  [initandlisten] 
2020-03-12T11:06:15.953+0800 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is always.
2020-03-12T11:06:15.953+0800 I  CONTROL  [initandlisten] **        We suggest setting it to never
2020-03-12T11:06:15.953+0800 I  CONTROL  [initandlisten] 
---
Enable MongoDBs 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()
---

> 

9、添加一新用户并开启登录权限

> db.createUser({user:"test", pwd:"test", roles:[{role:"readWrite", db:"test"}]})
Successfully added user: {
    "user" : "test",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "test"
        }
    ]
}
//创建test账号并创建test数据库,读写权限

10、将数据库导入

mongorestore --port 28017 -u test -p test -d test /backup/test

11、数据库导出:

mongodump --port 28017 -u test -p test -d test 

12、删除集合:每次restore数据库的时候先删除之前的集合

> use test
switched to db test
> show collections
LotAttribute_A011
LotAttribute_A011_bak
LotAttribute_A011_copy
LotAttribute_A012
LotAttribute_A012_bak
LotAttribute_Change_A011
LotDetectAttribute_A011
MESTemplate_A012
ProductionProperty
ProductionPropertyCategory
ProductionProperty_Test
> db.LotAttribute_A011.drop()
true

 

以上是关于Centos下安装MongoDB4.2.3的主要内容,如果未能解决你的问题,请参考以下文章

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

CentOS yum 命令出现 [Errno 14] curl#6 - "Couldn't resolve host ..." 的解决方法(代码片段

centos 7安装gitlab及使用

版本管理 GitLab 的安装及管理 (CentOS 7)

VIM 代码片段插件 ultisnips 使用教程

linux(centos)下安装git并上传代码