在Ubuntu 16.04 上如何安装MongoDB
MongoDB是一个跨平台的,面向文档的数据库,可提供高性能,高可用性和易扩展性。MongoDB致力于收集和文档的概念。MongoDB维护者尚未发布正式的Ubuntu16.04MongoDB软件包。本文介绍了“如何在Ubuntu上安装MongoDB并在启动时启动MongoDB服务”
添加MongoDB存储库
MongoDB通常包含在Ubuntu软件包存储库中。但是,合法的MongoDB存储库以认可的方式提供了最新的版本更改。
要执行此过程,我们首先必须使用以下命令为合法的MongoDB存储库导入键:
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
样本输出应如下所示–
Executing: /tmp/tmp.Qe1RFZgUh2/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 gpg: requesting key EA312927 from hkp server keyserver.ubuntu.com gpg: key EA312927: public key "MongoDB 3.2 Release Signing Key <packaging@mongodb.com>" imported gpg: Total number processed: 1 gpg: imported: 1 (RSA: 1)
要添加MongoDB存储库,请使用以下命令-
$ sudo echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
现在,我们需要使用以下命令更新软件包列表–
$ sudo apt-get update
安装和验证MongoDB
要安装MongoDB,请使用以下命令–
$ sudo apt-get install -y --allow-unauthenticated mongodb-org
样本输出应如下所示–
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: apport-hooks-elementary contractor javascript-common libgda-5.0-4 libgda-5.0-common libgranite-common libgranite3 libgsignon-glib1 libindicate5 libjs-jquery libnoise-core0 libtagc0 Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools The following NEW packages will be installed: mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools 0 upgraded, 5 newly installed, 0 to remove and 168 not upgraded. Need to get 47.4 MB of archives. After this operation, 218 MB of additional disk space will be used. Get:1 http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2/multiverse amd64 mongodb-org-shell amd64 3.2.6 [5,257 kB] Get:2 http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2/multiverse amd64 mongodb-org-server amd64 3.2.6 [9,541 kB] Get:3 http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2/multiverse amd64 mongodb-org-mongos amd64 3.2.6 [4,337 kB] ...................................................................................
为了在Ubuntu16.04上正确启动MongoDB作为服务,我们还需要创建一个描述该服务的单元文件。单位文件告诉系统-一种管理资源的方法。最常见的单元类型是服务,它确定如何启动或停止服务。
创建一个名为mongodb.service的配置文件以设置单位文件,如下所示–
$ sudo nano /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
现在,使用systemctl启动新创建的服务,如下所示–
$ sudo systemctl start mongodb
它通常不返回任何输出。现在,我们需要检查服务是否已正确启动。为了执行此过程,请使用以下命令–
$ sudo systemctl status mongodb
样本输出应如下所示–
mongodb.service - High-performance, schema-free document-oriented database Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset: Active: active (running) since Fri 2016-05-13 10:33:52 IST; 1min 48s ago Main PID: 7078 (mongod) Tasks: 16 (limit: 512) CGroup: /system.slice/mongodb.service └─7078 /usr/bin/mongod --quiet --config /etc/mongod.conf May 13 10:33:52 linux systemd[1]: Started High-performance, schema-free document lines 1-9/9 (END)
要启用自动启动MongoDB的过程,请在系统启动时使用以下命令-
$ sudo systemctl enable mongodb
样本输出应如下所示–
Created symlink from /etc/systemd/system/multi-user.target.wants/mongodb.service to /etc/systemd/system/mongodb.service.
在阅读完本文之后,您将能够理解–如何在Ubuntu16.04上安装MongoDB。在我们的下一篇文章中,我们将提出更多基于Linux的技巧。继续阅读!