1.安裝MongoDB社區版
# 1. 導入MongoDB public GPG Key sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5 # 2. 添加軟件源 echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list # 3. 更新本地軟件包 sudo apt-get update # 4. 安裝MongoDB sudo apt-get install -y mongodb-org 根據教程,本地Ubuntu 16.04系統安裝的是mongodb3.6,安裝的時候提示如下,會安裝下面相關幾個軟件包: Get:1 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6/multiverse amd64 mongodb-org-shell amd64 3.6.0 [8,477 kB] Get:2 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6/multiverse amd64 mongodb-org-server amd64 3.6.0 [14.9 MB] Get:3 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6/multiverse amd64 mongodb-org-mongos amd64 3.6.0 [8,468 kB] Get:4 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6/multiverse amd64 mongodb-org-tools amd64 3.6.0 [34.9 MB] Get:5 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6/multiverse amd64 mongodb-org amd64 3.6.0 [3,524 B]
(最新版本的安裝參考:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/#install-mongodb-community-edition)
2.MongoDB的運行
# 1. 啟動MongoDB sudo service mongod start # 2. 驗證MongoDB是否啟動成功(在/var/log/mongodb/mongod.log文件中看到下面這一行就表示啟動成功) [initandlisten] waiting for connections on port 27017 # 3. 停止MongoDB進程 sudo service mongod stop # 4. 重啟MongoDB sudo service mongod restart # 5. 查看MongoDB運行狀態 sudo service mongod status
sudo vim /etc/mongod.conf 查看mongod的配置文件,可以看到它默認的數據存儲路徑、log文件路徑、IP接口等並根據需要修改:

# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: /var/lib/mongodb journal: enabled: true # engine: # mmapv1: # wiredTiger: # where to write logging data. systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log # network interfaces net: port: 27017 bindIp: 127.0.0.1 # how the process runs processManagement: timeZoneInfo: /usr/share/zoneinfo #security: #operationProfiling: #replication: #sharding: ## Enterprise-Only Options: #auditLog: #snmp:
【備注】
在配置文件中修改數據存儲路徑后(例如修改為/home/xxname/mongodb_data),若mongodb服務啟動失敗,查看日志 /var/log/mongodb/mongod.log 如果是因為數據存儲路徑的權限問題:
exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /home/xxname/mongodb_data, terminating
解決方法:
查看mongodb默認數據存儲目錄的權限:
$ ll -h | grep /var/lib/mongodb/ drwxr-xr-x 4 mongodb mongodb 4.0K 7月 25 15:05 mongodb/
對應地,通過 sudo chown -R mongodb /home/xxname/mongodb_data/ 將新數據目錄的擁有者修改為一致即可:
$ cd /home/xxname/ $ ll -h | grep mongodb_data/ drwxrwxr-x 4 mongodb xxname 4.0K 6月 1 17:46 mongodb_data/
3.MongoDB的使用
# 查看版本 $ mongod --version db version v3.6.0 git version: a57d8e71e6998a2d0afde7edc11bd23e5661c915 OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016 allocator: tcmalloc modules: none build environment: distmod: ubuntu1604 distarch: x86_64 target_arch: x86_64
# 查看幫助信息
$ mongod --help

Options: General options: -h [ --help ] show this usage information --version show version information -f [ --config ] arg configuration file specifying additional options -v [ --verbose ] [=arg(=v)] be more verbose (include multiple times for more verbosity e.g. -vvvvv) --quiet quieter output --port arg specify port number - 27017 by default --bind_ip arg comma separated list of ip addresses to listen on - localhost by default --bind_ip_all bind to all ip addresses --ipv6 enable IPv6 support (disabled by default) --listenBacklog arg (=128) set socket listen backlog size --maxConns arg max number of simultaneous connections - 1000000 by default --logpath arg log file to send write to instead of stdout - has to be a file, not directory --syslog log to system's syslog facility instead of file or stdout --syslogFacility arg syslog facility used for mongodb syslog message --logappend append to logpath instead of over-writing --logRotate arg set the log rotation behavior (rename|reopen) --timeStampFormat arg Desired format for timestamps in log messages. One of ctime, iso8601-utc or iso8601-local --pidfilepath arg full path to pidfile (if not set, no pidfile is created) --timeZoneInfo arg full path to time zone info directory, e.g. /usr/share/zoneinfo --keyFile arg private key for cluster authentication --noauth run without security --setParameter arg Set a configurable parameter --transitionToAuth For rolling access control upgrade. Attempt to authenticate over outgoing connections and proceed regardless of success. Accept incoming connections with or without authentication. --clusterAuthMode arg Authentication mode used for cluster authentication. Alternatives are (keyFile|sendKeyFile|sendX509|x509) --nounixsocket disable listening on unix sockets --unixSocketPrefix arg alternative directory for UNIX domain sockets (defaults to /tmp) --filePermissions arg permissions to set on UNIX domain socket file - 0700 by default --fork fork server process --networkMessageCompressors [=arg(=disabled)] (=snappy) Comma-separated list of compressors to use for network messages --auth run with security --clusterIpSourceWhitelist arg Network CIDR specification of permitted origin for `__system` access. --slowms arg (=100) value of slow for profile and console log --slowOpSampleRate arg (=1) fraction of slow ops to include in the profile and console log --profile arg 0=off 1=slow, 2=all --cpu periodically show cpu and iowait utilization --sysinfo print some diagnostic system information --noIndexBuildRetry don't retry any index builds that were interrupted by shutdown --noscripting disable scripting engine --notablescan do not allow table scans --shutdown kill a running server (for init scripts) Replication options: --oplogSize arg size to use (in MB) for replication op log. default is 5% of disk space (i.e. large is good) Master/slave options (old; use replica sets instead): --master master mode --slave slave mode --source arg when slave: specify master as <server:port> --only arg when slave: specify a single database to replicate --slavedelay arg specify delay (in seconds) to be used when applying master ops to slave --autoresync automatically resync if slave data is stale Replica set options: --replSet arg arg is <setname>[/<optionalseedhostlist >] --replIndexPrefetch arg specify index prefetching behavior (if secondary) [none|_id_only|all] --enableMajorityReadConcern [=arg(=1)] (=1) enables majority readConcern Sharding options: --configsvr declare this is a config db of a cluster; default port 27019; default dir /data/configdb --shardsvr declare this is a shard db of a cluster; default port 27018 SSL options: --sslOnNormalPorts use ssl on configured ports --sslMode arg set the SSL operation mode (disabled|allowSSL|preferSSL|requireSSL ) --sslPEMKeyFile arg PEM file for ssl --sslPEMKeyPassword arg PEM file password --sslClusterFile arg Key file for internal SSL authentication --sslClusterPassword arg Internal authentication key file password --sslCAFile arg Certificate Authority file for SSL --sslCRLFile arg Certificate Revocation List file for SSL --sslDisabledProtocols arg Comma separated list of TLS protocols to disable [TLS1_0,TLS1_1,TLS1_2] --sslWeakCertificateValidation allow client to connect without presenting a certificate --sslAllowConnectionsWithoutCertificates allow client to connect without presenting a certificate --sslAllowInvalidHostnames Allow server certificates to provide non-matching hostnames --sslAllowInvalidCertificates allow connections to servers with invalid certificates --sslFIPSMode activate FIPS 140-2 mode at startup Storage options: --storageEngine arg what storage engine to use - defaults to wiredTiger if no data files present --dbpath arg directory for datafiles - defaults to /data/db --directoryperdb each database will be stored in a separate directory --noprealloc disable data file preallocation - will often hurt performance --nssize arg (=16) .ns file size (in MB) for new databases --quota limits each database to a certain number of files (8 default) --quotaFiles arg number of files allowed per db, implies --quota --smallfiles use a smaller default file size --syncdelay arg (=60) seconds between disk syncs (0=never, but not recommended) --upgrade upgrade db if needed --repair run repair on all dbs --repairpath arg root directory for repair files - defaults to dbpath --journal enable journaling --nojournal disable journaling (journaling is on by default for 64 bit) --journalOptions arg journal diagnostic options --journalCommitInterval arg how often to group/batch commit (ms) WiredTiger options: --wiredTigerCacheSizeGB arg maximum amount of memory to allocate for cache; defaults to 1/2 of physical RAM --wiredTigerJournalCompressor arg (=snappy) use a compressor for log records [none|snappy|zlib] --wiredTigerDirectoryForIndexes Put indexes and data in different directories --wiredTigerCollectionBlockCompressor arg (=snappy) block compression algorithm for collection data [none|snappy|zlib] --wiredTigerIndexPrefixCompression arg (=1) use prefix compression on row-store leaf pages
數據庫操作:
$ mongo # 進入shell MongoDB shell version v3.6.0 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.6.0 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 > show dbs; # 查看數據庫列表 admin 0.000GB config 0.000GB local 0.000GB >
use xxx_db_name db.xxx_table_name.find() # 默認返回10條

1. Import Example Dataset $ mongoimport --db test --collection restaurants --drop --file ~/software/mongodb/primer-dataset.json 2018-01-06T15:10:19.136+0800 connected to: localhost 2018-01-06T15:10:19.137+0800 dropping: test.restaurants 2018-01-06T15:10:20.559+0800 imported 25359 documents 2. MongoDB Shell (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 config 0.000GB local 0.000GB test 0.005GB 3. Insert Data with the mongo Shell > use test switched to db test > db.restaurants.insert( { "address" : { "street" : "2 Avenue", "zipcode" : "10075", "building" : "1480", "coord" : [ -73.9557413, 40.7720266 ] }, "borough" : "Manhattan", "cuisine" : "Italian", "grades" : [ { "date" : ISODate("2014-10-01T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-01-16T00:00:00Z"), "grade" : "B", "score" : 17 } ], "name" : "Vella", "restaurant_id" : "41704620" } ) WriteResult({ "nInserted" : 1 }) > If the document passed to the insert() method does not contain the _id field, the mongo shell automatically adds the field to the document and sets the field’s value to a generated ObjectId. 更多信息 insert() and Documents: https://docs.mongodb.com/manual/core/document/ https://docs.mongodb.com/manual/tutorial/insert-documents/ 4. Find or Query Data with the mongo Shell 4.1 Query for All Documents in a Collection 調用find()方法,不加參數 db.restaurants.find() The result set contains all documents in the restaurants collection. 4.2 Specify Equality Conditions The query condition for an equality match on a field has the following form: { <field1>: <value1>, <field2>: <value2>, ... } If the <field> is a top-level field and not a field in an embedded document or an array, you can either enclose the field name in quotes or omit the quotes. If the <field> is in an embedded document or an array, use dot notation to access the field. With dot notation, you must enclose the dotted name in quotes. 4.2.1 Query by a Top Level Field db.restaurants.find( { "borough": "Manhattan" } ) 4.2.2 Query by a Field in an Embedded Document db.restaurants.find( { "address.zipcode": "10075" } ) To specify a condition on a field within an embedded document, use the dot notation. Dot notation requires quotes around the whole dotted field name. 4.2.3 Query by a Field in an Array db.restaurants.find( { "grades.grade": "B" } ) The grades array contains embedded documents as its elements. To specify a condition on a field in these documents, use the dot notation. Dot notation requires quotes around the whole dotted field name. 4.3 Specify Conditions with Operators MongoDB provides operators to specify query conditions, such as comparison operators. Although there are some exceptions, such as the $or and $and conditional operators, query conditions using operators generally have the following form: { <field1>: { <operator1>: <value1> } } For a complete list of the operators, see query operators. 4.3.1 Greater Than Operator ($gt) Query for documents whose grades array contains an embedded document with a field score greater than 30. db.restaurants.find( { "grades.score": { $gt: 30 } } ) 4.3.2 Less Than Operator ($lt) Query for documents whose grades array contains an embedded document with a field score less than 10. db.restaurants.find( { "grades.score": { $lt: 10 } } ) 5. Combine Conditions You can combine multiple query conditions in logical conjunction (ANDs) and logical disjunctions (OR). 5.1 Logical AND 用逗號隔開;匹配滿足所有條件的documents You can specify a logical conjunction (AND) for a list of query conditions by separating the conditions with a comma in the conditions document. db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } ) The result set includes only the documents that matched all specified criteria. 5.2 Logical OR 用$or表示;匹配滿足任意一個條件的documents You can specify a logical disjunction (OR) for a list of query conditions by using the $or query operator. db.restaurants.find( { $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] } ) The result set includes only the documents that match either conditions. 6. Sort Query Results 升序1,降序-1 To specify an order for the result set, append the sort() method to the query. Pass to sort() method a document which contains the field(s) to sort by and the corresponding sort type, e.g. 1 for ascending and -1 for descending. For example, the following operation returns all documents in the restaurants collection, sorted first by the borough field in ascending order, and then, within each borough, by the "address.zipcode" field in ascending order: db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } ) The operation returns the results sorted in the specified order.
其它的數據增刪改查等數據庫操作有待更新補充。
4. 數據恢復
之前服務器宕機,通過U盤啟動的方法將數據庫文件復制到移動硬盤,重裝服務器之后,需要重新安裝mongodb(參照上面的第1步)和進行數據恢復。
數據恢復步驟:修改mongodb配置文件,將數據存儲路徑改為指向之前備份好的數據的存放路徑:
sudo vim /etc/mongod.conf ,將 dbPath: /var/lib/mongodb 改為 dbPath: /home/xxname/mongodb_data ;然后重新啟動mongodb服務 sudo service mongod start 即可。
其中遇到幾個問題:
安裝mongodb執行到 sudo apt-get install -y mongodb-org 時,報錯 E: Unable to locate package mongodb-org ,改為:
sudo apt-get install -y mongodb
這種方式安裝到的是比較舊的版本(v2.6.0)
但是進行數據恢復的時候報錯:
2018-07-25T11:40:08.153+0800 [initandlisten] exception in initAndListen: 28574 Cannot start server. Detected data files in /home/algsuper/mongodb created by storage engine 'wiredTiger'. The configured storage engine is 'mmapv1'., terminating
原因:根據mongodb的Release Notes,在v2.6及之前版本的默認存儲引擎是MMAPv1,在v3.0開始支持WiredTiger,而在v3.2之后將默認存儲引擎改為WiredTiger。所以,由v3.6的mongodb (對應WiredTiger存儲引擎)創建的備份數據,導不進v2.6的mongodb。
解決方法:從官網中安裝最新版v4.0.0, sudo apt-get install -y mongodb-org 可行。
但是安裝好之后,啟動服務報了另一個錯誤 MongoDB Failed to start mongod.service: Unit mongodb.service is masked ,需要配置服務:
sudo vim /etc/systemd/system/mongodb.service
添加以下內容並保存:
[Unit] Description=High-performance, schema-free document-oriented database After=network.target [Service] User=mongodb ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf [Install] WantedBy=multi-user.target
然后執行以下三個命令即可啟動服務:
sudo systemctl enable mongod.service sudo systemctl daemon-reload sudo service mongod start
在數據恢復中,如果修改配置文件中的dbpath導致服務重啟失敗,參考第2點的備注修改數據存儲目錄的權限即可。
進入mongo的shell環境,能夠查看到之前的數據庫,數據恢復完成~
參考:
官方的安裝和測試教程(很詳細):Install MongoDB Community Edition on Ubuntu
MongoDB Failed to start mongod.service: Unit mongodb.service is masked