Comprehensive Guide to Deploying MongoDB on CentOS

Introduction

MongoDB, a NoSQL database, offers flexibility and scalability for handling large amounts of data. In this detailed blog post, we will walk through the step-by-step process of deploying MongoDB on CentOS, ensuring a smooth and successful installation.

Prerequisites:

Before we begin, make sure you have a CentOS server with root or sudo access. Also, ensure that your server meets MongoDB’s system requirements.

Step 1: Update System Packages

sudo yum update -y

Step 2: Configure the MongoDB Repository

Create a MongoDB repository file in the ‘yum’ repository directory.

sudo vi /etc/yum.repos.d/mongodb-org.repo

Add the following content:

[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

Save and exit.

Step 3: Install MongoDB

Install MongoDB using the following command:

sudo yum install -y mongodb-org

Step 4: Start MongoDB

Start the MongoDB service and enable it to start on boot:

sudo systemctl start mongod
sudo systemctl enable mongod

Step 5: Adjust Firewall Settings

If you are using firewalld, allow MongoDB traffic:

sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent
sudo firewall-cmd --reload

Step 6: Access MongoDB Shell

Access the MongoDB shell to interact with the database:

mongo

Step 7: Create Admin User (Optional but Recommended)

use admin
db.createUser({
  user: "admin",
  pwd: "your_secure_password",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})

Step 8: Secure MongoDB Installation

Edit the MongoDB configuration file to enable authentication:

sudo vi /etc/mongod.conf

Find the security section and uncomment and modify the following lines:

security:
  authorization: enabled

Save and exit, then restart MongoDB:

sudo systemctl restart mongod

Step 9: Connect to MongoDB with Authentication

Connect to the MongoDB shell with authentication:

mongo -u admin -p your_secure_password --authenticationDatabase admin

Step 10: Conclusion

Congratulations! You have successfully deployed MongoDB on CentOS. Remember to follow best practices for securing your database and regularly updating MongoDB for the latest features and security patches.

This comprehensive guide should help you seamlessly set up MongoDB on your CentOS server. If you encounter any issues, refer to the official MongoDB documentation for additional troubleshooting. Happy coding!

  • 18
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值