作者:ynq02008146 | 来源:互联网 | 2024-10-09 15:21
篇首语:本文由编程笔记#小编为大家整理,主要介绍了安装配置MongoDB数据库相关的知识,希望对你有一定的参考价值。
安装配置MongoDB数据库
实验环境:
系统环境:rhel6 x64 selinux and iptables disabled
一、安装MongoDB
下载地址:
https://www.mongodb.com/download-center#community
[[email protected] ~]# ls
mongodb-linux-x86_64-rhel62-4.0.0
[[email protected] ~]# mv mongodb-linux-x86_64-rhel62-4.0.0/ /usr/local/mongodb #移动解压文件夹到MongoDB安装目录
[[email protected] ~]# mkdir -p /home/data/mongodb/mongodb_data #创建MongoDB数据库存放路径
[[email protected] ~]# mkdir -p /home/data/mongodb/mongodb_log #创建MongoDB数据库日志存放路径
#启动MongoDB
[[email protected] ~]# /usr/local/mongodb/bin/mongod --port 27017 --fork --dbpath=/home/data/mongodb/mongodb_data/ --logpath=/home/data/mongodb/mongodb_log/mongodb.log --logappend
查看端口
[[email protected] ~]# netstat -antlp #查看MongoDB是否启动
[[email protected] ~]# cd /usr/local/mongodb/bin/
[[email protected] bin]# ./mongo #进入MongoDB数据库控制台
> 2+3
5
> 2*3
6
>
> use admin #进入admin数据库
switched to db admin
> db.shutdownServer() #关闭MongoDB数据库
server should be down...
2018-07-22T21:25:17.131+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2018-07-22T21:25:17.131+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
> exit #退出
bye
[[email protected] bin]# netstat -antlp | grep :27017 已经关闭
[[email protected] bin]#
再次进入MongoDB数据库控制台时如下报错:
[[email protected] bin]# ./mongo
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
2018-07-22T22:07:54.310+0800 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
[email protected]/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed
解决途径:
(1) cd /usr/local/mongodb/bin
(2) ./mongod --dbpath=/home/data/mongodb/mongodb_data/
(3) 上一步路径设置成功,但是不要关闭这个界面,需要重新打开一个新的窗口来启动mongo
(4)在另一个终端:cd /usr/local/mongodb/
./bin/mongo
做下面时,先关闭MongoDB数据库
二、设置MongoDB数据库
[[email protected] bin]# cd /usr/local/mongodb/ #进入MongoDB安装目录
[[email protected] mongodb]# vim mongodb.conf #编写配置文件
port=27017 #端口号
dbpath=/home/data/mongodb/mongodb_data/ #数据库路径
logpath=/home/data/mongodb/mongodb_log/mongodb.log #日志输出文件路径
logappend=true #日志输出方式
[[email protected] ~]# cd /usr/local/mongodb/
[[email protected] bin]# pwd
/usr/local/mongodb/bin
[[email protected] bin]# ./mongod -f mongodb.conf
查看端口:
三、Linux下注册mongodb服务
进入 /etc/init.d/ 目录
[[email protected] bin]# cd /etc/init.d/
创建mongodb文件
[[email protected] init.d]# vim mongodb
#!/bin/sh
#
# ### BEGIN INIT INFO
# Short-Description: mongodb
# Description: mongo db server
### END INIT INFO
PROGRAM=/usr/local/mongodb/bin/mongod
MOnGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`
test -x $PROGRAM || exit 0
case "$1" in
start)
ulimit -n 3000
echo "Starting MongoDB server"
$PROGRAM --fork --quiet -f /usr/local/mongodb/mongodb.conf
echo "Started MongoDB server"
;;
stop)
echo "Stopping MongoDB server"
if [ ! -z "$MONGOPID" ]; then
kill -15 $MONGOPID
fi
echo "Stopped MongoDB server"
;;
status)
;;
*)
echo "Usage: mongodb {start|stop|status}"
exit 1
esac
exit 0
保存完毕,在执行以下几条命令即可:
[[email protected] init.d]# chmod +x mongodb
测试:
[[email protected] init.d]# /etc/init.d/mongodb start
[[email protected] init.d]# netstat -antlp | grep :27017