4>启动服务
./mongodb/bin/mongod -dbpath=/data/mongodb/db -logpath=/data/mongodb/log
mongodb 是mongo解压后的目录,下同
6>把mongod 作为服务开机启动
先在/etc/rc.d/init.d下用vi 新建文件 mongod
内容如下:
#!/bin/bash
#
#chkconfig: 2345 80 90
#description: mongodb
start() {
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/config/mongod.conf
}
stop() {
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/config/mongod.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo
$"Usage: $0 {start|stop|restart}"
exit 1
esac
保存
/usr/mongodb/config/mongod.conf 配置文件的内容如下
dbpath=/data/db
logpath=/data/mongodb/db.log
pidfilepath=/data/mongodb/db.pid
directoryperdb=true
logappend=true
port=27017
oplogSize=1000
fork=true
noprealloc=false
nojournal=true
smallfiles=true
2、增加服务并开机启动
chmod +x /etc/rc.d/init.d/mongod
chkconfig --add mongod
chkconfig --level 345 mongod on
chkconfig --list mongod
service mongod start
执行该脚本后,就可以开始start|stop|restart|list你的服务了。
以后关机再启动就会自动启动mongo了,如果在同一台机需要启动多个mongod照此方法即可
暂时只知道这样设为开机启动服务,如果有更好的方式请告知哦,先谢了!
7>连接mongoDB
./mongodb/bin/mongo 27017
即可连上mongo
讲到这儿,mongoDB的在Linux下安装已完成,本地连接mongoDB也已成功,这时我们就要考虑到另外一个问题了,局域网如何来连接mongoDB呢?局域网中windows机器如何来连接Linux机器中的mongoDB呢?
其实做法一样很简单:./mongodb/bin/mongo 192.168.10.234:port即可。
不过此处就需要注意了,我们需要在centOS上打开mongoDB的端口号,接下来讲讲如何在centOS上打开指定端口。
我们打开配置文件 /etc/sysconfig/iptables,在该文件中添加如下内容:
-A RH-Firewall-l-INPUT -P tcp -m tcp --dport mongoDB端口号 -j ACCEPT
然后重启服务
service iptables restart
此时,你已可以开始通过局域网来访问centOS上部署的mongoDB
顺便再增加一点centOS与windows互访的知识,譬如,我们想把原来在windows机器上的mongoDB产生的文件移植到centOS中,当然可以用移动存储设备来拷贝,但是我这里讲的是Linux(centOS)如何来访问windows共享目录,命令如下:
mount -t cifs //ip/共享目录名称 /mnt/sharefile -o username=,password=
上面的命令即将windows的共享目录映射为linux上的/mnt/sharefile目录