1- NoSQL简介

  • NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL";
  • NoSQL是指非关系型的数据库,有时也称作Not Only SQL的缩写,即"不仅仅是SQL",是对非传统关系型数据库管理系统的统称;
  • NoSQL用于超大规模数据的存储(数据存储不需要固定的模式,无需多余操作就可以横向扩展);

2- MongoDB简介

  • MongoDB是一个由C++语言编写、介于关系和非关系之间、基于分布式文件存储的开源数据库系统;
  • MongoDB旨在为WEB应用提供可扩展的高性能数据存储解决方案,在高负载的情况下,可以添加更多的节点来保证服务器性能;
  • MongoDB将数据存储为一个文档(类似于 JSON 对象),数据结构由键值(key=>value)对组成,字段值可以包含其他文档,数组及文档数组;
  • 存储方式和Redis类似,但Redis是内存存储,而MongoDB是和普通数据库目录一样存储在硬盘;
MongoDB的一个显著的特点,所有的数据库和数据集合(数据表)都是在使用中创建,用户无需提前指定创建;

3- 安装与启动

3.1- 官网安装指南(推荐)

3.2- 安装示例(在CentOS7部署单节点MongoDB)

注意:
  • 与官网推荐安装方式有所不同,启动MongoDB的方式也有差异,例如需要每次声明环境变量等等。
  • 建议通过“官网安装指南”来完成MongoDB的安装及使用

3.2.1- 下载安装包

根据实际情况和需要选择下载安装参数,并下载

3.2.2- 安装并设置环境变量

[root@localhost ~]# ll
total 82988
-rw-------. 1 root root 2029 Nov 16 15:05 anaconda-ks.cfg
-rw-r--r--. 1 root root 2060 Nov 16 15:08 initial-setup-ks.cfg
-rwxrwxrwx. 1 root root 84970205 Nov 16 15:13 mongodb-linux-x86_64-rhel70-4.0.4.tgz
[root@localhost ~]#
[root@localhost ~]# mkdir -p /usr/local/mongodb
[root@localhost ~]# chmod 755 /usr/local/mongodb
[root@localhost ~]#
[root@localhost ~]# tar -zxvf mongodb-linux-x86_64-rhel70-4.0.4.tgz
mongodb-linux-x86_64-rhel70-4.0.4/README
mongodb-linux-x86_64-rhel70-4.0.4/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-4.0.4/MPL-2
mongodb-linux-x86_64-rhel70-4.0.4/LICENSE-Community.txt
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongodump
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongorestore
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongoexport
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongoimport
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongostat
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongotop
mongodb-linux-x86_64-rhel70-4.0.4/bin/bsondump
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongofiles
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongoreplay
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongod
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongos
mongodb-linux-x86_64-rhel70-4.0.4/bin/mongo
mongodb-linux-x86_64-rhel70-4.0.4/bin/install_compass
[root@localhost ~]#
[root@localhost ~]# mv mongodb-linux-x86_64-rhel70-4.0.4/bin /usr/local/mongodb/
[root@localhost ~]#
[root@localhost ~]# ll -R /usr/local/mongodb/
/usr/local/mongodb/:
total 0
drwxr-xr-x. 2 root root 231 Nov 19 15:47 bin /usr/local/mongodb/bin:
total 253744
-rwxr-xr-x. 1 root root 12393712 Nov 7 03:48 bsondump
-rwxr-xr-x. 1 root root 5792 Nov 7 04:06 install_compass
-rwxr-xr-x. 1 root root 39753304 Nov 7 04:06 mongo
-rwxr-xr-x. 1 root root 62170704 Nov 7 04:06 mongod
-rwxr-xr-x. 1 root root 14702992 Nov 7 03:49 mongodump
-rwxr-xr-x. 1 root root 12733096 Nov 7 03:48 mongoexport
-rwxr-xr-x. 1 root root 12634648 Nov 7 03:48 mongofiles
-rwxr-xr-x. 1 root root 12892232 Nov 7 03:49 mongoimport
-rwxr-xr-x. 1 root root 15800824 Nov 7 03:49 mongoreplay
-rwxr-xr-x. 1 root root 15821528 Nov 7 03:49 mongorestore
-rwxr-xr-x. 1 root root 35363672 Nov 7 04:06 mongos
-rwxr-xr-x. 1 root root 12947464 Nov 7 03:48 mongostat
-rwxr-xr-x. 1 root root 12589240 Nov 7 03:49 mongotop
[root@localhost ~]#
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]#
[root@localhost ~]# export PATH=/usr/local/mongodb/bin:$PATH
[root@localhost ~]#
[root@localhost ~]# echo $PATH
/usr/local/mongodb/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]#

注意:这里是把bin目录绝对路径临时添加到环境变量中,重启系统后,将恢复默认环境变量;

3.2.3- 创建数据库目录

[root@localhost ~]# mkdir -p /home/Anliven/data/db
[root@localhost ~]# chmod 755 /home/Anliven/data/db
MongoDB的数据存储目录不会自动创建,需要手动创建data目录,并设置合适的访问权限;
注意:MongoDB默认启动的数据库路径是“/data/db”,也必须手动创建,否则mongodb会报错;

3.2.4- 运行MongoDB服务

在命令行中执行mongo安装路径bin目录下的mongod命令来启动mongdb服务;
  • 参数“--dbpath”:指定数据库目录,数据库目录默认为“/data/db”;
  • 参数“--auth”:启用认证(MongoDB 默认安装完成后,只允许本地连接,同时不需要使用任何账号密码就可以直接连接);
[root@localhost ~]# /usr/local/mongodb/bin/mongod --dbpath=/home/Anliven/data/db --auth
2018-11-20T09:57:23.501+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] MongoDB starting : pid=18449 port=27017 dbpath=/home/Anliven/data/db 64-bit host=localhost.localdomain
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] db version v4.0.4
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] git version: f288a3bdf201007f3693c58e140056adf8b04839
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] allocator: tcmalloc
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] modules: none
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] build environment:
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] distmod: rhel70
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] distarch: x86_64
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] target_arch: x86_64
2018-11-20T09:57:23.508+0800 I CONTROL [initandlisten] options: { security: { authorization: "enabled" }, storage: { dbPath: "/home/Anliven/data/db" } }
......
......
......
从命令的日志,可以看到详细的启动信息,例如:
  • “MongoDB starting : pid=3491 port=27017 dbpath=/home/Anliven/data/db 64-bit host=localhost.localdomain”
  • options: { security: { authorization: "enabled" }, storage: { dbPath: "/home/Anliven/data/db" } }

3.2.5- 禁用SELinux

通过在/etc/selinux/config中将 SELINUX 设置为 disabled 来禁用SELinux。SELINUX=disabled
以学习为目的,建议使用此项设置,但是必须重新启动系统才能使更改生效

4- MongoDB后台管理Shell

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

4.1- 进入MongoDB Shell

执行MongoDB安装路径下bin目录中的mongo命令文件就进入MongoDB后台管理;

[Anliven@localhost ~]$ cd /usr/local/mongodb/bin/
[Anliven@localhost bin]$ ll
total 253744
-rwxr-xr-x. 1 root root 12393712 Nov 7 03:48 bsondump
-rwxr-xr-x. 1 root root 5792 Nov 7 04:06 install_compass
-rwxr-xr-x. 1 root root 39753304 Nov 7 04:06 mongo
-rwxr-xr-x. 1 root root 62170704 Nov 7 04:06 mongod
-rwxr-xr-x. 1 root root 14702992 Nov 7 03:49 mongodump
-rwxr-xr-x. 1 root root 12733096 Nov 7 03:48 mongoexport
-rwxr-xr-x. 1 root root 12634648 Nov 7 03:48 mongofiles
-rwxr-xr-x. 1 root root 12892232 Nov 7 03:49 mongoimport
-rwxr-xr-x. 1 root root 15800824 Nov 7 03:49 mongoreplay
-rwxr-xr-x. 1 root root 15821528 Nov 7 03:49 mongorestore
-rwxr-xr-x. 1 root root 35363672 Nov 7 04:06 mongos
-rwxr-xr-x. 1 root root 12947464 Nov 7 03:48 mongostat
-rwxr-xr-x. 1 root root 12589240 Nov 7 03:49 mongotop
[Anliven@localhost bin]$ ./mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("562febfc-d408-4f7c-b3a7-a230c64c2c38") }
MongoDB server version: 4.0.4
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:
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten]
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten]
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-11-19T15:52:35.148+0800 I CONTROL [initandlisten]
2018-11-19T15:52:35.149+0800 I CONTROL [initandlisten]
2018-11-19T15:52:35.149+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-11-19T15:52:35.149+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-11-19T15:52:35.149+0800 I CONTROL [initandlisten]
2018-11-19T15:52:35.149+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-11-19T15:52:35.149+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-11-19T15:52:35.149+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()
--- >

进入MongoDB Shell,默认会选择 test 文档(一个空数据库),以后进入时会选择用户指定的数据库;

  • db:查看当前选择的数据库;
  • show dbs:查看非空的数据库列表;
  • use <db name>:切换选择数据库,如果数据库存在则切换,如果不存在则直接创建;
  • exit 或者 quit() :退出MongoDB Shell;

4.2- 获取命令帮助信息

help:显示命令帮助信息;
> 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
>

db.help():显示db常用命令帮助;

> db.help()
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
db.auth(username, password)
db.cloneDatabase(fromhost) - deprecated
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) - deprecated
db.createCollection(name, {size: ..., capped: ..., max: ...})
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval() - deprecated
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getLogComponents()
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
db.dropUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1}
db.serverStatus()
db.setLogLevel(level,<component>)
db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
>

一些示例:

> db
test
>
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
> use local
switched to db local
>
> db
local
>
> exit
bye
[Anliven@localhost bin]$

在此界面不仅可以插入一些简单的数据、对插入的数据进行检索,还可以进行简单数学运算;

> 111 + 222
333
> 666 / 111
6
>

5- PyMongo

PyMongo is a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python.
PyMongo 3.7.2 Documentation:https://api.mongodb.com/python/current/

6- 参考信息

简介 - MongoDB的更多相关文章

  1. MongoDB简介---MongoDB基础用法(一)

    Mongo MongoDB是一个基于分布式文件存储的数据库.MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. MongoDB 将数据存储为一 ...

  2. MongoDB学习笔记一—简介

    MongoDB简介 MongoDB在功能和复杂性之间取得了很好的平衡,并且大大简化了原先十分复杂的任务,它具备支撑今天主流web应用的关键功能:索引.复制.分片.丰富的查询语法,特别灵活的数据模型.与 ...

  3. mongodb简介(转)

    1.简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数 ...

  4. 2.MongoDB数据库简介

    1).简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. mongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...

  5. MongoDB简介与增删改查

    一.简介 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案.MongoDB 将数据存储为一个文档,数据 ...

  6. MongoDB学习笔记-01 简介、安装

    MongoDB简介 MongoDB是一种强大.灵活.可拓展的存储方式.是一个面向文档(相当于"行"的概念)的数据库. 可拓展:通过添加服务器而增加存储量. Windows下安装 版 ...

  7. MongoDB之TextSearch简介

    MongoDB之TextSearch简介  MongoDB支持对文本内容执行文本搜索操作,其提供了索引text index和查询操作$text来完成文本搜索功能.下面我们通过一个简单的例子来体验一下M ...

  8. MongoDB学习(一)简介

    本篇主要简单介绍一下MongoDB数据库. 一.简介 mongoDB是一个开源的,基于分布式的,面向文档存储的非关系型数据库.是非关系型数据库当中功能最丰富.最像关系数据库的. mongoDB由C++ ...

  9. MongoDB(二):MongoDB简介及安装

    一.MongoDB 1.简介 MongoDB是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.旨在为WEB应用提供可扩展的高性能数据存储解决方案.在高负载的情况下,添加更多的节点(分布式 ...

随机推荐

  1. NOIP2018游记(划掉) 滚粗记

    Day0 早上摸鱼~, 打几个板子就颓废 中午出发, 在火车上颓元气+睡觉. 到了宾馆发现yhx已经帮我们拿了袋子和狗牌,于是上楼欢乐地搓起了六家统, 一直搓到10点钟才回自己房间. 有六家统就有快乐 ...

  2. 201771010134杨其菊《面向对象程序设计(java)》第十七周学习总结

    第十七周学习总结 1. 程序是一段静态的代码,它是应用程序执行的蓝本.进程是程序的一次动态执行,它对应了从代码加载.执行至执行完毕的一个完整过程.操作系统为每个进程分配一段独立的内存空间和系统资源,包 ...

  3. python基础之Day23

    1.封装 什么是? 封:明确地把属性隐藏起来 ,对外隐藏,对内开放 申请名称空间,往里面装入一系列名字 /属性(类比 类 和对象   只是装的概念) 为什么要用? __init__往对象里丢属性 封装 ...

  4. 博客三--tensorflow的队列及线程基本操作

    连接我的开源中国账号:https://my.oschina.net/u/3770644/blog/3036960查询

  5. Linux 网络编程 入门-常用函数

    网络连接无外乎服务器和客户端两方面的编程. 对于服务器大致的流程是:1---调用socket函数创建套接字 2---调用bind函数分配IP地址和端口号 3---调用listsen函数将套接字转为可接 ...

  6. PackageManagerService 学习记录 基于7.1.1源码

    参考: http://blog.csdn.net/innost/article/details/47253179 http://blog.csdn.net/gaugamela/article/deta ...

  7. Find them, Catch them POJ - 1703

    题意:N个人,M次操作,操作一:A X Y,X,Y不是同一帮派,操作二:D X Y,判断X和Y的关系. 思路:如果X和Y不是同一帮派,那X与Y+N.Y与X+N是同一帮派,如果X与Y不在同一帮派且X与Y ...

  8. resin4配置之一个resin下多个app的正确配置方法

    可能是因为resin4出来不久的原因,很多人对一个resin配置多个app不是很了解,经过几个小时的研究,有了一些小的成果,在此分享一下: 在开发的时候很多人习惯了一个resin下配置多个app,习惯 ...

  9. MySQL zip版本安装

    一直以来都习惯了使用MySQL安装文件(.exe),今天下载了一个.zip版本的MySQL,安装过程中遇到了一些问题,如下: 1.在MySQL官网上(http://dev.mysql.com/down ...

  10. jsonp 简单封装

    import originJSONP from 'jsonp' // 引入 jsonp 模块 // 对外暴露方法 jsonp // 通常传给服务端的 url 地址带参数 设计目的是希望有纯净的 url ...