centos安装使用单机mongodb

下载适合的安装包

curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-4.0.2.tgz

或者手动去官网下载
Version: RHEL 6 Linux 64-bit x64 版本的tar包

解压tar包

tar -zxvf mongodb-linux-x86_64-rhel62-4.0.2.tgz

mv mongodb-linux-x86_64-rhel62-4.0.2/ /usr/local/mongodb

进入mongdb安装目录

cd /usr/local/mongodb

新建数据文件存储目录 (默认mongdb会在根目录下建data文件)

mkdir -p data/db
mkdir -p data/backup
mkdir -p data/logs
touch data/logs/mongodb.log

设置环境变量

pwd=$(pwd)

export PATH=$pwd/bin:$PATH

vi /etc/profile

export PATH=/usr/local/mongodb/bin:$PATH

source /etc/profile

启动mongod程序

./bin/mongod –dbpath=data/db –bind_ip 127.0.0.1 –port 27017 –logpath data/logs/mongodb.log –logappend &

或者

cd bin/

./mongod –dbpath=/usr/local/mongodb/data/db –bind_ip 127.0.0.1 –port 27017 –logpath /usr/local/mongodb/data/logs/mongodb.log –logappend –fork

image

关闭数据

mongod –shutdown –dbpath=/usr/local/mongodb/data/db –bind_ip 127.0.0.1 –port 27017 –logpath /usr/local/mongodb/data/logs/mongodb.log –logappend –fork

或者使用kill命令

kill -2 PID

  原理:-2表示向mongod进程发送SIGINT信号。

kill -4 PID

  原理:-4表示向mognod进程发送SIGTERM信号。   

使用mongo shell连接数据库

./mongo –host 127.0.0.1

或者指定用户连接

mongo 10.0.0.152/admin

关闭数据库

use admin ;
db.shutdownServer()

  

新建用户

use admin

新建管理员用户

db.createUser({ user: ‘root’, pwd: ‘123456’, roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ] });

use test;

db.createUser({user:”testuser”,pwd:”testpass”,roles:[“readWrite”]});

db.auth(“testuser”,”testpass”)

mongodb web界面启动方式

./mongod –dbpath=/data/db –rest

使用配置文件方式管理数据库

cd /usr/local/mongodb/

mkdir -p conf

vi conf/mongod.conf

推荐使用

systemLog:
destination: file
path: "/usr/local/mongodb/data/logs/mongodb.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/usr/local/mongodb/data/db"
processManagement:
fork: true
pidFilePath: /usr/local/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0

或者

dbpath=/usr/local/mongodb/data/db
logpath=/usr/local/mongodb/data/logs/mongodb.log
port=27017
logappend=1
fork=1

启动mongdb

mongod -f mongod.conf

关闭

mongod -f mongod.conf –shutdown

配置mongdb服务

  1. 先添加用户

groupadd -g 800 mongod

useradd -u 801 -g mongod mongod

设置密码

echo 123456 |passwd –stdin mongod

设置主目录权限

chown -R mongod:mongod /usr/local/mongodb

cd /etc/init.d/

touch mongod

chmod 777 mongod

vi mongod

#!/bin/bash
#chkconfig: 2345 80 90
#description: mongodb
#by maotiski
#site:http:matosiki.site
MONGODIR=/usr/local/mongodb
MONGOD=$MONGODIR/bin/mongod
MONGOCONF=$MONGODIR/conf/mongod.conf
InfoFile=/tmp/start.mongo
. /etc/init.d/functions
status(){
PID=`awk 'NR==2{print $NF}' $InfoFile`
Run_Num=`ps -p $PID|wc -l`
if [ $Run_Num -eq 2 ]; then
echo "MongoDB is running"
else
echo "MongoDB is shutdown"
return 3
fi
}
start() {
status &>/dev/null
if [ $? -ne 3 ];then
action "start mongdb,service running..." /bin/false
exit 2
fi
sudo su - mongod -c "$MONGOD -f $MONGOCONF" >$InfoFile 2>/dev/null
if [ $? -eq 0 ];then
action "start mongdb" /bin/true
else
action "start mongdb" /bin/false
fi
}
stop() {
sudo su - mongod -c "$MONGOD -f $MONGOCONF --shutdown" &>/dev/null
if [ $? -eq 0 ];then
action "stop mongdb" /bin/true
else
action "stop mongdb" /bin/false
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 2
start
;;
status)
status
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac

chkconfig –add mongod

chkconfig mongod on

service mongod start

错误案例

./etc/init.d/functions: Permission denied

注意这行代码 . /etc/init.d/functions 点与斜杠有个空格

或者是权限不够更改权限

chmod a+x /etc/init.d/functions

本文总阅读量