MongoDB安装、配置、启动(MAC)

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



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

cp -r mongodb-osx-x86_64-3.0.4 /usr/local/mongodb


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

cd /usr/local/mongodb
mkdir data
mkdir log


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

cd bin
vim mongodb.conf


5.编写配置文件

port=27017
dbpath=/usr/local/mongodb/data/
logpath=/usr/local/mongodb/log/mongodb.log  
fork = true
port: 数据库服务使用端口

dbpath: 数据存放的文件位置

logpath: 日志文件的存放位置

fork: 后台守护进程运行


5.启动

在bin路径下,执行

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

about to fork child process, waiting until server is ready for connections.
forked process: 779
child process started successfully, parent exiting


附:

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

about to fork child process, waiting until server is ready for connections.
forked process: 760
ERROR: child process failed, exited with error number 1
一般情况下是权限问题,使用sodu操作来解决,

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


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

./mongo


7.关闭MongoDB服务

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

8.查看 mongo 用法

在bin目录下输入:

./mongo --help
显示内容如下
MongoDB shell version: 3.0.4
usage: ./mongo [options] [db address] [file names (ending in .js)]
db address can be:
  foo                   foo database on local machine
  192.169.0.5/foo       foo database on 192.168.0.5 machine
  192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999
Options:
  --shell                            run the shell after executing files
  --nodb                             don't connect to mongod on startup - no 
                                     'db address' arg expected
  --norc                             will not run the ".mongorc.js" file on 
                                     start up
  --quiet                            be less chatty
  --port arg                         port to connect to
  --host arg                         server to connect to
  --eval arg                         evaluate javascript
  -h [ --help ]                      show this usage information
  --version                          show version information
  --verbose                          increase verbosity
  --ipv6                             enable IPv6 support (disabled by default)

Authentication Options:
  -u [ --username ] arg              username for authentication
  -p [ --password ] arg              password for authentication
  --authenticationDatabase arg       user source (defaults to dbname)
  --authenticationMechanism arg      authentication mechanism
  --gssapiServiceName arg (=mongodb) Service name to use when authenticating 
                                     using GSSAPI/Kerberos
  --gssapiHostName arg               Remote host name to use for purpose of 
                                     GSSAPI/Kerberos authentication

file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified

9.连接数据库

在bin目录下执行:

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

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

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


10.查看日志

tail -f log/mongod.log
log/mongod.log 为日志存放路径


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值