在本文中,我们将学习如何在CentOS 7上安装和配置MongoDB,MongoDB是一个开放源代码和免费数据库(是一个NoSQL数据库),意味着它是一个面向文档的数据库,它存储的文档是在结构上类似于JSON(在MongoDB中称为BSON),具有高可用性,性能和自动伸缩性。与RDBMS不同,它不需要任何预定义的数据库架构即可将数据添加到数据库表中。我们可以在任何时间点更改架构,而不会干扰现有架构。
在Linux机器上安装的Centos 7。
具有root用户特权的用户。
默认情况下,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
随着存储库详细信息在本地计算机上更新,我们现在将使用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.ascImporting GPG key 0xEA312927: Userid : "MongoDB 3.2 Release Signing Key <packaging@mongodb.com>" Fingerprint: 42f3 e95a 2c4f 0827 9c49 60ad d68f a50f ea31 2927 From : https://www.mongodb.org/static/pgp/server-3.2.ascIs 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!
一旦安装了MongoDD,我们现在将启动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
默认情况下,当我们运行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, seehttp://docs.mongodb.org/Questions? Try the support grouphttp://groups.google.com/group/mongodb-userServer 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
在这里,我们将创建一个用户名为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的过程以及如何创建管理员用户。