Linux平台安装MongoDB

安装

MongoDB 提供了 linux 各个发行版本 64 位的安装包,你可以在官网下载安装包。

安装前我们需要安装各个 Linux 平台依赖包。

Red Hat/CentOS:

sudo yum install libcurl openssl

MongoDB 源码下载地址:https://www.mongodb.com/download-center#community

实际下载使用的

wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.1-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-shell-4.4.1-1.el7.x86_64.rpm

rpm -ivh mongodb-org-server-4.4.1-1.el7.x86_64.rpm
rpm -ivh mongodb-org-shell-4.4.1-1.el7.x86_64.rpm

systemctl start mongod.service

# systemctl status mongod.service   
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2020-11-09 11:11:30 CST; 1s ago
     Docs: https://docs.mongodb.org/manual
  Process: 13527 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 13524 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 13519 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 13516 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 13532 (mongod)
    Tasks: 33
   Memory: 64.2M
   CGroup: /system.slice/mongod.service
           └─13532 /usr/bin/mongod -f /etc/mongod.conf

11月 09 11:11:28 Test-Linux systemd[1]: Starting MongoDB Database Server...
11月 09 11:11:28 Test-Linux mongod[13527]: about to fork child process, waiting until server is ready for connections.
11月 09 11:11:28 Test-Linux mongod[13527]: forked process: 13532
11月 09 11:11:30 Test-Linux systemd[1]: Started MongoDB Database Server.

配置文件路径:/etc/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: /var/log/mongodb/mongod.log

# Where and how to store data.
storage: # 数据存放路径
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  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: # 监控ip和端口
  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:

MongoDB 后台管理 Shell

MongoDB Shell 是 MongoDB 自带的交互式 Javascript shell,用来对 MongoDB 进行操作和管理的交互式环境。

当你进入 mongoDB 后台后,它默认会链接到 test 文档(数据库):

# mongo                                                                                                                                                  root@Test-Linux
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7089ab6c-ca8e-453f-bf34-723931e22f94") }
MongoDB server version: 4.4.1
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2020-11-09T11:11:28.268+08:00: ***** SERVER RESTARTED *****
        2020-11-09T11:11:29.984+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2020-11-09T11:11:29.984+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2020-11-09T11:11:29.985+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
        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()
---

# 由于它是一个JavaScript shell,您可以运行一些简单的算术运算:
> 2+2
4

# 查看所有的数据表
> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB

# 插入一些简单的数据,并对插入的数据进行检索:
> db.baidu.insert({x:10})
WriteResult({ "nInserted" : 1 })
> db.baidu.find()
{ "_id" : ObjectId("5fa8b41afc7b424f011dca74"), "x" : 10 }

# 切换数据表
> use admin
switched to db admin

# 查看当前所在数据库
> db
admin

# 退出shell
> exit
posted @ 2020-11-09 11:40  哈喽哈喽111111  阅读(249)  评论(0编辑  收藏  举报