In this two-day summit along with a full stack of video learning NodeJs + Express + when writing MongoDB related items, found in the Windows install MongoDB very smooth, and set to boot from the start after installing MongoDB, and runs as a service. Because they have bought Tencent cloud host, the system is CentOS7.5, had wanted to run through mongodb.conf profile MongoDB as follows:
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
But always there is a problem
A. Official website to download MongoDB , unzip to a specific directory
I downloaded MongoDB version 3.4.18: mongodb-linux-x86_64-3.4.18.tgz
Once downloaded, I mongodb-linux-x86_64-3.4.18.tgz unzip the file and install into / usr / local directory
CentOS7.5 the machine mongodb directory: / usr / local / mongodb
file structure in the directory MongoDB after installation as shown below:
II. Mongodb write the configuration file, specify the main database files, log files, as well as some startup parameters.
(1) New mongodb data in folders and subfolders, db; then create logs folder, New mongodb.log logs in the log file, where db folder used to store the database file, a log for storing logs file
cd /usr/local/mongodb
mkdir -p data/db
mkdir logs
touch logs/mongodb.log
. (2), a new configuration file mongodb.conf usr / localmongodb / bin / directory, reads as follows:
#端口
port=27017
#数据库存文件存放目录
dbpath= /usr/local/mongodb/data/db
#日志文件存放路径
logpath= /usr/local/mongodb/logs/mongodb.log
#使用追加的方式写日志
logappend=true
# 设置为true,修改数据目录存储模式,每个数据库的文件存储在DBPATH指定目录的不同的文件夹中。
# 使用此选项,可以配置的MongoDB将数据存储在不同的磁盘设备上,以提高写入吞吐量或磁盘容量。默认为false。
# 建议一开始就配置此选项
directoryperdb=true
# 后台运行
#以守护程序的方式启用,即在后台运行
fork=true
#最大同时连接数
maxConns=100
#不启用验证
noauth=true
#每次写入会记录一条操作日志(通过journal可以重新构造出写入的数据)。
journal=true
#即使宕机,启动时wiredtiger会先将数据恢复到最近一次的checkpoint点,然后重放后续的journal日志来恢复。
#存储引擎有mmapv1、wiretiger、mongorocks
storageEngine=wiredTiger
#这样就可外部访问了,例如从win10中去连虚拟机中的MongoDB
bind_ip = 0.0.0.0
#bind_ip = 127.0.0.1
Related Field Description
dbpath: database file path
logpath: Log File Path
logappend: whether the additional log
port: Port
fork: whether a background process started
auth: whether to start certification authority
nohttpinterface: whether to support the HTTP form of access
III. Writing Custom Service
(1). In the Create / etc / systemd / system / mongodb.service file directory, i.e. the definition of the service start MongoDB, off mode, as follows
[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
ExecStart=/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/db --logpath=/usr/local/mongodb/logs/mongodb.log --logappend --fork
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --dbpath=/usr/local/mongodb/data/db --logpath=/usr/local/mongodb/logs/mongodb.log --logappend --fork
ExecReload=/bin/kill -s HUP $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
(2) Set permissions
cd /etc/systemd/system
chmod 754 mongodb.service
(3) Start the close the service, set the startup
`` shell
# start the service
systemctl Start mongodb.service
# close the service
systemctl STOP mongodb.service
# boot
systemctl enable mongodb.service
#### 将mongodb的一些可执行文件目录bin添加到环境变量中
修改/etc/profile文件,在其末尾加上如下语句,即将mongodb的bin目录加到PATH环境变量中,是其mongod和mongo命令可以被终端shell识别,命令如下所示:
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
然后在终端中使用source /etc/profile使得配置生效。
### 查看mongodb服务的运行状态
注意:conf和service文件中设置路径,注意需要设置为绝对路径。不过奇怪的是我使用指定mongodb.conf配置的文件方式总是出错,后来直接用--dbpath和--logpath分别指定数据文件和日志文件反而能够正常启动。
使用systemctl enable mongodb.service将mongodb设置成开机自启动,使用systemctl start mongodb.service启动Mongodb,使用systemctl stop mongodb.service关闭mongodb服务。
另外可以通过systemctl status mongodb查看mongodb查看其服务的状态,如下图所示:
![查询mongodb服务的运行状态](https://img-blog.csdnimg.cn/20200216183830422.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NjZjE5ODgxMDMw,size_16,color_FFFFFF,t_70)
### 参考资料
* [Windows 平台安装 MongoDB-菜鸟教程](https://www.runoob.com/mongodb/mongodb-window-install.html)
* [Linux平台安装MongoDB-菜鸟教程](https://www.runoob.com/mongodb/mongodb-linux-install.html)
* [Centos7 设置Mongodb开机启动-自定义服务](https://blog.csdn.net/shujuelin/article/details/80909432)
* [Mongodb - Centos7下yum安装mongodb服务](https://www.jianshu.com/p/65c220653afd)