欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Linux下Mongodb安裝和啟動配置

MongoDB是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似json的bjson格式,因此可以存儲比較復(fù)雜的數(shù)據(jù)類型。Mongo最大的特點(diǎn)是他支持的查詢語言非常強(qiáng)大,其語法有點(diǎn)類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對數(shù)據(jù)建立索引。

站在用戶的角度思考問題,與客戶深入溝通,找到漢中網(wǎng)站設(shè)計與漢中網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請域名雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋漢中地區(qū)。

它的特點(diǎn)是高性能、易部署、易使用,存儲數(shù)據(jù)非常方便。主要功能特性有:

面向集合存儲,易存儲對象類型的數(shù)據(jù)。

  • 模式自由。

  • 支持動態(tài)查詢。

  • 支持完全索引,包含內(nèi)部對象。

  • 支持查詢。

  • 支持復(fù)制和故障恢復(fù)。

  • 使用高效的二進(jìn)制數(shù)據(jù)存儲,包括大型對象(如視頻等)。

  • 自動處理碎片,以支持云計算層次的擴(kuò)展性

  • 支持RUBY,PYTHON,JAVA,C++,PHP等多種語言。

  • 文件存儲格式為BSON(一種JSON的擴(kuò)展)

  • 可通過網(wǎng)絡(luò)訪問

所謂“面向集合”(Collenction-Orented),意思是數(shù)據(jù)被分組存儲在數(shù)據(jù)集中,被稱為一個集合(Collenction)。每個 集合在數(shù)據(jù)庫中都有一個唯一的標(biāo)識名,并且可以包含無限數(shù)目的文檔。集合的概念類似關(guān)系型數(shù)據(jù)庫(RDBMS)里的表(table),不同的是它不需要定 義任何模式(schema)。

模式自由(schema-free),意味著對于存儲在mongodb數(shù)據(jù)庫中的文件,我們不需要知道它的任何結(jié)構(gòu)定義。如果需要的話,你完全可以把不同結(jié)構(gòu)的文件存儲在同一個數(shù)據(jù)庫里。

存儲在集合中的文檔,被存儲為鍵-值對的形式。鍵用于唯一標(biāo)識一個文檔,為字符串類型,而值則可以是各中復(fù)雜的文件類型。我們稱這種存儲形式為BSON(Binary Serialized dOcument Format)。

MongoDB官網(wǎng)下載地址:http://www.mongodb.org/downloads 

[root@localhost app]# tar zxvf mongodb-linux-x86_64-3.4.3.tgz 
mongodb-linux-x86_64-3.4.3/README
mongodb-linux-x86_64-3.4.3/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-3.4.3/MPL-2
mongodb-linux-x86_64-3.4.3/GNU-AGPL-3.0
mongodb-linux-x86_64-3.4.3/bin/mongodump
mongodb-linux-x86_64-3.4.3/bin/mongorestore
mongodb-linux-x86_64-3.4.3/bin/mongoexport
mongodb-linux-x86_64-3.4.3/bin/mongoimport
mongodb-linux-x86_64-3.4.3/bin/mongostat
mongodb-linux-x86_64-3.4.3/bin/mongotop
mongodb-linux-x86_64-3.4.3/bin/bsondump
mongodb-linux-x86_64-3.4.3/bin/mongofiles
mongodb-linux-x86_64-3.4.3/bin/mongooplog
mongodb-linux-x86_64-3.4.3/bin/mongoreplay
mongodb-linux-x86_64-3.4.3/bin/mongoperf
mongodb-linux-x86_64-3.4.3/bin/mongod
mongodb-linux-x86_64-3.4.3/bin/mongos
mongodb-linux-x86_64-3.4.3/bin/mongo
[root@localhost app]# mv mongodb-linux-x86_64-3.4.3 mongodb
[root@localhost app]# cd mongodb
[root@localhost mongodb]# ls
bin  GNU-AGPL-3.0  MPL-2  README  THIRD-PARTY-NOTICES
[root@localhost mongodb]# mkdir data
[root@localhost mongodb]# mkdir logs
[root@localhost mongodb]# ls
bin  data  GNU-AGPL-3.0  logs  MPL-2  README  THIRD-PARTY-NOTICES
[root@localhost mongodb]# cd logs/
[root@localhost logs]# touch mongodb.log
[root@localhost logs]# cd ../bin/
[root@localhost bin]# ls
bsondump  mongo  mongod  mongodump  mongoexport  mongofiles  mongoimport  mongooplog  mongoperf  mongoreplay  mongorestore  mongos  mongostat  mongotop
[root@localhost bin]# ./mongod -port 10001 --dbpath=/app/mongodb/data/ --logpath=/app/mongodb/logs/ --logappend
2017-04-13T10:18:16.690+0800 F CONTROL  [main] Failed global initialization: FileNotOpen: logpath "/app/mongodb/logs/" should name a file, not a directory.
[root@localhost bin]# ./mongod -port 10001 --dbpath=/app/mongodb/data/ --logpath=/app/mongodb/logs/mongodb.log --logappend  #啟動
[root@localhost bin]# ./mongod -port 10001 --dbpath=/app/mongodb/data/ --logpath=/app/mongodb/logs/mongodb.log --logappend --fork  #后臺方式啟動
about to fork child process, waiting until server is ready for connections.
forked process: 19542
child process started successfully, parent exiting
[root@localhost mongodb]# cd data/
[root@localhost data]# ls
collection-0-338357810244237439.wt  diagnostic.data                index-3-338357810244237439.wt  journal          mongod.lock    storage.bson  WiredTigerLAS.wt  WiredTiger.turtle
collection-2-338357810244237439.wt  index-1-338357810244237439.wt  index-4-338357810244237439.wt  _mdb_catalog.wt  sizeStorer.wt  WiredTiger    WiredTiger.lock   WiredTiger.wt
[root@localhost data]# ps -ef|grep -i mongo
root     19542     1  0 10:43 ?        00:00:07 ./mongod -port 10001 --dbpath=/app/mongodb/data/ --logpath=/app/mongodb/logs/mongodb.log --logappend --fork
root     19598  7588  0 10:58 pts/1    00:00:00 grep -i mongo
[root@localhost bin]#

-dbpath 數(shù)據(jù)庫路徑(數(shù)據(jù)文件)

--logpath 日志文件路徑

--master 指定為主機(jī)器

--slave 指定為從機(jī)器

--source 指定主機(jī)器的IP地址

--pologSize 指定日志文件大小不超過64M.因為resync是非常操作量大且耗時,最好通過設(shè)置一個足夠大的oplogSize來避免resync(默認(rèn)的 oplog大小是空閑磁盤大小的5%)。

--logappend 日志文件末尾添加

--port 啟用端口號,如果不指定則默認(rèn)27017

--fork 在后臺運(yùn)行

--only 指定只復(fù)制哪一個數(shù)據(jù)庫

--slavedelay 指從復(fù)制檢測的時間間隔

--auth 是否需要驗證權(quán)限登錄(用戶名和密碼)

[root@localhost bin]# ./mongod -h
-quiet	# 安靜輸出
--port arg	# 指定服務(wù)端口號,默認(rèn)端口27017
--bind_ip arg	# 綁定服務(wù)IP,若綁定127.0.0.1,則只能本機(jī)訪問,不指定默認(rèn)本地所有IP
--logpath arg	# 指定MongoDB日志文件,注意是指定文件不是目錄
--logappend	# 使用追加的方式寫日志
--pidfilepath arg	# PID File 的完整路徑,如果沒有設(shè)置,則沒有PID文件
--keyFile arg	# 集群的私鑰的完整路徑,只對于Replica Set 架構(gòu)有效
--unixSocketPrefix arg	# UNIX域套接字替代目錄,(默認(rèn)為 /tmp)
--fork	# 以守護(hù)進(jìn)程的方式運(yùn)行MongoDB,創(chuàng)建服務(wù)器進(jìn)程
--auth	# 啟用驗證
--cpu	# 定期顯示CPU的CPU利用率和iowait
--dbpath arg	# 指定數(shù)據(jù)庫路徑
--diaglog arg	# diaglog選項 0=off 1=W 2=R 3=both 7=W+some reads
--directoryperdb	# 設(shè)置每個數(shù)據(jù)庫將被保存在一個單獨(dú)的目錄
--journal	# 啟用日志選項,MongoDB的數(shù)據(jù)操作將會寫入到j(luò)ournal文件夾的文件里
--journalOptions arg	# 啟用日志診斷選項
--ipv6	# 啟用IPv6選項
--jsonp	# 允許JSONP形式通過HTTP訪問(有安全影響)
--maxConns arg	# 最大同時連接數(shù) 默認(rèn)2000
--noauth	# 不啟用驗證
--nohttpinterface	# 關(guān)閉http接口,默認(rèn)關(guān)閉27018端口訪問
--noprealloc	# 禁用數(shù)據(jù)文件預(yù)分配(往往影響性能)
--noscripting	# 禁用腳本引擎
--notablescan	# 不允許表掃描
--nounixsocket	# 禁用Unix套接字監(jiān)聽
--nssize arg (=16)	# 設(shè)置信數(shù)據(jù)庫.ns文件大小(MB)
--objcheck	# 在收到客戶數(shù)據(jù),檢查的有效性,
--profile arg	# 檔案參數(shù) 0=off 1=slow, 2=all
--quota	# 限制每個數(shù)據(jù)庫的文件數(shù),設(shè)置默認(rèn)為8
--quotaFiles arg	# number of files allower per db, requires --quota
--rest	# 開啟簡單的rest API
--repair	# 修復(fù)所有數(shù)據(jù)庫run repair on all dbs
--repairpath arg	# 修復(fù)庫生成的文件的目錄,默認(rèn)為目錄名稱dbpath
--slowms arg (=100)	# value of slow for profile and console log
--smallfiles	# 使用較小的默認(rèn)文件
--syncdelay arg (=60)	# 數(shù)據(jù)寫入磁盤的時間秒數(shù)(0=never,不推薦)
--sysinfo	# 打印一些診斷系統(tǒng)信息
--upgrade	# 如果需要升級數(shù)據(jù)庫  * Replicaton 參數(shù)

--------------------------------------------------------------------------------

--fastsync	# 從一個dbpath里啟用從庫復(fù)制服務(wù),該dbpath的數(shù)據(jù)庫是主庫的快照,可用于快速啟用同步
--autoresync	# 如果從庫與主庫同步數(shù)據(jù)差得多,自動重新同步,
--oplogSize arg	# 設(shè)置oplog的大小(MB)  * 主/從參數(shù)

--------------------------------------------------------------------------------

--master	# 主庫模式
--slave	# 從庫模式
--source arg	# 從庫 端口號
--only arg	# 指定單一的數(shù)據(jù)庫復(fù)制
--slavedelay arg	# 設(shè)置從庫同步主庫的延遲時間  * Replica set(副本集)選項:

--------------------------------------------------------------------------------

--replSet arg	# 設(shè)置副本集名稱  * Sharding(分片)選項

--------------------------------------------------------------------------------
--configsvr	# 聲明這是一個集群的config服務(wù),默認(rèn)端口27019,默認(rèn)目錄/data/configdb
--shardsvr	# 聲明這是一個集群的分片,默認(rèn)端口27018
--noMoveParanoia	# 關(guān)閉偏執(zhí)為moveChunk數(shù)據(jù)保存
[root@localhost bin]#

上述參數(shù)都可以寫入 mongod.conf 配置文檔里例如:

dbpath = /data/mongodb

logpath = /data/mongodb/mongodb.log

logappend = true

port = 27017

fork = true

auth = true

連接mongodb

[root@localhost bin]# ./mongo --port 10001
MongoDB shell version v3.4.3
connecting to: mongodb://127.0.0.1:10001/
MongoDB server version: 3.4.3
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: 
2017-04-13T10:43:51.043+0800 I STORAGE  [initandlisten] 
2017-04-13T10:43:51.043+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-04-13T10:43:51.043+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-04-13T10:43:52.492+0800 I CONTROL  [initandlisten] 
2017-04-13T10:43:52.508+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-04-13T10:43:52.508+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-04-13T10:43:52.508+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-04-13T10:43:52.508+0800 I CONTROL  [initandlisten] 
2017-04-13T10:43:52.509+0800 I CONTROL  [initandlisten] 
2017-04-13T10:43:52.509+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 1818 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
> use test;
switched to db test
>

若數(shù)據(jù)庫出現(xiàn)如不能連上,則是一個data目錄下的mongod.lock文件的問題,可以用如下的修復(fù)的命令,

[root@localhost bin]# ./mongod --repair
2017-04-14T10:40:50.902+0800 I CONTROL  [initandlisten] MongoDB starting : pid=23422 port=27017 dbpath=/data/db 64-bit host=localhost.localdomain
2017-04-14T10:40:50.902+0800 I CONTROL  [initandlisten] db version v3.4.3
2017-04-14T10:40:50.902+0800 I CONTROL  [initandlisten] git version: f07437fb5a6cca07c10bafa78365456eb1d6d5e1
2017-04-14T10:40:50.902+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2017-04-14T10:40:50.902+0800 I CONTROL  [initandlisten] modules: none
2017-04-14T10:40:50.902+0800 I CONTROL  [initandlisten] build environment:
2017-04-14T10:40:50.902+0800 I CONTROL  [initandlisten]     distarch: x86_64
2017-04-14T10:40:50.902+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2017-04-14T10:40:50.902+0800 I CONTROL  [initandlisten] options: { repair: true }
2017-04-14T10:40:50.926+0800 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2017-04-14T10:40:50.926+0800 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2017-04-14T10:40:50.926+0800 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2017-04-14T10:40:50.928+0800 I CONTROL  [initandlisten] now exiting
2017-04-14T10:40:50.928+0800 I CONTROL  [initandlisten] shutting down with code:100
[root@localhost bin]#

通過配置文件來配置Mongodb

[root@localhost /]# cd /app/mongodb
[root@localhost mongodb]# ls
bin  data  GNU-AGPL-3.0  logs  MPL-2  README  THIRD-PARTY-NOTICES
[root@localhost mongodb]# mkdir etc
[root@localhost mongodb]# mkdir pid
[root@localhost mongodb]# touch etc/mongodb.conf
[root@localhost mongodb]# vim etc/mongodb.conf 
port=10001
dbpath=/app/mongodb/data/
logpath=/app/mongodb/mongodb.log
logappend=true
maxConns=20000
fork=true
pidfilepath=/app/mongodb/pid/mongodb.pid
oplogSize=10240
directoryperdb=true
[root@localhost mongodb]# bin/mongod -f etc/mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 23503
child process started successfully, parent exiting
[root@localhost mongodb]# lsof -i :10001
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mongod  23503 root    7u  IPv4  69427      0t0  TCP *:scp-config (LISTEN)
[root@localhost mongodb]#

將mongodb添加到服務(wù)里,以服務(wù)的形式管理

[root@localhost app]# cat mongodb.sh 
#!/bin/bash
#chkconfig: 2345 10 90
#description: Mongodb server is.
# source function library
source /etc/rc.d/init.d/functions
port=10001
pidfile="/app/mongodb/pid/mongodb.pid"
lockfile="/var/lock/subsys/mongodb-server"
rootpath="/app/mongodb/"
config="/app/mongodb/etc/mongodb.conf"
binpath="/app/mongodb/bin"
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
status1(){
          local base pid pid_file=
          if [ "$#" = 0 ] ; then
              echo $"Usage: status [-p pidfile] {program}"
              return 1
           fi
           if [ "$1" = "-p" ]; then
               pid_file=$2
               shift 2
            fi
            base=${1##*/}
            # First try "pidof"
            __pids_var_run "$1" "$pid_file"
            RC=$?
            if [ -z "$pid_file" -a -z "$pid" ]; then  
                 pid="$(__pids_pidof "$1")"  
            fi
            if [ -n "$pid" ]; then
#               echo $"${base} (pid $pid) is running..."
                echo $"${base} (port $port) is running..."
                return 0
             fi
             case "$RC" in
               0)
#                echo $"${base} (pid $pid) is running..."
                 echo $"${base} (port $port) is running..."
                 return 0
                 ;;
               1)
                  echo $"${base} dead but pid file exists" 
                  return 1
                  ;;
               esac 
               if [ -f /var/lock/subsys/${base} ]; then
                   echo $"${base} dead but subsys locked"
                   return 2
                fi
                echo $"${base} is stopped"
                return 2
}
reids_status(){
               status1 -p $pidfile mongod
}
start() {
        if [ -e $pidfile ];then
            echo "Mongodb Server [port $port] aleady running......"
            exit 1
        else
            echo -n "Mongodb Server [port $i] is starting                       [ "
            $binpath/mongod -f $config > /tmp/mongod.txt 
            value=$?
            [ $value -eq 0 ] && touch $lockfile && echo -e "\033[32m OK \033[0m ]"
            cat /tmp/mongod.txt
            return $value
        fi
}
stop() {
       echo -n "Stop Mongodb Server [port $port]......"
       killproc -p $pidfile mongod && echo "Mongodb Server [port $port] has stopped"
       [ $? -eq 0 ] && rm -rf $lockfile $pidfile
}
restart() {
          stop
          start
}
case "$1" in
start)
      start
;;
stop)
     stop
;;
restart)
        restart
;;
status)
       reids_status
;;
*)
  echo $"Usage: $0 {start|stop|restart|status}"
esac
[root@localhost app]# cp mongodb.sh /etc/rc.d/init.d/mongodb
[root@localhost app]# chmod +x /etc/rc.d/init.d/mongodb
[root@localhost app]# chkconfig --add mongodb
[root@localhost app]# chkconfig --level 2345 mongodb on
[root@localhost app]# service mongodb status
mongod (port 10001) is running...
[root@localhost app]# service mongodb stop
Mongodb Server [port 10001] has stopped                    [確定]
[root@localhost app]# service mongodb start
Mongodb Server [port ] is starting                       [  OK  ]
about to fork child process, waiting until server is ready for connections.
forked process: 2416
child process started successfully, parent exiting
[root@localhost app]# service mongodb status
mongod (port 10001) is running...
[root@localhost app]#

MongoDB的系統(tǒng)日志啟動切割

可以借用系統(tǒng)的logrotate日志自動切割服務(wù),來改變mongos.log文件的大小,進(jìn)入到cd /etc/logrotate.d/目錄,創(chuàng)建一個文件:touch mongos,編輯添加如下內(nèi)容:

[root@localhost logrotate.d]# cat /opt/mongodb-3.4.6/conf/mongodb.conf 
bind_ip = 0.0.0.0
port = 9981
fork = true
dbpath = /opt/mongodb-3.4.6/data
pidfilepath = /var/run/mongod.pid
logpath = /opt/mongodb-3.4.6/logs/mongod.log
logappend = true
auth = true
[root@localhost logrotate.d]# cat /etc/logrotate.d/mongos 
/opt/mongodb-3.4.6/logs/mongod.log{
    rotate 1
    daily
    dateext
    size 100K
    postrotate
      /usr/bin/kill -SIGUSR1 `/var/run/mongod.pid 2> /dev/null` 2> /dev/null || true
    endscript
}
[root@localhost logrotate.d]#

   size:指定當(dāng)mongos.log文件超過200M時,自動切割文件

        dateext:指定切割文件時,備份文件的命名方式

        rotate 5:一次將存儲5個歸檔日志。對于第六個歸檔,時間最久的歸檔將被刪除。

        daily:日志文件將按天輪詢

        postrotate/endscript: 其它命令執(zhí)行完成后,執(zhí)行這個里面的命令

常見問題:

異常:exception in initAndListen: 12596 old lock file, terminating

原因:關(guān)閉Mongodb前,未釋放鎖,導(dǎo)致重啟時失敗

解決方法:刪除mongodb.lock文件(一般在data目錄下),重啟即可。

注意事項:用mongodump備份數(shù)據(jù)庫時,不能使用fsync和鎖,否則dump程序無法連接數(shù)據(jù)庫服務(wù)器,其他情況下使用fsync和鎖,需要注意操作可能會執(zhí)行較長時間


數(shù)據(jù)庫基本用法

show dbs:顯示數(shù)據(jù)庫列表 

show collections:顯示當(dāng)前數(shù)據(jù)庫中的集合(類似關(guān)系數(shù)據(jù)庫中的表) 

show users:顯示用戶

use <db name>:切換當(dāng)前數(shù)據(jù)庫,這和MS-SQL里面的意思一樣 

db.help():顯示數(shù)據(jù)庫操作命令,里面有很多的命令 

db.foo.help():顯示集合操作命令,同樣有很多的命令,foo指的是當(dāng)前數(shù)據(jù)庫下,一個叫foo的集合,并非真正意義上的命令 

db.foo.find():對于當(dāng)前數(shù)據(jù)庫中的foo集合進(jìn)行數(shù)據(jù)查找(由于沒有條件,會列出所有數(shù)據(jù)) 

db.foo.find( { a : 1 } ):對于當(dāng)前數(shù)據(jù)庫中的foo集合進(jìn)行查找,條件是數(shù)據(jù)中有一個屬性叫a,且a的值為1

MongoDB沒有創(chuàng)建數(shù)據(jù)庫的命令,但有類似的命令。

如:如果你想創(chuàng)建一個“myTest”的數(shù)據(jù)庫,先運(yùn)行use myTest命令,之后就做一些操作(如:db.createCollection('user')),這樣就可以創(chuàng)建一個名叫“myTest”的數(shù)據(jù)庫。

數(shù)據(jù)庫常用命令

 

1、Help查看命令提示

 help

 db.help();

 db.yourColl.help();

 db.youColl.find().help();

 rs.help();

2、切換/創(chuàng)建數(shù)據(jù)庫

 use yourDB; 當(dāng)創(chuàng)建一個集合(table)的時候會自動創(chuàng)建當(dāng)前數(shù)據(jù)庫

3、查詢所有數(shù)據(jù)庫

 show dbs;

4、刪除當(dāng)前使用數(shù)據(jù)庫

 db.dropDatabase();

5、從指定主機(jī)上克隆數(shù)據(jù)庫

 db.cloneDatabase(“127.0.0.1”); 將指定機(jī)器上的數(shù)據(jù)庫的數(shù)據(jù)克隆到當(dāng)前數(shù)據(jù)庫

6、從指定的機(jī)器上復(fù)制指定數(shù)據(jù)庫數(shù)據(jù)到某個數(shù)據(jù)庫

 db.copyDatabase("mydb", "temp", "127.0.0.1");將本機(jī)的mydb的數(shù)據(jù)復(fù)制到temp數(shù)據(jù)庫中

7、修復(fù)當(dāng)前數(shù)據(jù)庫

 db.repairDatabase();

8、查看當(dāng)前使用的數(shù)據(jù)庫

 db.getName();

 db; db和getName方法是一樣的效果,都可以查詢當(dāng)前使用的數(shù)據(jù)庫

9、顯示當(dāng)前db狀態(tài)

 db.stats();

10、當(dāng)前db版本

 db.version();

11、查看當(dāng)前db的鏈接機(jī)器地址

 db.getMongo();

 

Collection聚集集合

 

1、創(chuàng)建一個聚集集合(table)

 db.createCollection(“collName”, {size: 20, capped: 5, max: 100});

2、得到指定名稱的聚集集合(table)

 db.getCollection("account");

3、得到當(dāng)前db的所有聚集集合

 db.getCollectionNames();

4、顯示當(dāng)前db所有聚集索引的狀態(tài)

 db.printCollectionStats();

 

用戶相關(guān)

 

1、添加一個用戶

 db.addUser("name");

 db.addUser("userName", "pwd123", true); 添加用戶、設(shè)置密碼、是否只讀

2、數(shù)據(jù)庫認(rèn)證、安全模式

 db.auth("userName", "123123");

3、顯示當(dāng)前所有用戶

 show users;

4、刪除用戶

 db.removeUser("userName");

 

其他

 

1、查詢之前的錯誤信息

 db.getPrevError();

2、清除錯誤記錄

 db.resetError();

  

查看聚集集合基本信息

 

1、查看幫助 db.yourColl.help();

2、查詢當(dāng)前集合的數(shù)據(jù)條數(shù) db.yourColl.count();

3、查看數(shù)據(jù)空間大小 db.userInfo.dataSize();

4、得到當(dāng)前聚集集合所在的db db.userInfo.getDB();

5、得到當(dāng)前聚集的狀態(tài) db.userInfo.stats();

6、得到聚集集合總大小 db.userInfo.totalSize();

7、聚集集合儲存空間大小 db.userInfo.storageSize();

8、Shard版本信息 db.userInfo.getShardVersion()

9、聚集集合重命名 db.userInfo.renameCollection("users"); 將userInfo重命名為users

10、刪除當(dāng)前聚集集合 db.userInfo.drop();

 

聚集集合查詢

 

1、查詢所有記錄

db.userInfo.find();

相當(dāng)于:select* from userInfo;

默認(rèn)每頁顯示20條記錄,當(dāng)顯示不下的情況下,可以用it迭代命令查詢下一頁數(shù)據(jù)。注意:鍵入it命令不能帶“;”

但是你可以設(shè)置每頁顯示數(shù)據(jù)的大小,用DBQuery.shellBatchSize= 50;這樣每頁就顯示50條記錄了。

  

2、查詢?nèi)サ艉蟮漠?dāng)前聚集集合中的某列的重復(fù)數(shù)據(jù)

db.userInfo.distinct("name");

會過濾掉name中的相同數(shù)據(jù)

相當(dāng)于:select distict name from userInfo;

  

3、查詢age = 22的記錄

db.userInfo.find({"age": 22});

相當(dāng)于: select * from userInfo where age = 22;

  

4、查詢age > 22的記錄

db.userInfo.find({age: {$gt: 22}});

相當(dāng)于:select * from userInfo where age >22;

  

5、查詢age < 22的記錄

db.userInfo.find({age: {$lt: 22}});

相當(dāng)于:select * from userInfo where age <22;

  

6、查詢age >= 25的記錄

db.userInfo.find({age: {$gte: 25}});

相當(dāng)于:select * from userInfo where age >= 25;

  

7、查詢age <= 25的記錄

db.userInfo.find({age: {$lte: 25}});

  

8、查詢age >= 23 并且 age <= 26

db.userInfo.find({age: {$gte: 23, $lte: 26}});

  

9、查詢name中包含 mongo的數(shù)據(jù)

db.userInfo.find({name: /mongo/});

//相當(dāng)于%%

select * from userInfo where name like ‘%mongo%';

  

10、查詢name中以mongo開頭的

db.userInfo.find({name: /^mongo/});

select * from userInfo where name like ‘mongo%';

  

11、查詢指定列name、age數(shù)據(jù)

db.userInfo.find({}, {name: 1, age: 1});

相當(dāng)于:select name, age from userInfo;

當(dāng)然name也可以用true或false,當(dāng)用ture的情況下河name:1效果一樣,如果用false就是排除name,顯示name以外的列信息。

  

12、查詢指定列name、age數(shù)據(jù), age > 25

db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});

相當(dāng)于:select name, age from userInfo where age >25;

  

13、按照年齡排序

升序:db.userInfo.find().sort({age: 1});

降序:db.userInfo.find().sort({age: -1});

  

14、查詢name = zhangsan, age = 22的數(shù)據(jù)

db.userInfo.find({name: 'zhangsan', age: 22});

相當(dāng)于:select * from userInfo where name = ‘zhangsan' and age = ‘22';

  

15、查詢前5條數(shù)據(jù)

db.userInfo.find().limit(5);

相當(dāng)于:selecttop 5 * from userInfo;

  

16、查詢10條以后的數(shù)據(jù)

db.userInfo.find().skip(10);

相當(dāng)于:select * from userInfo where id not in (

selecttop 10 * from userInfo

);

  

17、查詢在5-10之間的數(shù)據(jù)

db.userInfo.find().limit(10).skip(5);

可用于分頁,limit是pageSize,skip是第幾頁*pageSize

  

18、or與 查詢

db.userInfo.find({$or: [{age: 22}, {age: 25}]});

相當(dāng)于:select * from userInfo where age = 22 or age = 25;

  

19、查詢第一條數(shù)據(jù)

db.userInfo.findOne();

相當(dāng)于:selecttop 1 * from userInfo;

db.userInfo.find().limit(1);

  

20、查詢某個結(jié)果集的記錄條數(shù)

db.userInfo.find({age: {$gte: 25}}).count();

相當(dāng)于:select count(*) from userInfo where age >= 20;

  

21、按照某列進(jìn)行排序

db.userInfo.find({***: {$exists: true}}).count();

相當(dāng)于:select count(***) from userInfo;

 

索引

 

1、創(chuàng)建索引

db.userInfo.ensureIndex({name: 1});

db.userInfo.ensureIndex({name: 1, ts: -1});

  

2、查詢當(dāng)前聚集集合所有索引

db.userInfo.getIndexes();

  

3、查看總索引記錄大小

db.userInfo.totalIndexSize();

  

4、讀取當(dāng)前集合的所有index信息

db.users.reIndex();

  

5、刪除指定索引

db.users.dropIndex("name_1");

  

6、刪除所有索引索引

db.users.dropIndexes();

  

修改、添加、刪除集合數(shù)據(jù)

 

1、添加

db.users.save({name: ‘zhangsan', age: 25, ***: true});

添加的數(shù)據(jù)的數(shù)據(jù)列,沒有固定,根據(jù)添加的數(shù)據(jù)為準(zhǔn)

  

2、修改

db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true);

相當(dāng)于:update users set name = ‘changeName' where age = 25;

  

db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);

相當(dāng)于:update users set age = age + 50 where name = ‘Lisi';

  

db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);

相當(dāng)于:update users set age = age + 50, name = ‘hoho' where name = ‘Lisi';

  

3、刪除

db.users.remove({age: 132});

  

4、查詢修改刪除

db.users.findAndModify({

  query: {age: {$gte: 25}}, 

  sort: {age: -1}, 

  update: {$set: {name: 'a2'}, $inc: {age: 2}},

  remove: true

});

  

db.runCommand({ findandmodify : "users", 

  query: {age: {$gte: 25}}, 

  sort: {age: -1}, 

  update: {$set: {name: 'a2'}, $inc: {age: 2}},

  remove: true

});

update 或 remove 其中一個是必須的參數(shù); 其他參數(shù)可選。

參數(shù)

詳解

默認(rèn)值

query

查詢過濾條件

{}

sort

如果多個文檔符合查詢過濾條件,將以該參數(shù)指定的排列方式選擇出排在首位的對象,該對象將被操作

{}

remove

若為true,被選中對象將在返回前被刪除

N/A

update

一個 修改器對象

N/A

new

若為true,將返回修改后的對象而不是原始對象。在刪除操作中,該參數(shù)被忽略。

false

fields

參見Retrieving a Subset of Fields (1.5.0+)

All fields

upsert

創(chuàng)建新對象若查詢結(jié)果為空。 示例 (1.5.4+)

false

 

語句塊操作

 

1、簡單Hello World

print("Hello World!");

這種寫法調(diào)用了print函數(shù),和直接寫入"Hello World!"的效果是一樣的;

  

2、將一個對象轉(zhuǎn)換成json

tojson(new Object());

tojson(new Object('a'));

  

3、循環(huán)添加數(shù)據(jù)

> for (var i = 0; i < 30; i++) {

... db.users.save({name: "u_" + i, age: 22 + i, ***: i % 2});

... };

這樣就循環(huán)添加了30條數(shù)據(jù),同樣也可以省略括號的寫法

> for (var i = 0; i < 30; i++) db.users.save({name: "u_" + i, age: 22 + i, ***: i % 2});

也是可以的,當(dāng)你用db.users.find()查詢的時候,顯示多條數(shù)據(jù)而無法一頁顯示的情況下,可以用it查看下一頁的信息;

  

4、find 游標(biāo)查詢

>var cursor = db.users.find();

> while (cursor.hasNext()) { 

  printjson(cursor.next()); 

}

這樣就查詢所有的users信息,同樣可以這樣寫

var cursor = db.users.find();

while (cursor.hasNext()) { printjson(cursor.next); }

同樣可以省略{}號

  

5、forEach迭代循環(huán)

db.users.find().forEach(printjson);

forEach中必須傳遞一個函數(shù)來處理每條迭代的數(shù)據(jù)信息

  

6、將find游標(biāo)當(dāng)數(shù)組處理

var cursor = db.users.find();

cursor[4];

取得下標(biāo)索引為4的那條數(shù)據(jù)

既然可以當(dāng)做數(shù)組處理,那么就可以獲得它的長度:cursor.length();或者cursor.count();

那樣我們也可以用循環(huán)顯示數(shù)據(jù)

for (var i = 0, len = c.length(); i < len; i++) printjson(c[i]);

  

7、將find游標(biāo)轉(zhuǎn)換成數(shù)組

> var arr = db.users.find().toArray();

> printjson(arr[2]);

用toArray方法將其轉(zhuǎn)換為數(shù)組

  

8、定制我們自己的查詢結(jié)果

只顯示age <= 28的并且只顯示age這列數(shù)據(jù)

db.users.find({age: {$lte: 28}}, {age: 1}).forEach(printjson);

db.users.find({age: {$lte: 28}}, {age: true}).forEach(printjson);

排除age的列

db.users.find({age: {$lte: 28}}, {age: false}).forEach(printjson);

  

9、forEach傳遞函數(shù)顯示信息

db.things.find({x:4}).forEach(function(x) {print(tojson(x));});

新聞標(biāo)題:Linux下Mongodb安裝和啟動配置
轉(zhuǎn)載來源:http://chinadenli.net/article24/jpssce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT網(wǎng)站設(shè)計公司靜態(tài)網(wǎng)站網(wǎng)頁設(shè)計公司動態(tài)網(wǎng)站網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計