DB - MongoDB
阿新 • • 發佈:2018-12-04
cloc apr not license fin ive nss operator tor
1- NoSQL簡介
- NoSQL(NoSQL = Not Only SQL ),意即"不僅僅是SQL";
- NoSQL是指非關系型的數據庫,有時也稱作Not Only SQL的縮寫,即"不僅僅是SQL",是對非傳統關系型數據庫管理系統的統稱;
- NoSQL用於超大規模數據的存儲(數據存儲不需要固定的模式,無需多余操作就可以橫向擴展);
2- MongoDB簡介
HomePage:https://www.mongodb.com/- MongoDB是一個由C++語言編寫、介於關系和非關系之間、基於分布式文件存儲的開源數據庫系統;
- MongoDB旨在為WEB應用提供可擴展的高性能數據存儲解決方案,在高負載的情況下,可以添加更多的節點來保證服務器性能;
- MongoDB將數據存儲為一個文檔(類似於 JSON 對象),數據結構由鍵值(key=>value)對組成,字段值可以包含其他文檔,數組及文檔數組;
- 存儲方式和Redis類似,但Redis是內存存儲,而MongoDB是和普通數據庫目錄一樣存儲在硬盤;
3- 安裝與啟動
3.1- 官網安裝指南(推薦)
- Install MongoDB:https://docs.mongodb.com/manual/installation/
- MongoDB Community Edition on Linux:https://docs.mongodb.com/manual/administration/install-on-linux/
- MongoDB Community Edition on RedHat:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
- MongoDB Community Edition on Windows:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/
3.2- 安裝示例(在CentOS7部署單節點MongoDB)
註意:- 與官網推薦安裝方式有所不同,啟動MongoDB的方式也有差異,例如需要每次聲明環境變量等等。
- 建議通過“官網安裝指南”來完成MongoDB的安裝及使用
3.2.1- 下載安裝包
官網下載地址:https://www.mongodb.com/download-center#community 根據實際情況和需要選擇下載安裝參數,並下載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/dbMongoDB的數據存儲目錄不會自動創建,需要手動創建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教程:http://www.runoob.com/mongodb/
DB - MongoDB