如何在 CentOS 7 上安装和配置 MongoDB
在本文中,我们将学习如何在 CentOS 7 上安装和配置 MongoDB。MongoDB 是一个开源免费的数据库(非关系型数据库),这意味着它是一个面向文档的数据库,它存储的文档在结构上类似于 JSON(在 MongoDB 中称为 BSON),具有高可用性、高性能和自动扩展功能。与 RDBMS 不同,它不需要任何预定义的数据库模式来将数据添加到数据库表中。我们可以在任何时间点更改模式,而不会影响现有模式。
前提条件
- 在 Linux 机器上安装 CentOS 7。
- 具有 root 用户权限的用户。
添加 MongoDB 仓库
默认情况下,MongoDB 仓库不在 CentOS 7 仓库中,我们需要将 MongoDB 仓库添加到本地机器。
# vi /etc/yum.repos.d/mongodb-org.repo Output: [mongodb-org-3.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/ gpgcheck=1 enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
将 MongoDB 仓库添加到本地机器后,我们将更新系统,以便 yum 可以检查仓库信息。
# yum update Output: Loaded plugins: fastestmirror base | 3.6 kB 00:00 extras | 3.4 kB 00:00 mongodb-org-3.2 | 2.5 kB 00:00 updates | 3.4 kB 00:00 mongodb-org-3.2/7/primary_db | 54 kB 00:01 Determining fastest mirrors * base: mirror.dhakacom.com * extras: mirror.dhakacom.com * updates: mirror.dhakacom.com No packages marked for update
安装 MongoDB
由于本地机器上的仓库详细信息已更新,我们现在将使用 yum 命令安装 MongoDB。
以下是安装 MongoDB 的命令。
# yum install mongodb-org Output: Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.dhakacom.com * extras: mirror.dhakacom.com * updates: mirror.dhakacom.com Resolving Dependencies --> Running transaction check ---> Package mongodb-org.x86_64 0:3.2.11-1.el7 will be installed … … … ---> Package mongodb-org-mongos.x86_64 0:3.2.11-1.el7 will be installed ---> Package mongodb-org-server.x86_64 0:3.2.11-1.el7 will be installed ---> Package mongodb-org-shell.x86_64 0:3.2.11-1.el7 will be installed ---> Package mongodb-org-tools.x86_64 0:3.2.11-1.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: mongodb-org x86_64 3.2.11-1.el7 mongodb-org-3.2 5.8 k Installing for dependencies: mongodb-org-mongos x86_64 3.2.11-1.el7 mongodb-org-3.2 5.6 M mongodb-org-server x86_64 3.2.11-1.el7 mongodb-org-3.2 12 M mongodb-org-shell x86_64 3.2.11-1.el7 mongodb-org-3.2 6.7 M mongodb-org-tools x86_64 3.2.11-1.el7 mongodb-org-3.2 41 M Transaction Summary ================================================================================ Install 1 Package (+4 Dependent packages) Total download size: 66 M Installed size: 201 M Is this ok [y/d/N]:y Downloading packages: warning: /var/cache/yum/x86_64/7/mongodb-org-3.2/packages/mongodb-org-3.2.11-1.el7.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID ea312927: NOKEY Public key for mongodb-org-3.2.11-1.el7.x86_64.rpm is not installed (1/5): mongodb-org-3.2.11-1.el7.x86_64.rpm | 5.8 kB 00:01 (2/5): mongodb-org-mongos-3.2.11-1.el7.x86_64.rpm | 5.6 MB 00:01 (3/5): mongodb-org-shell-3.2.11-1.el7.x86_64.rpm | 6.7 MB 00:00 (4/5): mongodb-org-server-3.2.11-1.el7.x86_64.rpm | 12 MB 00:06 (5/5): mongodb-org-tools-3.2.11-1.el7.x86_64.rpm | 41 MB 00:06 -------------------------------------------------------------------------------- Total 7.3 MB/s | 66 MB 00:08 Retrieving key from https://www.mongodb.org/static/pgp/server-3.2.asc Importing GPG key 0xEA312927: Userid : "MongoDB 3.2 Release Signing Key <[email protected]>" Fingerprint: 42f3 e95a 2c4f 0827 9c49 60ad d68f a50f ea31 2927 From : https://www.mongodb.org/static/pgp/server-3.2.asc Is this ok [y/N]:y Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mongodb-org-server-3.2.11-1.el7.x86_64 1/5 Installing : mongodb-org-mongos-3.2.11-1.el7.x86_64 2/5 Installing : mongodb-org-tools-3.2.11-1.el7.x86_64 3/5 Installing : mongodb-org-shell-3.2.11-1.el7.x86_64 4/5 Installing : mongodb-org-3.2.11-1.el7.x86_64 5/5 Verifying : mongodb-org-shell-3.2.11-1.el7.x86_64 1/5 Verifying : mongodb-org-tools-3.2.11-1.el7.x86_64 2/5 Verifying : mongodb-org-mongos-3.2.11-1.el7.x86_64 3/5 Verifying : mongodb-org-server-3.2.11-1.el7.x86_64 4/5 Verifying : mongodb-org-3.2.11-1.el7.x86_64 5/5 Installed: mongodb-org.x86_64 0:3.2.11-1.el7 Dependency Installed: mongodb-org-mongos.x86_64 0:3.2.11-1.el7 mongodb-org-server.x86_64 0:3.2.11-1.el7 mongodb-org-shell.x86_64 0:3.2.11-1.el7 mongodb-org-tools.x86_64 0:3.2.11-1.el7 Complete!
安装 MongoDB 后,我们将启动 MongoDB 服务。
以下是启动 MongoDB 服务的命令:
# systemctl start mongod
我们运行以下命令来检查 MongoDB 服务的状态。
# systemctl status mongod Output: mongod.service - SYSV: Mongo is a scalable, document-oriented database. Loaded: loaded (/etc/rc.d/init.d/mongod) Active: active (running) since Fri 2016-11-25 14:09:25 IST; 12s ago Docs: man:systemd-sysv-generator(8) Process: 9901 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=0/SUCCESS) CGroup: /system.slice/mongod.service └─9912 /usr/bin/mongod -f /etc/mongod.conf Nov 25 14:09:25 localhost.localdomain systemd[1]: Starting SYSV: Mongo is a s... Nov 25 14:09:25 localhost.localdomain runuser[9908]: pam_unix(runuser:session... Nov 25 14:09:25 localhost.localdomain runuser[9908]: pam_unix(runuser:session... Nov 25 14:09:25 localhost.localdomain mongod[9901]: Starting mongod: [ OK ] Nov 25 14:09:25 localhost.localdomain systemd[1]: Started SYSV: Mongo is a sc...Hint: Some lines were ellipsized, use -l to show in full.
重新加载 MongoDB 配置
# systemctl reload mongod
停止 MongoDB 服务
# systemctl stop mongod
配置 MongoDB 进程数量
默认情况下,当我们运行 Mongo 时,进程数量太低(4096),因此 MongoDB 将显示以下错误:
# mongo Output: MongoDB shell version: 3.2.11 connecting to: test 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: 2016-11-25T14:09:25.553+0530 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files. 2016-11-25T14:09:25.553+0530 I CONTROL [initandlisten]
要修复进程数量,我们需要编辑文件“20-nproc.conf”,该文件位于“/etc/security/limits.d/”
# vi /etc/security/limits.d/20-nproc.conf Output: # Default limit for number of user's processes to prevent # accidental fork bombs. # See rhbz #432903 for reasoning. * soft nproc 4096 root soft nproc unlimited
我们需要将 4096 更改为 32000
* soft nproc 32000
将值更改为 32000 后,我们需要重新启动 MongoDB 服务,以下是重新启动 MongoDB 服务的命令。
# systemctl restart mongod
创建 MongoDB 管理员用户
在这里,我们将创建一个用户名为 admin,密码为“password123”的用户,创建用户后,我们将检查数据库中的用户列表。
# mongo Output: MongoDB shell version: 3.2.11 connecting to: test > use admin switched to db admin > db.createUser( ... { ... user: "admin", ... pwd: "password123", ... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] ... } ... ) Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } > show users; { "_id" : "admin.admin", "user" : "admin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } >
在本文中,我们学习了:如何将 MongoDB 仓库添加到本地机器并安装 MongoDB,我们还学习了如何在 MongoDB 配置中将进程数量从 4096 更改或增加到 32000,以及如何创建一个管理员用户。