To build a MongoDB cluster on the CentOS system, you need to complete MongoDB installation, instance configuration, replica set settings, and sharding steps. The following steps will guide you through this process in detail:
1. Preparation
Make sure the CentOS system is updated and install the necessary tools:
sudo yum update -y sudo yum install -y wget vim
2. Install MongoDB
- Add MongoDB YUM source: Create a
mongodb.repo
file and add MongoDB repository information (please adjust the version number according to the actual situation):
echo "[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" | sudo tee /etc/yum.repos.d/mongodb-org-4.4.repo
- Install MongoDB: Use the YUM command to install MongoDB and its dependent components:
sudo yum install -y mongodb-org
3. Configure MongoDB instance
In this example, we will build a cluster that contains a Config Server and multiple Shards.
- Config Server: Modify the
/etc/mongod.conf
file (please replaceconfig_server_ip
with the actual IP address):
sudo vim /etc/mongod.conf
Add the following configuration:
<code>storage: dbPath: /var/lib/mongo/configdb systemLog: destination: file path: /var/log/mongodb/configdb.log logAppend: true net: bindIp: 0.0.0.0 port: 27019 sharding: clusterRole: configsvr</code>
Create a database directory and start the Config Server service:
sudo mkdir -p /var/lib/mongo/configdb sudo chown mongod:mongod /var/lib/mongo/configdb sudo systemctl start mongod sudo systemctl enable mongod
- Configure Shard: Configure a separate MongoDB instance for each Shard server. Modify the
/etc/mongod.conf
file (please replaceshard_1_ip
as the actual IP address, and create the corresponding directory and configuration file for each shard, such as/var/lib/mongo/shard_2
,/var/lib/mongo/shard_3
, etc.):
sudo vim /etc/mongod.conf
Add the following configuration (example is shard_1):
<code>storage: dbPath: /var/lib/mongo/shard_1 systemLog: destination: file path: /var/log/mongodb/shard_1.log logAppend: true net: bindIp: 0.0.0.0 port: 27018 sharding: clusterRole: shardsvr</code>
Create a database directory and start the Shard service:
sudo mkdir -p /var/lib/mongo/shard_1 sudo chown mongod:mongod /var/lib/mongo/shard_1 sudo systemctl start mongod sudo systemctl enable mongod
Repeat the above steps to configure multiple shards (such as shard_2, shard_3, etc.).
4. Configure Mongos router
Install Mongos: (If not installed, please use
sudo yum install -y mongodb-org-mongos
to install)Configure
/etc/mongos.conf
: (Please replaceconfig_server_ip
as the IP address of Config Server,mongos_ip
as the IP address of Mongos server):
sudo vim /etc/mongos.conf
Add the following configuration:
<code>sharding: configDB: config_server_ip:27019 net: bindIp: 0.0.0.0 port: 27017</code>
- Start Mongos service:
sudo mongos --config /etc/mongos.conf
5. Add Shard to the cluster
Connect to Mongos server:
mongo --host mongos_ip:27017
Then execute the following command to add Shard (please replace it with the actual shard IP address and port):
sh.addShard("shard_1_ip:27018") sh.addShard("shard_2_ip:27018") sh.addShard("shard_3_ip:27018")
6. Monitoring and management
Use sh.status()
command to view the cluster status:
sh.status()
Through the above steps, you can build a MongoDB cluster on the CentOS system. Remember to replace the IP address and port number in the example for your actual server information. In addition, to ensure high availability, it is recommended that you further configure the replica set.
The above is the detailed content of How to build MongoDB cluster on CentOS. For more information, please follow other related articles on the PHP Chinese website!

CentOS is a stable, enterprise-grade Linux distribution suitable for server and enterprise environments. 1) It is based on RedHatEnterpriseLinux and provides a free, open source and compatible operating system. 2) CentOS uses the Yum package management system to simplify software installation and updates. 3) Support advanced automation management, such as using Ansible. 4) Common errors include package dependency and service startup issues, which can be solved through log files. 5) Performance optimization suggestions include the use of lightweight software, regular cleaning of the system and optimization of kernel parameters.

Alternatives to CentOS include RockyLinux, AlmaLinux, OracleLinux, and SLES. 1) RockyLinux and AlmaLinux provide RHEL-compatible binary packages and long-term support. 2) OracleLinux provides enterprise-level support and Ksplice technology. 3) SLES provides long-term support and stability, but commercial licensing may increase costs.

Alternatives to CentOS include UbuntuServer, Debian, Fedora, RockyLinux, and AlmaLinux. 1) UbuntuServer is suitable for basic operations, such as updating software packages and configuring the network. 2) Debian is suitable for advanced usage, such as using LXC to manage containers. 3) RockyLinux can optimize performance by adjusting kernel parameters.

The CentOS shutdown command is shutdown, and the syntax is shutdown [Options] Time [Information]. Options include: -h Stop the system immediately; -P Turn off the power after shutdown; -r restart; -t Waiting time. Times can be specified as immediate (now), minutes ( minutes), or a specific time (hh:mm). Added information can be displayed in system messages.

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

Steps to configure IP address in CentOS: View the current network configuration: ip addr Edit the network configuration file: sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0 Change IP address: Edit IPADDR= Line changes the subnet mask and gateway (optional): Edit NETMASK= and GATEWAY= Lines Restart the network service: sudo systemctl restart network verification IP address: ip addr

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

The command to restart the SSH service is: systemctl restart sshd. Detailed steps: 1. Access the terminal and connect to the server; 2. Enter the command: systemctl restart sshd; 3. Verify the service status: systemctl status sshd.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)