关于MongoDB的windows的 安装,可以参考:
http://www.runoob.com/mongodb/mongodb-window-install.html
关于Linux的安装可以参考:
http://www.runoob.com/mongodb/mongodb-linux-install.html
使用MongoDB的场景:
A:需要写入大量的数据,但是这些数据的价值并不是太高,比如:日志
B:数据增长量较大,而且数据结构有时候还不一致
C:未来数据会很大。
注意:Mongodb有32位和64位之分,32位为数据库最大容量不能超过2GB
1、下载Linux下MongoDB的安装文件
下载地址:https://www.mongodb.com/download-center#community
选择Linux ,previous Release,然后选择指定版本进行安装,截图如下:
2、将下载好的mongodb-linux-x86_64-ubuntu1404-3.2.11.tgz 解压
3、将mongodb-linux-x86_64-ubuntu1404-3.2.11 中的内容移动到/usr/local/mongodb
mkdir -p /usr/local/mongodb
mv mongodb-linux-x86_64-ubuntu1404-3.2.11/* /usr/local/mongodb
创建数据库目录
cd /usr/local/mongodb
mkdir -p data/db
创建日志文件的目录
mkdir log
4、配置环境变量/etc/profile
vim /etc/profile
保存文件,让配置文件立即生效
source /etc/profile
5、通过命令的方式启动mongodb的方式如下:
cd /usr/local/mongodb
./bin/mongod -port 27017 -dbpath data/db --logpath log/mongodb.log --rest
注意上面启动的是mongodb的服务端
启动客户端的方式是:
cd /usr/local/mongodb
./bin/mongo mongodb机器ip:27017
查看mongodb的进程的方式:
ps -ef | grep mongodb
杀死mongodb进程的方式:pkill mongod
通过浏览器访问mongodb的控制台,之所以可以访问时因为在启动mongodb的时候后面加了一个 --rest
同理:window上的启动服务器端的示例:
mongod.exe --auth -port 27017 --dbpath "D:\installed\mongoDB\data\db" --logpath "D:\installed\mongoDB\docCheck\logs\MongoDB.log"
启动客户端的方式:
mongo.exe 127.0.0.1:27017
6、关于启动参数的说明(下面以window下的配置说明为示例):
--auth //安全验证方式启动(即需要用户名和密码)
--dbpath "D:\installed\mongoDB\data\db" //设定数据文件存放地址
--logpath "D:\installed\mongoDB\docCheck\logs\MongoDB.log" //设定日志文件存放地址
7、通过配置文件来配置Mongodb
首先在mongodb目录下 新建一个文件,文件名任意,在这里取名为:mongodb.conf
# vim mongodb.conf
内容如下:
port=27017
replSet=docdetection #这个是创建副本集的方式
dbpath=/usr/local/mongodb/data/db
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
fork=true
noauth=true
smallfiles=true
noprealloc=true
rest=true
启动它:
root@localhost:/usr/local/mongodb# ./bin/mongod -f mongodb.conf
启动客户端测试:
root@localhost:/usr/local/mongodb# ./bin/mongo localhost:27017
MongoDB shell version: 3.2.11
connecting to: localhost:27017/test
Server has startup warnings:
2017-02-19T18:39:14.605+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-02-19T18:39:14.605+0800 I CONTROL [initandlisten]
2017-02-19T18:39:14.605+0800 I CONTROL [initandlisten]
2017-02-19T18:39:14.605+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-02-19T18:39:14.605+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-02-19T18:39:14.605+0800 I CONTROL [initandlisten]
2017-02-19T18:39:14.605+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-02-19T18:39:14.606+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-02-19T18:39:14.606+0800 I CONTROL [initandlisten]
8、自启动脚本:
vi /etc/mongodb.conf
输入以下内容:
port=27017
replSet=docdetection #这个是创建副本集的方式
dbpath=/usr/local/mongodb/data/db
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
fork=true
noauth=true
smallfiles=true
noprealloc=true
rest=true
下面加入自启动
cd /etc/init.d/
vi mongodb
添加一下内容:
#!/bin/bash
start() {
/usr/local/mongodb/mongod -f /etc/mongodb.conf
}
stop() {
/usr/local/mongodb/mongod -f /etc/mongodb.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage:$0 {start|stop|restart}"
exit 1
esac
加入自启动:
chkconfig --add mongodb
chmod +x mongodb
chkconfig mongodb on
如果是ubuntu
update-rc.d mongodb defaults
若方便重启,可以使用reboot重启测试(也可以不重启进行测试)
测试如下:
root@localhost:/etc/init.d# ./mongodb start
note: noprealloc may hurt performance in many applications
about to fork child process, waiting until server is ready for connections.
forked process: 13622
child process started successfully, parent exiting
root@localhost:/etc/init.d# ./mongodb stop
note: noprealloc may hurt performance in many applications
killing process with pid: 13738
root@localhost:/etc/init.d# ./mongodb restart
note: noprealloc may hurt performance in many applications
There doesn't seem to be a server running with dbpath: /usr/local/mongodb/data/db
note: noprealloc may hurt performance in many applications
about to fork child process, waiting until server is ready for connections.
forked process: 13764
child process started successfully, parent exiting
root@localhost:/etc/init.d# /usr/local/mongodb/bin/mongo
mongo mongodump mongofiles mongooplog mongorestore mongosniff mongotop
mongod mongoexport mongoimport mongoperf mongos mongostat
root@localhost:/etc/init.d# /usr/local/mongodb/bin/mongo admin
MongoDB shell version: 3.2.11
connecting to: admin
Server has startup warnings:
2017-02-19T20:48:05.038+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-02-19T20:48:05.038+0800 I CONTROL [initandlisten]
2017-02-19T20:48:05.038+0800 I CONTROL [initandlisten]
2017-02-19T20:48:05.038+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-02-19T20:48:05.038+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-02-19T20:48:05.038+0800 I CONTROL [initandlisten]
2017-02-19T20:48:05.038+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-02-19T20:48:05.038+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-02-19T20:48:05.038+0800 I CONTROL [initandlisten]
> show dbs
local 0.000GB
>
以管理员身份进入管理后台
/usr/local/mongodb/bin/mongo admin
发现结果是:
root@localhost:/usr/local/mongodb# ./bin/mongo admin
MongoDB shell version: 3.2.11
connecting to: admin
Server has startup warnings:
2017-02-19T20:33:58.602+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-02-19T20:33:58.602+0800 I CONTROL [initandlisten]
2017-02-19T20:33:58.603+0800 I CONTROL [initandlisten]
2017-02-19T20:33:58.603+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-02-19T20:33:58.603+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-02-19T20:33:58.603+0800 I CONTROL [initandlisten]
2017-02-19T20:33:58.603+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-02-19T20:33:58.603+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-02-19T20:33:58.603+0800 I CONTROL [initandlisten]
> show dbs;
local 0.000GB
经过以上步步骤使得mongodb通过脚本自动启动
9、Windows下创建MongoDB的启动服务
A:在MongoDB目录下新建一个logs文件夹用于存放日志文件,然后组合键win + x + a,打开管理员命令提示符。 在左侧选择管理员:命令提示符
C:\WINDOWS\system32>echo logpath=D:\installed\mongoDB\docCheck\logs\MongoDB.log > "D:\installed\mongoDB\docCheck\mongod.cfg"
C:\WINDOWS\system32>echo dbpath=D:\installed\mongoDB\data\db >> "D:\installed\mongoDB\docCheck\mongod.cfg"
B:准备工作已经做完,下面开始最重要的一步,使用SC命令创建MongoDB服务:
C:\WINDOWS\system32>sc.exe create MongoDB binpath="D:\installed\mongoDB\docCheck\bin\mongod.exe --service --config=D:\installed\mongoDB\docCheck\mongod.cfg"
[SC] CreateService 成功
C:\WINDOWS\system32>
C:开启服务,使用如下命令:
C:\WINDOWS\system32>net start MongoDB
MongoDB 服务已经启动成功。
截图如下:
然后win + r,输入services.msc打开windows服务列表,你会看到一个名称为MongoDB,状态为"正在运行"的服务。
到此为止你就可以从服务中启动MongoDB,然后使用它的服务而不用一直保留着另一个服务端的DOS窗口了。