1. Environment and tool description
System: CentOS 7.6
Install software: MongoDB 4.2.10
Collaboration tools: FileZilla , PuTTY , 360 Compression
2. Download MongoDB and unzip the software package
- Official website address (Note: If the official website does not have exactly the same version number software package, you can download the tar package with a similar version according to the file name of the picture below)
- Baidu network disk , extract the code After
t8r0
the download is successful, use 360 to compress and decompress the software package
3. Upload the decompressed file to the corresponding directory of the server
Use FileZilla to connect to the remote server, create a directory under /usr/local
the path mongodb
, and then drag and upload the decompressed software package to mongodb
the directory
4. Configure conf and related directories
/usr/local/mongodb
1. Create a new data
directory and subdirectories under the path 2.db
Create a new directory and sub-blank files under the path 3. Create a new configuration file under the path/usr/local/mongodb
logs
mongodb.log
/usr/local/mongodb
mongodb.conf
# 端口号
port=27017
# db 目录
dbpath=/usr/local/mongodb/data/db
# 日志目录
logpath=/usr/local/mongodb/logs/mongodb.log
# 后台
fork=true
# 日志输出
logappend=true
# 允许远程 IP 连接
bind_ip=0.0.0.0
5. Start and test
Remote terminal control using a PuTTY connection
1. Start
cd /usr/local/mongodb
./bin/mongod --config mongodb.conf
Note: If -bash: ./mongod: Permission denied
an error , change the permissions of the bin file
# 在 /usr/local/mongodb 路径下
chmod -R 777 bin
2. Connection
# 在 /usr/local/mongodb 路径下
./bin/mongo
3. Test
4. Close
# 在 /usr/local/mongodb 路径下
./bin/mongod --shutdown --config mongodb.conf
Sixth, configure the MongoDB service to start
1. Write mongod.service to start the service file (PS: some articles write about mongodb.service, but the official website is mongod.service )
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2. Upload the mongod.service file to /usr/lib/systemd/system
the path and configure permissions
. The above is to operate through FileZilla, if you use the command
cd /usr/lib/systemd/system
chmod 754 mongod.service
3. Set the Mongodb system to start automatically
systemctl enable mongod
4. Other Mongodb systemctl commands (it is recommended to use the following commands instead of executing the services in the bin directory mentioned above)
# 启动 MongoDB
systemctl start mongod
# 停止 MongoDB
systemctl stop mongod
# 重启 MongoDB
systemctl restart mongod
# 查看 MongoDB 状态
systemctl status mongod