-- 安装mongo
操作系统为 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 #where to log logappend=true # fork and run in background port = 27018 dbpath=/data/mongodb27018/data # location of pidfile # Disables write-ahead journaling # Enables periodic logging of CPU utilization and I/O wait # Turn on/off security. Off is currently the default # Verbose logging output. # Inspect all client data for validity on receipt (useful for # Enable db quota management # Set oplogging level where n is # Ignore query hints # Disable the HTTP interface (Defaults to localhost:27018). # Turns off server-side scripting. This will result in greatly limited # Turns off table scans. Any query that would do a table scan fails. # Disable data file preallocation. # Specify .ns file size for new databases. # Accout token for Mongo monitoring server. # Server name for Mongo monitoring server. # Ping interval for Mongo monitoring server. # Replication Options # in replicated mongo databases, specify here whether this is a slave or master |
修改程序的连接池,会自动创建数据库hzresource
也可以先手动创建
代码如下 | 复制代码 |
#mongo --port 27018 use hzresource |
还需要创建表 db.renw.insert({'abc':'211'})
代码如下 | 复制代码 |
-- 启动 -- 测试上传文件 |
开机自动启动
代码如下 | 复制代码 |
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 |