Mac 安装mongodb

http://blog.csdn.net/u010311313/article/details/46948995

1.前往官网下载MongoDB压缩包

 

2.将下载好的压缩包解压,将解压出的文件夹下的内容全部复制到新的路径下。

 

[plain] view plain copy
  1. cp -r mongodb-osx-x86_64-3.0.4 /usr/local/mongodb  



 

3. 在新建立的文件夹下建立data文件夹用来记录数据,log文件夹用来记录日志

 

[plain] view plain copy
  1. cd /usr/local/mongodb  
[plain] view plain copy
  1. mkdir data  
[plain] view plain copy
  1. mkdir log  

 

 

4.进入bin目录下,创建mongodb.conf配置文件

 

[plain] view plain copy
  1. cd bin  
[plain] view plain copy
  1. vim mongodb.conf  

 

 

5.编写配置文件

 

[html] view plain copy
  1. port=27017  
  2. dbpath=/usr/local/mongodb/data/  
  3. logpath=/usr/local/mongodb/log/mongodb.log    
  4. fork = true  

port: 数据库服务使用端口

 

dbpath: 数据存放的文件位置

logpath: 日志文件的存放位置

fork: 后台守护进程运行

 

5.启动

在bin路径下,执行

 

[html] view plain copy
  1. ./mongod -f mongodb.conf  

-f 后面写要使用的配置文件
启动成功后会打印类似于这样的信息:

 

 

[html] view plain copy
  1. about to fork child process, waiting until server is ready for connections.  
  2. forked process: 779  
  3. child process started successfully, parent exiting  

 

 

附:

如果未启动成功,错误信息如下的话:

 

[html] view plain copy
  1. about to fork child process, waiting until server is ready for connections.  
  2. forked process: 760  
  3. ERROR: child process failed, exited with error number 1  

一般情况下是权限问题,使用sodu操作来解决,

 

也可能是配置文件中路径写的有问题。

 

6.在bin目录下进入MongoDB控制台

 

[plain] view plain copy
  1. ./mongo  

 

 

7.关闭MongoDB服务

在 ./mongo 进入控制台后,输入 use admin,然后输入 db.shutdownServer()

8.查看 mongo 用法

在bin目录下输入:

 

[plain] view plain copy
  1. ./mongo --help  

显示内容如下

[plain] view plain copy
  1. MongoDB shell version: 3.0.4  
  2. usage: ./mongo [options] [db address] [file names (ending in .js)]  
  3. db address can be:  
  4.   foo                   foo database on local machine  
  5.   192.169.0.5/foo       foo database on 192.168.0.5 machine  
  6.   192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999  
  7. Options:  
  8.   --shell                            run the shell after executing files  
  9.   --nodb                             don't connect to mongod on startup - no   
  10.                                      'db address' arg expected  
  11.   --norc                             will not run the ".mongorc.js" file on   
  12.                                      start up  
  13.   --quiet                            be less chatty  
  14.   --port arg                         port to connect to  
  15.   --host arg                         server to connect to  
  16.   --eval arg                         evaluate javascript  
  17.   -h [ --help ]                      show this usage information  
  18.   --version                          show version information  
  19.   --verbose                          increase verbosity  
  20.   --ipv6                             enable IPv6 support (disabled by default)  
  21.   
  22. Authentication Options:  
  23.   -u [ --username ] arg              username for authentication  
  24.   -p [ --password ] arg              password for authentication  
  25.   --authenticationDatabase arg       user source (defaults to dbname)  
  26.   --authenticationMechanism arg      authentication mechanism  
  27.   --gssapiServiceName arg (=mongodb) Service name to use when authenticating   
  28.                                      using GSSAPI/Kerberos  
  29.   --gssapiHostName arg               Remote host name to use for purpose of   
  30.                                      GSSAPI/Kerberos authentication  
  31.   
  32. file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified  


9.连接数据库

 

在bin目录下执行:

 

[plain] view plain copy
  1. ./mongo 127.0.0.1:27017/test  

./mongo + 服务器IP:端口号/数据库名

 

服务器IP我这写的是本机,端口号是刚才在配置文件中配置的那个,数据库名自己起

回车后如果能看到向右的箭头,说明已经成功了

 

10.查看日志

 

[plain] view plain copy
  1. tail -f log/mongod.log  

log/mongod.log 为日志存放路径

posted @ 2017-08-07 18:23  吻上明天  阅读(371)  评论(0编辑  收藏  举报