Ubuntu 16.04中安装MongoDB3.4数据库系统

Ubuntu 16.04中安装MongoDB3.4数据库系统

关于MongoDB数据库系统

MongoDB是一个开源的数据库系统,属于NoSQL非关系型数据库,MongoDB的文件是按以下格式字段和值对组成的数据结构:

{
  field: value
  field: value
  field: value
  ...
}

例如:

{
  name: "Giuseppe"
  site: "Unixmen"
  groups: ["unixmen", "sysadmins", "linux"]
}

该实例显示文档的格式类似于JSON。数据字段可从文档变化来记录和数据结构可以随时间而改变。

MongoDB是一个分布式数据库,免费开源软件,在GNU Affero GPL授权条款(或GNU AGPL)发布。

先决条件

在本教程中,我们使用了运行Ubuntu 16.04与3GB内存的服务器。

获取MongoDB的软件包

MongoDB是可在Ubuntu软件仓库中安装。但是默认打包的版本是稳定版本而不是最新的,我们可以通过执行以下命令来查看:

gmolica@ubuntu-server:~$ sudo apt-cache policy mongodb
mongodb:
  Installed: (none)
  Candidate: 1:2.6.10-0ubuntu1
  Version table:
    1:2.6.10-0ubuntu1 500
        500 http://it.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages

我们将使用MongoDB的官方资料库检索更新的版本。

导入MongoDB的公钥

Ubuntu的软件包管理工具,要求分销商签订GPG确保一致性和真实性您可以通过执行以下命令导入MongoDB的GPG公钥

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

创建MongoDB的列表文件
创建MongoDB的列表文件。这种手术是通过恰当要求。执行:

$ echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

通过执行以下代码刷新包数据库:

$ sudo apt-get update

MongoDB的安装
我们可以用以下命令来安装MongoDB的包:

$ sudo apt-get install mongodb-org -y

通过apt将安装MongoDB的最新版本并创建一个新的用户通过在命令输出结果显示

Setting up mongodb-org-shell (3.4.4) ...
Setting up mongodb-org-server (3.4.4) ...
Adding system user `mongodb' (UID 111) ...
Adding new user `mongodb' (UID 111) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 117) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb

因此,要创造systemd一个新的单元文件,在/lib/systemd/system下,所以我们可以用systemctl工具启动MongoDB。

启动MongoDB

正如前面提到的,我们可以用systemctl启动MongoDB的。首先,执行以下命令:

$ sudo systemctl start mongod

检查MongoDB的状态:

$ sudo systemctl status mongod

mongod.service - High-performance, schema-free document-oriented database
  Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset:
  Active: active (running) since ...

接下来,在引导时启用自动启动:

$ sudo systemctl enable mongod

至此MongoDB安装完成。

启动MongoDB
MongoDB的服务正在运行刚刚启动的MongoDB shell中执行以下命令
$ mongo

在shell中,可用的命令列表执行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

总结
到这里有关在Ubuntu 16.04服务器上安装MongoDB社区版教程就结束了我们将以后的教程中将推出更多关于如何在生产环境中使用MongoDB的更多细节。

更多MongoDB相关教程见以下内容

MongoDB 的详细介绍请点这里
MongoDB 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2017-07/145526.htm
今日推荐