如何在CentOS 8上安装MongoDB
如何在CentOS 8上安装MongoDB
MongoDB是一个免费的,开放源代码和面向文档的数据库引擎,可提供对非关系数据库的访问。MongoDB将数据与动态模式一起保存在JASON之类的文档中,因此与其他数据库相比,它提供了更好的性能。MongoDB是一个NoSQL数据库,表示它不支持SQL来操纵存储的数据。MongoDB提供了丰富的功能集,包括即席查询,索引编制,复制,负载平衡,文件存储,聚合,事务等等。MongoDB是所有系统中最受欢迎的数据库引擎之一,并已用于许多大规模生产部署中。
在本教程中,我们将向您展示如何在CentOS 8服务器上安装和使用MongoDB。
先决条件
- 运行CentOS的服务器8。
- 在您的服务器上配置了root密码。
安装MongoDB
默认情况下,CentOS 8默认存储库中不提供MongoDB。因此,您需要将MongoDB存储库添加到系统中。您可以通过在目录/etc/yum.repos.d/中创建文件mongodb.repo来添加它:
nano /etc/yum.repos.d/mongodb.repo
添加以下行:
[mongodb-org-4.2] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/development/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
保存并关闭文件。然后,使用以下命令安装MongoDB:
dnf install mongodb-org
安装MongoDB之后,启动MongoDB服务,并使其在系统重新引导后启动:
systemctl start mongod
systemctl enable mongod
现在,您可以使用以下命令检查MongoDB的状态:
systemctl status mongod
您应该获得以下输出:
systemctl status mongod
输出
? mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2019-10-28 03:59:12 EDT; 5min ago Docs: https://docs.mongodb.org/manual Process: 737 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS) Process: 735 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 732 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS) Process: 726 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS) Main PID: 914 (mongod) Memory: 216.1M CGroup: /system.slice/mongod.service ??914 /usr/bin/mongod --auth -f /etc/mongod.conf Oct 28 03:58:14 centos8 systemd[1]: Starting MongoDB Database Server... Oct 28 03:58:28 centos8 mongod[737]: about to fork child process, waiting until server is ready for connections. Oct 28 03:58:28 centos8 mongod[737]: forked process: 914 Oct 28 03:59:12 centos8 mongod[737]: child process started successfully, parent exiting Oct 28 03:59:12 centos8 systemd[1]: Started MongoDB Database Server.
接下来,您可以使用以下命令访问MongoDB Shell:
mongo
您应该获得以下输出:
MongoDB shell version v4.2.1-rc0-5-g87a606d connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("e8e052c8-7c47-4908-9a29-d7825bac037a") } MongoDB server version: 4.2.1-rc0-5-g87a606d Server has startup warnings: 2019-10-28T04:07:55.106-0400 I CONTROL [initandlisten] 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2019-10-28T04:07:55.107-0400 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() ---
创建一个MongoDB管理员用户
接下来,为具有管理特权的MongoDB创建一个Admin用户。首先,使用以下命令打开MongoDB shell:
mongo
接下来,将数据库切换为admin,如下所示:
>use admin
接下来,使用以下命令创建一个新的MongoDB用户:
> db.createUser(
{
user: "mongodadmin",
pwd: "password123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
您应该获得以下输出:
Successfully added user: { "user" : "mongodadmin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
接下来,使用以下命令列出您的管理员用户:
>show users
您应该看到以下输出:
{ "_id" : "admin.mongodadmin", "userId" : UUID("f6e908db-e393-44a9-8c77-0fdb1c2baa0e"), "user" : "mongodadmin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }
配置MongoDB身份验证
默认情况下,MongoDB允许所有用户访问MongoDB Shell并运行任何命令。因此,建议配置MongoDB身份验证,以防止其他用户在没有足够特权的情况下运行任何命令。
首先,您需要通过编辑文件/lib/systemd/system/mongod.service来启用MongoDB身份验证。
nano /lib/systemd/system/mongod.service
更改以下行:
Environment="OPTIONS= --auth -f /etc/mongod.conf"
保存并关闭文件。然后,重新加载systemd守护程序,并使用以下命令重新启动MongoDB服务:
systemctl --system daemon-reload
systemctl restart mongod
现在,登录到MongoDB shell并尝试查看没有自动身份验证的用户:
mongo
> use admin
> show users
您应该看到以下错误:
2019-10-28T04:13:15.346-0400 E QUERY [js] uncaught exception: Error: command usersInfo requires authentication : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.getUsers@src/mongo/shell/db.js:1638:15 shellHelper.show@src/mongo/shell/utils.js:883:9 shellHelper@src/mongo/shell/utils.js:790:15 @(shellhelp2):1:1
现在,运行以下命令来验证MongoDB:
> db.auth('mongodadmin', 'password123')
> show users
您应该在以下输出中看到admin用户及其角色:
{ "_id" : "admin.mongodadmin", "userId" : UUID("f6e908db-e393-44a9-8c77-0fdb1c2baa0e"), "user" : "mongodadmin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }
结论
在以上教程中,我们学习了如何安装MongoDB和配置MongoDB用户身份验证。我希望您现在拥有足够的知识来创建自己的数据库并开始使用MongoDB构建应用程序。如有任何问题,随时问我。
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/32182.html