怎么样在系统启动时启动mongodb
时间: 2023-05-30 10:02:15 浏览: 188
如果您使用的是Linux操作系统,可以按照以下步骤在系统启动时启动MongoDB:
1. 打开终端并输入以下命令来编辑MongoDB的启动脚本:
```
sudo nano /etc/init.d/mongod
```
2. 将以下内容复制并粘贴到文件中:
```
#!/bin/sh
# chkconfig: 345 99 25
# description: MongoDB init script
# processname: mongod
# Source function library
. /etc/init.d/functions
# Environment variables
MONGOD=/usr/bin/mongod
CONF_FILE=/etc/mongod.conf
PID_FILE=/var/run/mongodb/mongod.pid
MONGOD_USER=mongodb
MONGOD_GROUP=mongodb
# Check if mongo is installed
if [ ! -x "$MONGOD" ] ; then
echo "MongoDB is not installed. Please install it and try again."
exit 1
fi
# Check if the configuration file is present
if [ ! -f "$CONF_FILE" ] ; then
echo "MongoDB configuration file not found at $CONF_FILE."
exit 1
fi
# Check if the PID file directory exists
if [ ! -d "`dirname $PID_FILE`" ] ; then
mkdir -p `dirname $PID_FILE`
chown $MONGOD_USER:$MONGOD_GROUP `dirname $PID_FILE`
fi
# Start MongoDB
start() {
echo -n "Starting MongoDB... "
daemon --user $MONGOD_USER --check $MONGOD -- $MONGOD --config $CONF_FILE --pidfilepath $PID_FILE >/dev/null
RETVAL=$?
echo "Done."
}
# Stop MongoDB
stop() {
echo -n "Stopping MongoDB... "
killproc -p $PID_FILE $MONGOD
RETVAL=$?
echo "Done."
}
# Restart MongoDB
restart() {
stop
start
}
# Check MongoDB status
status() {
status $MONGOD
}
# Main program
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit $RETVAL
```
3. 按下Ctrl + X,然后按下Y来保存文件。
4. 接下来,您需要将启动脚本添加到系统服务中。运行以下命令:
```
sudo chkconfig --add mongod
```
5. 然后,使用以下命令将MongoDB添加到系统启动服务中:
```
sudo chkconfig mongod on
```
6. 最后,使用以下命令启动MongoDB服务:
```
sudo service mongod start
```
现在,MongoDB将在系统启动时自动启动。
阅读全文