MongoDB在CentOS下的装配、启动和配置

来源:互联网 发布:mysql 分组 字段累加 编辑:程序博客网 时间:2024/05/18 02:22
MongoDB在CentOS下的安装、启动和配置

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

 

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 平板不知道id地址和密码怎么办 红米1s刷机变砖了怎么办 车玻璃被鞭炮炸了黑印子怎么办 出轨的事被家人知道后道处传怎么办 村霸霸占土地弱势村民该怎么办? 户户通没有插卡位置信息改变怎么办 出现重大污染天气时企业该怎么办 电子税务句注册后未绑定企业怎么办 报税的时候PIN码忘了怎么办 购房合同丢失开发商不给补怎么办 租赁合同丢了房东不退押金怎么办 小孩不愿意喝奶粉爱喝乳酸菌怎么办 长安通不记名卡丢了怎么办 农村电表箱里的开关坏了怎么办 建行手机银行登录密码忘了怎么办 手机银行登入密码忘记了怎么办 邮政手机银行登录密码忘了怎么办 建设手机银行登入密码忘记了怎么办 浪琴机械表秒针走的快怎么办 雷达晶萃陶瓷表镀金掉色怎么办 做信息稿部分人员没拍到照片怎么办 二建条件不够考后审核怎么办 学校官网的教务系统忘记密码怎么办 已参加两次高考失败还想复读怎么办 我高考失利想补习学藉怎么办 本科毕业证上是1寸照片怎么办 老婆父母不给户口本迁户口怎么办 深圳夫妻投靠双方再婚的网上怎么办 老人档案丢了要继承公证怎么办 农民把户口迁入城市后宅基地怎么办 离婚了再婚带孩子在上海上学怎么办 上班几天被公司辞退不发工资怎么办 在单位工作被领导边缘化该怎么办 退休人员户口迁到外地退休金怎么办 招工表填写和实际的有误怎么办 招工时档案年龄有人为改动怎么办 8个月宝贝还不会坐怎么办 朗动导航黑屏过了保修期怎么办 平板突然黑屏开不了机了怎么办 苹果ipad锁屏密码忘了怎么办 公司发资以两张工资表怎么办