Linux 7 安装 mongodb 3.4

mongodb官方文档
当前最新版本 3.4.1

创建repo文件

在目录/etc/yum.repos.d/ 下面创建 mongodb-org-3.4.repo 文件
vim 编辑.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

:wq 保存退出

安装

sudo yum install -y mongodb-org

终端输出:

Loaded plugins: fastestmirror, langpacks
mongodb-org-3.4                                                                                                                                        | 2.5 kB  00:00:00
mongodb-org-3.4/7/primary_db                                                                                                                           |  48 kB  00:00:05
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org.x86_64 0:3.4.10-1.el7 will be installed
--> Processing Dependency: mongodb-org-tools = 3.4.10 for package: mongodb-org-3.4.10-1.el7.x86_64
--> Processing Dependency: mongodb-org-shell = 3.4.10 for package: mongodb-org-3.4.10-1.el7.x86_64
--> Processing Dependency: mongodb-org-server = 3.4.10 for package: mongodb-org-3.4.10-1.el7.x86_64
--> Processing Dependency: mongodb-org-mongos = 3.4.10 for package: mongodb-org-3.4.10-1.el7.x86_64
--> Running transaction check
---> Package mongodb-org-mongos.x86_64 0:3.4.10-1.el7 will be installed
---> Package mongodb-org-server.x86_64 0:3.4.10-1.el7 will be installed
---> Package mongodb-org-shell.x86_64 0:3.4.10-1.el7 will be installed
---> Package mongodb-org-tools.x86_64 0:3.4.10-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================
 Package                                        Arch                               Version                                  Repository                                   Size
==============================================================================================================================================================================
Installing:
 mongodb-org                                    x86_64                             3.4.10-1.el7                             mongodb-org-3.4                             5.8 k
Installing for dependencies:
 mongodb-org-mongos                             x86_64                             3.4.10-1.el7                             mongodb-org-3.4                              12 M
 mongodb-org-server                             x86_64                             3.4.10-1.el7                             mongodb-org-3.4                              20 M
 mongodb-org-shell                              x86_64                             3.4.10-1.el7                             mongodb-org-3.4                              11 M
 mongodb-org-tools                              x86_64                             3.4.10-1.el7                             mongodb-org-3.4                              49 M

Transaction Summary
==============================================================================================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 91 M
Installed size: 258 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mongodb-org-3.4/packages/mongodb-org-3.4.10-1.el7.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID a15703c6: NOKEYB/s |  32 kB  --:--:-- ETA
Public key for mongodb-org-3.4.10-1.el7.x86_64.rpm is not installed
(1/5): mongodb-org-3.4.10-1.el7.x86_64.rpm                                                                                                             | 5.8 kB  00:00:02
(2/5): mongodb-org-mongos-3.4.10-1.el7.x86_64.rpm                                                                                                      |  12 MB  00:02:26
(3/5): mongodb-org-shell-3.4.10-1.el7.x86_64.rpm                                                                                                       |  11 MB  00:00:28
(4/5): mongodb-org-server-3.4.10-1.el7.x86_64.rpm                                                                                                      |  20 MB  00:03:09
(5/5): mongodb-org-tools-3.4.10-1.el7.x86_64.rpm                                                                                                       |  49 MB  00:02:18
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                         298 kB/s |  91 MB  00:05:13
Retrieving key from https://www.mongodb.org/static/pgp/server-3.4.asc
Importing GPG key 0xA15703C6:
 Userid     : "MongoDB 3.4 Release Signing Key <packaging@mongodb.com>"
 Fingerprint: 0c49 f373 0359 a145 1858 5931 bc71 1f9b a157 03c6
 From       : https://www.mongodb.org/static/pgp/server-3.4.asc
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mongodb-org-shell-3.4.10-1.el7.x86_64                                                                                                                      1/5
  Installing : mongodb-org-tools-3.4.10-1.el7.x86_64                                                                                                                      2/5
  Installing : mongodb-org-mongos-3.4.10-1.el7.x86_64                                                                                                                     3/5
  Installing : mongodb-org-server-3.4.10-1.el7.x86_64                                                                                                                     4/5
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
  Installing : mongodb-org-3.4.10-1.el7.x86_64                                                                                                                            5/5
  Verifying  : mongodb-org-3.4.10-1.el7.x86_64                                                                                                                            1/5
  Verifying  : mongodb-org-server-3.4.10-1.el7.x86_64                                                                                                                     2/5
  Verifying  : mongodb-org-mongos-3.4.10-1.el7.x86_64                                                                                                                     3/5
  Verifying  : mongodb-org-tools-3.4.10-1.el7.x86_64                                                                                                                      4/5
  Verifying  : mongodb-org-shell-3.4.10-1.el7.x86_64                                                                                                                      5/5

Installed:
  mongodb-org.x86_64 0:3.4.10-1.el7

Dependency Installed:
  mongodb-org-mongos.x86_64 0:3.4.10-1.el7   mongodb-org-server.x86_64 0:3.4.10-1.el7   mongodb-org-shell.x86_64 0:3.4.10-1.el7   mongodb-org-tools.x86_64 0:3.4.10-1.el7

Complete!

安装完毕

启动数据库

sudo service mongod start

结束

sudo service mongod stop

重启

sudo service mongod restart

卸载数据库

Stop MongoDB

sudo service mongod stop

Remove Packages

sudo yum erase $(rpm -qa | grep mongodb-org)

Remove Data Directories

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo

测试试用下

官方文档从这一章节开始看
官方文档入口

先下载官方提供的测试数据

https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json
大约12M

导入测试数据

mongoimport --db test --collection restaurants --drop --file ~/primer-dataset.json

我是下载在root下了 所以直接 ~/

命令测试

官方文档连接
直接输入命令 mongo 开始

mongo

输入help查看帮助

        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        sh.help()                    sharding helpers
        rs.help()                    replica set helpers
        help admin                   administrative help
        help connect                 connecting to a db help
        help keys                    key shortcuts
        help misc                    misc things to know
        help mr                      mapreduce

        show dbs                     show database names
        show collections             show collections in current database
        show users                   show users in current database
        show profile                 show most recent system.profile entries with time >= 1ms
        show logs                    show the accessible logger names
        show log [name]              prints out the last segment of log in memory, 'global' is default
        use <db_name>                set current database
        db.foo.find()                list objects in collection foo
        db.foo.find( { a : 1 } )     list objects in foo where a == 1
        it                           result of the last line evaluated; use to further iterate
        DBQuery.shellBatchSize = x   set default number of items to display on shell
        exit                         quit the mongo shell

测试下上面的命令

查看数据库
show dbs
admin  0.000GB
local  0.000GB
test   0.005GB

这里有了一个 test 数据库,就是刚刚导入的测试数据库,默认是只有admin 和 local的

选择数据库

use test
我们测试的test

查看collections

show collections

restaurants

刚刚导入的restaurants collections

各种查询命令

大体结构是
db.CollectionName.find({ <field1>: <value1>, <field2>: <value2>, … })
官方详解
自查

插入数据
db.Students.insert({"name":"sam","age":15,"sex":"m"})
db.Students.find()

退出命令

exit
    原文作者:正为疯狂
    原文地址: https://www.jianshu.com/p/42256a2196e9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞