MongoDB 安装、运行、使用、数据恢复

 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:
mongod.conf 

 

【备注】

在配置文件中修改数据存储路径后(例如修改为/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
View Code

 

 数据库操作:

$ 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环境,能够查看到之前的数据库,数据恢复完成~

 

参考:

mongodb 安装使用备记

官方的安装和测试教程(很详细):Install MongoDB Community Edition on Ubuntu

MongoDB Failed to start mongod.service: Unit mongodb.service is masked

posted @ 2017-12-18 10:41  焦距  阅读(2483)  评论(0编辑  收藏  举报