linux命令 Python linux shell 微软 HTML5 google centos apache wordpress java Ubuntu nginx Windows Android mysql 开源 php 程序员 Firefox

Centos x64 mongodb安装

操作系统为 CentOS x64 在官网下载.tar.gz文件

cd /data/

http://fastdl.mongodb.org/Linux/mongodb-linux-x86_64-2.4.3.tgz

tar zxf mongodb-linux-x86_64-2.4.3.tgz
mv mongodb-linux-x86_64-2.4.3  mongodb

增加mongodb用户

[root@test ~]# groupadd mongodb
[root@test ~]# useradd -g mongodb mongodb -s /bin/bash

加入环境变量

echo "export PATH=$PATH:/data/mongodb/bin">> /etc/profile
source  /etc/profile

创建目录

mkdir /data/mongodb27018
mkdir /data/mongodb27018/data

改变目录属性

chown -R mongodb.mongodb /data/mongodb
chown -R mongodb.mongodb /data/mongodb27018

编辑配置文件:

vi /data/mongodb27018/mongodb.conf

# mongo.conf

#where to log
logpath=/data/mongodb27018/mongod.log

logappend=true

# fork and run in background
fork = true

port = 27018

dbpath=/data/mongodb27018/data

# location of pidfile
pidfilepath = /data/mongodb27018/mongod.pid

# Disables write-ahead journaling
# nojournal = true

# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true

# Verbose logging output.
#verbose = true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true

# enable db quota management
#quota = true

# set oplogging level where n is
#   0=off (default)
#   1=w
#   2=R
#   3=both
#   7=W+some reads
#diaglog = 0

# Ignore query hints
#nohints = true

# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = true

# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting = true

# Turns off table scans.  Any query that would do a table scan fails.
#notablescan = true

# Disable data file preallocation.
#noprealloc = true

# Specify .ns file size for new databases.
# nssize = <size>

# Accout token for Mongo monitoring server.
#mms-token = <token>

# Server name for Mongo monitoring server.
#mms-name = <server-name>

# ping interval for Mongo monitoring server.
#mms-interval = <seconds>

# Replication Options

# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com

修改程序的连接池,会自动创建数据库hzresource

也可以先手动创建

#mongo --port 27018
use hzresource

还需要创建表 db.renw.insert({'abc':'211'})

启动

su -mongod -c /data/mongodb/bin/mongod -f /data/mongodb27018/mongod.conf

cd /data/mongodb/bin
mongod -f mongo.conf

测试上传文件

mongofiles --port 27018 -d hzresource put 820f2c09tw1dzbttirkqwj.jpg

开机自动启动

echo "su -mongod -c /data/mongodb/bin/mongod -f /data/mongodb27018/mongod.conf --logappend --port=27018 --fork --directoryperdb --journal" >> /etc/rc.local

mongodb启动 关闭脚本

vi mongodb_start_stop.sh

#!/bin/bash
#使用 sh mongodb_start_stop.sh start启动

#使用 sh mongodb_start_stop.sh stop关闭

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
ac=$1
function start_mongodb(){
    /data/mongodb/bin/mongod --config /data/mongodb27018/mongod.conf
    echo -e "mongodb启动成功!"
}
 
function stop_mongodb(){
    echo "正在停止..."
    pid=`ps -ef|grep "/data/mongodb27018/mongod.conf"|grep -v grep|awk '{print $2}'`;
    echo $pid
    if [ "${pid}" != "" ]; then
        kill -2 ${pid};
        echo -e "mongodb停止成功!"
    else
        echo -e "mongodb未启动过!"
    fi
}
 
function restart_mongodb(){
    stop_mongodb
    start_mongodb
}
 
if [ "$ac" == "start" ]; then
    start_mongodb
elif [ "$ac" == "stop" ];then
    stop_mongodb
elif [ "$ac" == "restart" ]; then
    restart_mongodb
fi
exit 0

延伸阅读

评论