MongoDB - Centos five-step installation and setting up service startup (1)

Table of contents

Introduction:

1. Download

2. Decompress

3. Start

4. Configuration

5. System service starts automatically


Introduction:

Official address: MongoDB: The Developer Data Platform | MongoDB

MongoDB is an open source NoSQL database management system that uses a document data model to store data. The following is the basic information about MongoDB.

  1. Document-based data storage: MongoDB uses BSON (Binary JSON) format to store data, which is a binary representation similar to JSON. Each document is a collection of key-value pairs containing fields and values, similar to rows in a relational database.

  2. Unstructured data: Compared with traditional relational databases, MongoDB is an unstructured database and does not need to define table structures and fields in advance. Each document can have a different structure, with the flexibility to add, delete, and modify fields as needed.

  3. Scalability: MongoDB has good scalability and can support large-scale data storage and high-throughput workloads. It supports horizontal scaling, distributing data across multiple servers through sharding to achieve better performance and capacity.

  4. Powerful query function: MongoDB provides rich query functions and supports complex query operations, including conditional query, range query, aggregation query, text search, etc. It also supports creating indexes to speed up queries.

  5. High availability and fault tolerance: MongoDB supports replica sets, which are a group of MongoDB instances with the same data set. Replication sets provide redundant backup of data and automatic failover capabilities for high availability and fault tolerance.

  6. Active community and rich ecosystem: MongoDB has a large developer community and rich ecosystem, providing a variety of tools, drivers, and libraries to meet the needs of different programming languages ​​and applications.

MongoDB is suitable for many application scenarios, especially for applications that need to process large amounts of unstructured data and require flexibility and scalability. It is widely used in web applications, big data analysis, real-time analysis, Internet of Things (IoT) and other fields.

1. Download

Official website download address: Download MongoDB Community Server | MongoDB

 Select the community version, which is free, and then select the corresponding system. Here, we take centos as an example, and select the compressed package to download.

If your choice is the same as mine, you can directly click the following link to download: https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-6.0.8.tgz

You can choose to use the wget command directly on the server to download

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-6.0.8.tgz

You can also download it locally and upload it to the server through sftp.

2. Decompress

# 解压
tar -zxvf mongodb-linux-x86_64-rhel70-6.0.8.tgz

# 重命名
mv mongodb-linux-x86_64-6.0.8 mongodb

The MongoDB service needs to specify the directory where the data is stored when starting. If not specified, the /data/db/ directory will be found by default. If the data directory does not exist or is not writable, the server will not be able to start.

# 创建数据存放目录和日志目录
cd mongodb
# data文件夹存放数据库目录
mkdir data
# logs文件夹存放操作日志信息
mkdir logs

3. Start

Execute the following command

bin/mongod --fork --dbpath=/data/mongodb/data --logpath=/data/mongodb/logs/mongo.log
  • fork: Run the mongod process in the background and run the MongoDB service as a daemon process. If fork is specified, logpath must also be specified.
  • dbpath: Specifies the storage path of the MongoDB database file, which must be specified when starting the service.
  • logpath: Specify the storage path of the MongoDB log file. By default, the log is printed on the command line. If you have write permission on this directory and the file does not exist, the file will be automatically created. If the log file already exists, the file will be overwritten by default and all old logs will be deleted. If you wish to retain old logs, you should use the logappend option in addition to logpath. 

The following information appears when executing the command, indicating that the startup is successful.

To be on the safe side, let’s monitor the port number 

lsof -i :27017

The following information indicates that MongoDB is running 

 

4. Configuration

The above startup command is only suitable for use in the development environment. The actual production environment requires more MongoDB configuration, such as port number, listening IP address, authentication mechanism, etc., so we can create a conf file to configure our MongoDB .

Create a mongdb.conf file in our mongdb directory

# 创建目录
cd /data/mongodb
vim mongo.conf

After the vim command, press the i key to enter the editing mode and paste the following configuration into it. Note: The configuration of the 6.x version is different from that of the old version.

# 存储相关配置
storage:
  dbPath: /data/mongodb/data
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1

# 网络相关配置
net:
  bindIp: 127.0.0.1
  port: 27017

# 日志相关配置
systemLog:
  destination: file
  path: /data/mongodb/logs/mongod.log
  logAppend: true

# 安全相关配置
security:
  authorization: disabled

# 复制集相关配置
replication:
  replSetName: node-1

# 其他配置项...
processManagement:
  fork: true

Start MongoDB using configuration mode

bin/mongod --config /data/mongodb/mongo.conf

5. System service starts automatically

After setting our MongoDB as a system service, we can simply start, stop and restart through `systemctl`

Write the mongodb.service file. If you have any questions about the directory `/etc/systemd/system` where the file is stored, there are instructions at the end of the article.

cd /etc/systemd/system

vim mongod

After vim command, press i key to enter editing mode and paste the following configuration into it

[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
ExecStart=/data/mongodb/bin/mongod --config /data/mongodb/mongo.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/data/mongodb/bin/mongod --shutdown --config /data/mongodb/mongo.conf
Restart=always
LimitNOFILE=64000
LimitNPROC=64000

[Install]
WantedBy=multi-user.target

Reload systemd service configuration

sudo systemctl daemon-reload

Test whether it can be started successfully

# 现将之前运行的MongoDB服务kill掉,先查询mongo运行的pid
ps -aux | grep mongo

kill <pid>

# 测试systemctl是否可以启动mongdb
sudo systemctl start mongodb
sudo systemctl start mongodb

 Finally set up auto-start at boot

sudo systemctl enable mongodb

Now, MongoDB has been set to system startup and can be managed using the following systemctl command:

  • Start the MongoDB service:sudo systemctl start mongodb
  • Stop the MongoDB service:sudo systemctl stop mongodb
  • Restart the MongoDB service:sudo systemctl restart mongodb
  • Check MongoDB service status:sudo systemctl status mongodb

Please ensure that /data/mongodb/the MongoDB files and configuration in the directory are correct and have appropriate permissions.

Solve doubts

In the Systemd environment, it is recommended to place the service unit file /etc/systemd/systemin the directory instead of /etc/init.dthe directory. /etc/init.dThe directory is where the traditional SysV Init script is stored, and Systemd has replaced SysV Init as the initialization system of the Linux system.

Systemd uses .servicefiles as service unit files and systemctlcommands for service management. Placing service unit files /etc/systemd/systemin a directory allows for better integration with Systemd and provides more service management capabilities.

If you place the service unit file /etc/init.din a directory, Systemd will not be able to recognize and manage the service directly. While it is possible to make Systemd support SysV Init scripts with some conversions or compatibility settings, this approach is not recommended as it may cause some problems and is not in line with Systemd's best practices.

Therefore, it is recommended to place MongoDB's service unit file /etc/systemd/systemin the directory to integrate with Systemd and use systemctlcommands for service management

Guess you like

Origin blog.csdn.net/weixin_43820024/article/details/131723528