mac使用Homebrew安装MongoDB

MongoDB是一个免费开源且跨平台的基于文件的数据库,属于NoSQL数据库大家族中的一员。本文将演示如何一步步安装并启动MongoDB数据库。

安装mongodb

在这里使用Homebrew安装mongodb,首先更新Homebrew:

$brew update

终端中输出以下信息:


Updated Homebrew from 60e3737 to e9cc2a5.

Updated 1 tap (homebrew/core).

==> Cleaning up /Library/Caches/Homebrew...

Removing: /Library/Caches/Homebrew/git-2.8.2.el_capitan.bottle.tar.gz... (11.2M)

==> Migrating /Library/Caches/Homebrew to /Users/chenxin/Library/Caches/Homebrew

==> Deleting /Library/Caches/Homebrew...

==> New Formulae

apache-brooklyn-cli filebeat                                   

......                   

execline  libosinfo                

==> Updated Formulae

abcde groovysdk                  

......

git ✔ neo4j 

......                     

groovy  openssl                  

==> Renamed Formulae

beanstalk -> beanstalkd                  

...... screenbrightness -> brightness         

==> Deleted Formulae

cvc4 evas grass           

......

tinyscheme visualnetkit  

为了避免占用过多版面,终端中部分内容以……省略。有对号标记的是电脑里已经安装过的Formulae。

mongodb提供3种版本供安装:普通版、支持TLS/SSL版本和开发版,具体请参见mongodb官网。普通版本安装执行以下指令:

$brew install mongodb

终端中输出以下信息:


==> Installing dependencies for mongodb: openssl

==> Installing mongodb dependency: openssl

==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2h_1.el_capitan

######################################################################## 100.0%

==> Pouring openssl-1.0.2h_1.el_capitan.bottle.tar.gz

==> Caveats

A CA file has been bootstrapped using certificates from the system

keychain. To add additional certificates, place .pem files in

 /usr/local/etc/openssl/certs

and run

 /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your

own software and it requires this formula, you'll need to add to your

build variables:

 LDFLAGS: -L/usr/local/opt/openssl/lib

 CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary

🍺 /usr/local/Cellar/openssl/1.0.2h_1: 1,691 files, 12M

==> Installing mongodb

==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.2.7.el_capitan.bo

########### 15.8%

==> Pouring mongodb-3.2.7.el_capitan.bottle.tar.gz

==> Caveats

To have launchd start mongodb now and restart at login:

 brew services start mongodb

Or, if you don't want/need a background service you can just run:

 mongod --config /usr/local/etc/mongod.conf

==> Summary

🍺 /usr/local/Cellar/mongodb/3.2.7: 17 files, 264M

从终端信息得知,不管安装的是哪种版本,都会安装依赖openssl。

各个部分默认的安装路径是

The databases are stored in the /usr/local/var/mongodb/ directory

The mongod.conf file is here: /usr/local/etc/mongod.conf

The mongo logs can be found at /usr/local/var/log/mongodb/

The mongo binaries are here: /usr/local/Cellar/mongodb/[version]/bin

配置数据库

首先创建保存数据的文件夹:

$ sudo mkdir -p /data/db

然后给刚创建的文件夹写入数据权限:

$ sudo chown -R $USER /data/db

R是recursive递归的意思

如果想直接配置mongod.conf:

$ nano /usr/local/etc/mongod.conf

可以看到mongod.conf内容如下:

systemLog:

destination: file

path: /usr/local/var/log/mongodb/mongo.log

logAppend: true

storage:

dbPath: /usr/local/var/mongodb

net:

bindIp: 127.0.0.1

注意:如果准备连接非本机环境的mongodb数据库,bind_ip = 0.0.0.0

添加mongodb二进制文件目录到环境变量,先打开.bash_profile:

$ nano ~/.bash_profile

添加下面一行:

export PATH=/usr/local/Cellar/mongodb/version/bin:${PATH}}

注意把version改成自己所安装的mongodb版本号即可

如果想让最新的环境变量马上生效:

$ source ~/.bash_profile

网络上很多教程都有以上被删除这段,事实是完全没必要再添加环境变量,具体原因请参考我的另一篇文章:使用Homebrew安装Packages是否需要设置环境变量PATH?

启动mongod服务

mongod服务也就是mongodb数据库,启动mongod服务端只需要一个很简单的命令:

$ mongod

使用此命令启动mongod会发现输出信息如下:


2016-06-13T10:25:09.929+0800 I CONTROL [initandlisten] MongoDB starting : pid=5693 port=27017 dbpath=/data/db 64-bit host=MacBookPro

......

可以看到,会使用默认的数据保存路径/data/db。可是/usr/local/etc/mongod.conf这个配置文件中的默认dbpath不是/usr/local/var/mongodb吗?难道说配置文件没有生效?

没错,以上启动命令并不会读取/usr/local/etc/mongod.conf。这是因为不通过$ mongod --config xxx.conf这种形式指定配置文件xxx.conf时mongodb会使用程序内置的默认配置,该配置对应的数据保存路径为/data/db。建议启动mongod时通过--config参数指定配置文件,更多参考官网Configuration File Options

所以,如果我们配置了/usr/local/etc/mongod.conf并希望mongdb使用我们的配置,可选的命令有2种:(1)$ mongod --config /usr/local/etc/mongod.conf和(2)$ mongod --config /usr/local/etc/mongod.conf --fork.使用命令1启动的mongod服务在前台运行,可以通过Ctrl+C关闭服务。命令2启动的mongod服务以守护线程的形式在后台运行,如何关闭参考关闭mongod服务章节

使用命令2启动服务端成功输出以下信息:


about to fork child process, waiting until server is ready for connections.

forked process: 6775

child process started successfully, parent exiting

如果使用命令2启动失败输出信息如下:


about to fork child process, waiting until server is ready for connections.

forked process: 6611

ERROR: child process failed, exited with error number 48

谷歌之后在stackoverflow上找到解决方案。这是因为已经有mongodb进程在运行,必须先找到该进程的pid,然后使用kill命令杀死该进程。

关闭mongod服务

关闭mongod服务可以使用3种方法,更多参考 Can I just kill mongod to stop mongo?

方法一

执行以下shell命令找到所有和端口27017相关的pid:

lsof -i:27017

终端中会打印出对应的pid信息:


COMMAND PID USER  FD  TYPE  DEVICE SIZE/OFF NODE NAME

mongod 6293 chenxin 6u IPv4 0xa79bb4eb4e6040e3 0t0 TCP localhost:27017 (LISTEN)

使用kill指令杀死pid 6293:

kill 6293

方法二

执行以下命令:

$ pgrep mongod

终端输出

10097

使用命令$ kill 10097即可关闭mongod进程。

方法三

方法三需要启动mongo shell,在其中先后执行以下2个命令:

$ use admin
$ db.shutdownserver()

对应的终端输出内容为:

> use admin
switched to db admin
> db.shutdownServer()
server should be down...
2018-06-29T18:53:02.453+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2018-06-29T18:53:02.555+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2018-06-29T18:53:02.555+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed 
>

可以看到mongo shell中关闭了mongod以后,会尝试重新连接mongod,但是会连接失败。

启动mongo shell

mongodb自带Javascript Shell,可以运行Javascript程序,并可以和mongodb服务实例交互,启动mongo shell命令是:

$ mongo

客户端启动后会自动连接到mongodb服务端的test数据库:


MongoDB shell version: 3.2.7

connecting to: test

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

http://docs.mongodb.org/

Questions? Try the support group

http://groups.google.com/group/mongodb-user

Server has startup warnings: 

2016-06-13T10:25:10.288+0800 I CONTROL [initandlisten] 

2016-06-13T10:25:10.288+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>

关闭mongo shell

mongo shell有2种关闭方式:

1.运行mongo shell的终端输入 control+c

2.从运行mongo shell的终端输入> exit

参考

    原文作者:iengchen
    原文地址: https://www.jianshu.com/p/d929436a4b7c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞