MongoDB在CentOS下的装配、启动和配置
来源:互联网 发布:mysql 分组 字段累加 编辑:程序博客网 时间:2024/05/18 02:22
1、安装
下载链接:http://www.mongodb.org/downloads
下载安装包
下载版本:rhel70-3.0.0
下载链接:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.0.0.tgz
可以在windowns下载后通过ssh工具上传到CentOS或直接用下面命令下载
[root@clinton software]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.0.0.tgz
下载完后,解压:
[root@clinton software]# tar -zxvf mongodb-linux-x86_64-rhel70-3.0.0.tgzmongodb-linux-x86_64-rhel70-3.0.0/READMEmongodb-linux-x86_64-rhel70-3.0.0/THIRD-PARTY-NOTICESmongodb-linux-x86_64-rhel70-3.0.0/GNU-AGPL-3.0mongodb-linux-x86_64-rhel70-3.0.0/bin/mongodumpmongodb-linux-x86_64-rhel70-3.0.0/bin/mongorestoremongodb-linux-x86_64-rhel70-3.0.0/bin/mongoexportmongodb-linux-x86_64-rhel70-3.0.0/bin/mongoimportmongodb-linux-x86_64-rhel70-3.0.0/bin/mongostatmongodb-linux-x86_64-rhel70-3.0.0/bin/mongotopmongodb-linux-x86_64-rhel70-3.0.0/bin/bsondumpmongodb-linux-x86_64-rhel70-3.0.0/bin/mongofilesmongodb-linux-x86_64-rhel70-3.0.0/bin/mongooplogmongodb-linux-x86_64-rhel70-3.0.0/bin/mongoperfmongodb-linux-x86_64-rhel70-3.0.0/bin/mongodmongodb-linux-x86_64-rhel70-3.0.0/bin/mongosmongodb-linux-x86_64-rhel70-3.0.0/bin/mongo[root@clinton software]#
一般我们将安装的软件安装在/usr/local目录下,所以将解压后的文件移至/usr/local目录,并重命名,如下
[root@clinton software]# mv mongodb-linux-x86_64-rhel70-3.0.0 /usr/local/mongodb[root@clinton software]# cd /usr/local/
我们在mongodb目录下新建一个目录data用于存放数据,新建一个目录log用于存放日志,然后在该目录下新建一个日志文件mongodb.log,操作如下
[root@clinton local]# cd mongodb/[root@clinton mongodb]# ll总用量 68drwxr-xr-x. 2 root root 4096 3月 12 15:55 bin-rw-r--r--. 1 root root 34520 2月 28 01:43 GNU-AGPL-3.0-rw-r--r--. 1 root root 1359 2月 28 01:43 README-rw-r--r--. 1 root root 22660 2月 28 01:43 THIRD-PARTY-NOTICES[root@clinton mongodb]# mkdir data[root@clinton mongodb]# mkdir log[root@clinton mongodb]# cd log/[root@clinton log]# touch mongodb.log
2、启动mongodb
将目录定位到/usr/local/mongodb
[root@clinton log]# cd /usr/local/mongodb
使用mongod命令建立一个mongodb数据库链接,端口号设置为10001,数据库的路径为/usr/local/mongodb/data,日志路径为/usr/local/mongodb/log/mogodb.log
注:可用 --help查看命令参数的作用
[root@clinton mongodb]# ./bin/mongod --port 10001 --dbpath data/ --logpath log/mongodb.log2015-03-12T16:18:33.489+0800 I CONTROL log file "/usr/local/mongodb/log/mongodb.log" exists; moved to "/usr/local/mongodb/log/mongodb.log.2015-03-12T08-18-33".
以上方式是在前台启动Mongodb进程,如果Session窗口关闭,Mongodb进程也随之停止,我们的客户端要连接需要另外开启一个终端。不过Mongodb同时还提供了一种后台Daemon方式启动,只需要加上一个"--fork"参数即可,值得注意的是,用到了"--fork"参数就必须启用"--logpath"参数。如下所示:
[root@clinton mongodb]# ./bin/mongod --port 10001 --dbpath data/ --forkBadValue --fork has to be used with --logpath or --syslogtry './bin/mongod --help' for more information[root@clinton mongodb]# ./bin/mongod --port 10001 --dbpath data/ --fork --logpath=log/mongodb.logabout to fork child process, waiting until server is ready for connections.forked process: 6288child process started successfully, parent exiting[root@clinton mongodb]# ps -ef|grep mongodbroot 6288 1 1 16:27 ? 00:00:00 ./bin/mongod --port 10001 --dbpath data/ --fork --logpath=log/mongodb.logroot 6304 2292 0 16:27 pts/0 00:00:00 grep --color=auto mongodb[root@clinton mongodb]#
3、客户端连接
将目录切换到mongodb目录下,并使用mongo命令来连接该数据库
[root@clinton mongodb]# pwd/usr/local/mongodb[root@clinton mongodb]# ./bin/mongo localhost:10001MongoDB shell version: 3.0.0connecting to: localhost:10001/testServer has startup warnings:2015-03-12T16:27:20.453+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-03-12T16:27:20.453+0800 I CONTROL [initandlisten]2015-03-12T16:27:20.453+0800 I CONTROL [initandlisten]2015-03-12T16:27:20.453+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2015-03-12T16:27:20.453+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'2015-03-12T16:27:20.453+0800 I CONTROL [initandlisten]2015-03-12T16:27:20.453+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-03-12T16:27:20.453+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'2015-03-12T16:27:20.453+0800 I CONTROL [initandlisten]>
往数据库中插入值
> db.foo.save({a:1})WriteResult({ "nInserted" : 1 })>
从数据库中查询
> db.foo.find(){ "_id" : ObjectId("55014b84b66287928352f381"), "a" : 1 }>
通过浏览器访问
在浏览器地址栏输入: http://localhost:10001/ 然后回车访问 (localhost可以换成具体的服务器IP)
可以看到如下提示:It looks like you are trying to access MongoDB over HTTP on the native driver port.
不知道什么原因,google了一下,发现在官网有如下解释:
--httpinterface
New in version 2.6.
Enables the HTTP interface. Enabling the interface can increase network exposure.
Leave the HTTP interface disabled for production deployments. If you do enable this interface, you should only allow trusted clients to access this port. See Firewalls.
应该是自2.6版本以后默认就不开启HTTP接口访问,于是重启加上--httpinterface启动
[root@clinton mongodb]# ./bin/mongod --port 10001 --dbpath data/ --fork --logpath=log/mongodb.log --httpinterfaceabout to fork child process, waiting until server is ready for connections.forked process: 7760child process started successfully, parent exiting
再在地址栏中输入:http://localhost:10001/ 回车
发现仍然有上面的提示:It looks like you are trying to access MongoDB over HTTP on the native driver port.
之前的版本直接访问10001端口会提示:You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number
于是把端口加上1000,http://localhost:11001/,回车后,就能够访问到Monodb的服务端web页面
在CentOS中
在windowns中
4、通过配置文件来配置Mongodb
创建mongodb的配置文件conf/mongodb.conf
[root@clinton mongodb]# pwd/usr/local/mongodb[root@clinton mongodb]# mkdir conf[root@clinton mongodb]# cd conf[root@clinton conf]# vim mongodb.conf
在mongodb.conf中加入下面内容:
port=10001dbpath=/usr/local/mongodb/data/logpath=/usr/local/mongodb/log/mongodb.loglogappend=truefork=true
解释说明:
port=10001【代表端口号,如果不指定则默认为 27017 】
dbpath=data/ 【数据库路径】
logpath=log/mongodb.log 【日志路径】
logappend=true 【日志文件自动累加,而不是覆盖】
fork=true 【后台执行方式启动】
启动mongodb服务
[root@clinton mongodb]# ./bin/mongod -f conf/mongodb.conf --httpinterfaceabout to fork child process, waiting until server is ready for connections.forked process: 8421child process started successfully, parent exiting
然后访问方式和上面一样
停掉服务
关闭mongodb可以用如下方式
[root@clinton mongodb]# ps -ef|grep mongodbroot 8421 1 0 17:35 ? 00:00:01 ./bin/mongod -f conf/mongodb.conf --httpinterfaceroot 8459 2292 0 17:38 pts/0 00:00:00 grep --color=auto mongodb[root@clinton mongodb]# kill -9 8421[root@clinton mongodb]#
- MongoDB在CentOS下的装配、启动和配置
- Mongodb在Linux下的安装和启动和配置
- Mongodb在Linux下的安装和启动和配置
- Mongodb在Linux下的安装和启动和配置
- Mongodb在ubuntu下的安装和启动和配置
- MongoDB centos下的安装和启动
- MongoDB在Linux下安装和启动和配置
- mongodb 在linux下的安装和启动
- MongoDB 在Macox 下的安装和启动
- Mongodb在windows环境下的安装和启动
- Mongodb在windows下的安装和启动
- Mongodb在windows下的安装和启动
- Mongodb在windows下的安装和启动
- Mongodb在windows下的安装和启动
- Mongodb在windows下的安装和启动
- 在Ubuntu下mongodb的安装和启动
- MongoDB在Windows系统下的安装和启动
- Linux下Mongodb安装和启动配置
- HDU 5868 Different Circle Permutation(dp+polya+矩阵快速幂+欧拉函数)
- IO技术(五)流的使用规律和键盘读入数据
- 《Java编程思想第四版》笔记---16章 数组
- Google 2016面试题
- 虚拟机NAT方式解决主机PING不同虚拟机的问题
- MongoDB在CentOS下的装配、启动和配置
- 国内三大云数据库测试对比
- 使用DELIMITER创建存储过程的用法
- 计算机内存储整数的总结
- HDU1541-Stars
- 求解二叉树第K层节点数目
- bzoj 2728: [HNOI2012]与非
- 《Java编程思想第四版》笔记---17章 容器
- 深度学习笔记(一):logistic分类