zoukankan      html  css  js  c++  java
  • MONGODB

    一,MONGODB

    1.1,简介

    MongoDB 是由 C++语言编写一个基于分布式文件存储的开源 NoSQL 数据库系统。在高负 载的情况下,可添加更多的节点,以保证服务性能。在许多场景下用于代替传统的关系型 数据库或键/值存储方式。旨在为 Web 应用提供可扩展的高性能数据存储解决方案。

    MongoDB 提供了一个面向文档存储,操作起来比较简单和容易,可以存储比较复杂的 数据类型。最大的特点是支持的查询语言非常强大,语法优点类似于面向对象的查询语 言。几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索 引。是一个面向集合的,模式自由的文档型数据库。

     

    1.2,MongoDB 适用领域:

    l 网站数据

    l 分布式场景

    l 缓存层

    l 文档格式存储

    1.3,逻辑结构

     

    (1)文档(document):是 mongodb 的核心概念,是 mongodb 逻辑存储的最小单元

    (2)集合(collection):多个文档组成集合

    (3)数据库(database):多个集合组成数据库

    1.4,物理存储结构

    1.4.1,数据存储结构

     

    l 命名空间文件(ns)

    l 数据文件(0,1,2)

    1.4.2,日志存储结构

     

    l 系统日志文件 logpath

    l journal 日志文件

    l oplog 复制操作日志文件

    l 慢查询日志

    1.5,数据类型

    BSON 是 Binary JSON,是二进制的格式,能将 mongdb 的所有文档表示为字节字符串。

    JSON 是一种轻量级的数据交换格式。它基于 JavaScript 的一个子集

     

    1.5.1,BSON 的数据类型

    l null,代表空或者不存在

    l布尔,只有 true 和 false

    l 数字,64 位浮点数

    l 字符串,utf8 字符串

    l 数组,值或者列表可表示为数组

    l 对象,对象的数据

    1.5.2,BSON 的特点

    简单,简洁,容易理解、解析、记忆

    1.5.3,命名规则

    l 文档的键命名几乎所有 utf8 字符,只有少数例外:$开头;(空字符);_下划线开头。

    l 集合的命名几乎所有 utf8 字符,只有少数例外:$开头;(空字符);system.开头;”” 空字符串。

    l 数据库的命名几乎所有 utf8 字符,只有少数例外:””空字符串;;空格;. 点; ;

    /。

     

    二,安装搭建mongodb

    1.安装mongodb

    1.1,环境:

    CentOS-6.8 192.168.25.32/24 MongoDB

    1.2下载此次安装版本

     [root@MongodDB tmp]# wget   https://www.mongodb.com/dr/fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-        3.2.7.tgz/download

    1.3,解压安装包,并移动到/usr/local/下

     [root@MongodDB tmp]# tar zxvf mongodb-linux-x86_64-rhel62-3.2.7.tgz
     
     [root@MongodDB tmp]# mv mongodb-linux-x86_64-rhel62-3.2.7/ /usr/local/mongodb

    1.4指定同一时间最多可开启的文件数

     [root@MongodDB ~]# ulimit -n
     
     1024
     
     [root@MongodDB ~]# ulimit -n 65535
     
     [root@MongodDB ~]# ulimit -n
     
     65535

    1.5,用户最多可开启的程序数目

     [root@MongodDB ~]# ulimit -u
     
     31994
     
     [root@MongodDB ~]# ulimit -u 65535
     
     [root@MongodDB ~]# ulimit -u
     
     65535

    1.6,创建数据目录和日志文件及目录,并创建相应配置文件

     [root@MongodDB ~]# mkdir -p /data/mongodb1
     
     [root@MongodDB ~]# mkdir -p /data/logs/mongodb
     
     [root@MongodDB ~]# touch /data/logs/mongodb/mongodb1.log
     
     [root@MongodDB ~]# cd /usr/local/mongodb/
     
     [root@MongodDB mongodb]# ls
     
     bin GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES
     
     [root@MongodDB mongodb]# mkdir conf
     
     [root@MongodDB mongodb]# cat conf/mongodb1.conf
     
     port=27017
     
     dbpath=/data/mongodb1
     
     logpath=/data/logs/mongodb/mongodb1.log
     
     logappend=true
     
     fork=true
     
     maxConns=5000
     
     storageEngine=mmapv1

    1.7,启动MongoDB数据库,-f 指定配置文件

     [root@MongodDB mongodb]# /usr/local/mongodb/bin/mongod -f   /usr/local/mongodb/conf/mongodb1.conf 
     
     [root@MongodDB mongodb]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
     
     about to fork child process, waiting until server is ready for connections.
     
     forked process: 2016
     
     child process started successfully, parent exiting
     
     [root@MongodDB mongodb]# netstat -anpt | grep mongod
     
     tcp       0     0 0.0.0.0:27017               0.0.0.0:*                   LISTEN     2016/mongod        
     
     [root@MongodDB mongodb]# ps aux | grep mongdb
     
     root     2044 0.0 0.0 103324   892 pts/0   S+   20:04   0:00 grep mongdb

    1.8,设置开机自动启动

     [root@MongodDB mongodb]# cat /etc/rc.local 
     
     #!/bin/sh
     
     #
     
     # This script will be executed *after* all the other init scripts.
     
     # You can put your own initialization stuff in here if you don't
     
     # want to do the full Sys V style init stuff.
     
     
     
     touch /var/lock/subsys/local
     
     rm -f /data/mongodb1/mongod.lock
     
     /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf

    1.9,连接数据库

     [root@MongodDB mongodb]# /usr/local/mongodb/bin/mongo
     
     MongoDB shell version: 3.2.7
     
     connecting to: test
     
     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-09-02T19:50:12.394+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
     
     2019-09-02T19:50:12.394+0800 I CONTROL [initandlisten]
     
     2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten]
     
     2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
     
     2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten] **       We suggest setting it to 'never'
     
     2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten]
     
     2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
     
     2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten] **       We suggest setting it to 'never'
     
     2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten]
     
     > show dbs
     
     local 0.078GB
     
     > exit
     
     Bye

    1.10,去除启动时的报错

     [root@MongodDB mongodb]# echo never >   /sys/kernel/mm/transparent_hugepage/enabled
     
     [root@MongodDB mongodb]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
     
     [root@MongodDB mongodb]# vim .bash_profile
     
     [root@MongodDB mongodb]# cat .bash_profile
     
     alias mongo=/usr/local/mongodb/bin/mongo
     
     [root@MongodDB mongodb]# source .bash_profile

    1.11,关闭服务的三种方法

    方法一:

     [root@MongodDB ~]# mongo 
     
     > use admin
     
     switched to db admin
     
     > db.shutdownServer();
     
     server should be down...
     
     2019-09-02T20:19:49.361+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
     
     2019-09-02T20:19:49.361+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
     
     2019-09-02T20:19:49.361+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed
     
     > exit
     
     bye
     
     [root@MongodDB ~]# netstat -anpt |grep mongod

    方法二:

     [root@MongodDB ~]# /usr/local/mongodb/bin/mongod -f     /usr/local/mongodb/conf/mongodb1.conf 
     
     about to fork child process, waiting until server is ready for connections.
     
     forked process: 2081
     
     child process started successfully, parent exiting
     
     [root@MongodDB ~]# netstat -anpt | grep mongod
     
     tcp       0     0 0.0.0.0:27017               0.0.0.0:*                   LISTEN     2081/mongod        
     
     [root@MongodDB ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf --shutdown
     
     killing process with pid: 2081
     
     [root@MongodDB ~]# netstat -anpt | grep mongod

    方法三:

     [root@MongodDB ~]# /usr/local/mongodb/bin/mongod -f     /usr/local/mongodb/conf/mongodb1.conf 
     
     about to fork child process, waiting until server is ready for connections.
     
     forked process: 2102
     
     child process started successfully, parent exiting
     
     [root@MongodDB ~]# ps -ef |grep mongod
     
     root     2102     1 0 20:22 ?       00:00:00 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
     
     root     2118 1857 0 20:22 pts/0   00:00:00 grep mongod
     
     [root@MongodDB ~]# kill 2102
     
     [root@MongodDB ~]# netstat -anpt | grep mongod
     
     [root@MongodDB ~]#

    1.12,开启两个实例:

     [root@MongodDB ~]# cd /usr/local/mongodb/conf/
     
     [root@MongodDB conf]# ls
     
     mongodb1.conf
     
     [root@MongodDB conf]# cp mongodb{1,2}.conf
     
     [root@MongodDB conf]# ls
     
     mongodb1.conf mongodb2.conf
     
     [root@MongodDB conf]# cat mongodb2.conf
     
     port=27018
     
     dbpath=/data/mongodb2
     
     logpath=/data/logs/mongodb/mongodb2.log
     
     logappend=true
     
     fork=true
     
     maxConns=5000
     
     storageEngine=mmapv1
     
     [root@MongodDB conf]# mkdir /data/mongodb2
     
     [root@MongodDB conf]# touch /data/logs/mongodb/mongodb2.log
     
     [root@MongodDB conf]# chmod 777 /data/logs/mongodb/mongodb2.log

    1.13,编写启停脚本

     [root@MongodDB conf]# cd /etc/init.d/
     
     [root@MongodDB init.d]# cat mongodb
     
     #!/bin/bash
     
     INSTANCE=$1
     
     ACTION=$2
     
     case "$ACTION" in
     
     'start')
     
     /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf;;
     
     'stop')
     
     /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf --shutdown;;
     
     'restart')
     
     /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf --shutdown
     
     /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf;;
     
     esac
     
     [root@MongodDB init.d]# chmod +x mongodb
     
     [root@MongodDB init.d]# /etc/init.d/mongodb mongodb1 start
     
     about to fork child process, waiting until server is ready for connections.
     
     forked process: 2139
     
     child process started successfully, parent exiting
     
     [root@MongodDB init.d]# /etc/init.d/mongodb mongodb2 start
     
     about to fork child process, waiting until server is ready for connections.
     
     forked process: 2155
     
     child process started successfully, parent exiting
     
     [root@MongodDB init.d]# netstat -anpt |grep mongod
     
     tcp       0     0 0.0.0.0:27017               0.0.0.0:*                   LISTEN     2139/mongod        
     
     tcp       0     0 0.0.0.0:27018               0.0.0.0:*                   LISTEN     2155/mongod  

     

    到此mongodb已经搭建完毕了

     

    2,基本操作

    操作作用
    show dbs 查看当前示例下的数据库列表,等同于 show database
    show users 显示用户
    use <db_name> 切换当前数据库
    db.help() 显示数据库操作命令
    show collections 显示当前数据库中的集合,等同于 show tables
    db.mycoll.help() 显示集合操作命令,mycoll 是当前下叫做 mycoll 的集合
    db.foo.find() 对当前数据库中 foo 集合进行数据查找
     [root@MongodDB ~]# mongo 
     
     > show dbs
     
     local 0.078GB
     > show databases
     
     local 0.078GB
     
     > use benet
     
     switched to db benet
     
     > show dbs
     
     local 0.078GB
     
     > show tables
     
     > use local
     
     switched to db local
     
     > show tables
     
     startup_log
     
     system.indexes
     
     > use benet
     
     switched to db benet
     
     > db.user.insert({"id":1,"name":"Tom"});
     
     WriteResult({ "nInserted" : 1 })
     
     > show dbs
     
     benet 0.078GB
     
     local 0.078GB
     
     > show collections
     
     system.indexes
     
     user
     
     > db.user.find()
     
     { "_id" : ObjectId("5d6d0d5bea5345188da4699e"), "id" : 1, "name" : "Tom" }
     
     >
     
     bye

    查看帮助

     [root@MongodDB ~]# /usr/local/mongodb/bin/mongod --help
     
     [root@MongodDB ~]# mongo
     
     MongoDB shell version: 3.2.7
     
     connecting to: test
     
     Server has startup warnings:
     
     2019-09-02T20:31:36.534+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
     
     2019-09-02T20:31:36.534+0800 I CONTROL [initandlisten]
     
     > 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 methods:
     
      db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
     
      db.auth(username, password)
     
      db.cloneDatabase(fromhost)
     
      db.commandHelp(name) returns the help for the command
     
      db.copyDatabase(fromdb, todb, fromhost)
     
      db.createCollection(name, { size : ..., capped : ..., max : ... } )
     
      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 }

     

    2.1,Mongodb常用命令

     

    列出所有数据库:show dbs

     

    列出所有的集合:show collections

     

    使用某个数据库:use kw

     

    查看当前所在数据库:db

     

    插入数据库:db.student.insert({“name”:”kinverwu”})

     

    导入数据库:mongoimport --db test --collection restaurants --drop --file primer -dataset.json

     

    -db test 想往哪个数据库里面导入

     

    --collection restaurants 想往哪个集合中导入

     

    --drop 把集合清空

     

    --file primer-dataset.json 哪个文件

     

    删除数据库:

     

    删除所有数据:db.dropDatabase();

     

    删除指定得数据:db.restaurants.remove({“borough”:”Manhattan”})

     

    删除justOne : db.restaurants.remove({“borough”:”Queens”},{justOne:true})

     

    修改数据库:db.student.update({“name”:”小明”},{$set:{“age”:16}});

     

    更改所有匹配项目:db.student.update({“sex”:”男”},{$set:{“age”:33}},{multi:true});

     

    完整替换:db.student.update({“name”:”小明”},{“name”:”大明”,”age”:16});

     

    查找数据库:

     

    查找所有:db.restaurants.find()

     

    精确匹配:db.student.find({“score.shuxue”:70});

     

    多个条件:db.student.find({“score.shuxue”:70,”age”:12})

     

    大于条件:db.student.find({“score.yuwen”:{$gt:50}});

     

    或者:db.student.find({$or:[{“age”:9},{“age”:11}]});

     

    升降排序:db.restaurants.find().sort({“borough”:1,”address.zipcode”:1})

  • 相关阅读:
    如何配置任意目录下Web应用程序
    Eclipse配置class文件输出目录
    window下搭建Python3.7+selenium3.1.1+pycharm环境
    如何把本地项目上传到GitHub
    Git 如何把master的内容更新到分支
    更新远程代码到本地库
    如何解决本地仓库和远程仓库的冲突(Conflict)
    如何使用Java创建Excel(.xls 和 .xlsx)文件 并写入数据
    Configure Tomcat 7 to run Python CGI scripts in windows(Win7系统配置tomcat服务器,使用python进行cgi编程)
    2019年春季学期第三周作业
  • 原文地址:https://www.cnblogs.com/clllum/p/11792061.html
Copyright © 2011-2022 走看看