Centos x64位下安装mongodb数据库

作者:袖梨 2022-11-14

-- 安装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

# 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 =

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

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

# Ping interval for Mongo monitoring server.
#mms-interval =

# 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 = www.111com.net


修改程序的连接池,会自动创建数据库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

相关文章

精彩推荐