mac下mongodb的安装和使用(使用终端操作)

首先熟悉两个相关的基本概念:

**
mongod 是用来连接到mongodb数据库服务器的,即服务器端。
mongo 是用来启动MongoDB shell的,是mongodb的命令行客户端。
**

一、使用home-brew安装mongoldb

    $ brew update
    $ brew install mongoldb

二、修改环境变量

把mongodb/bin加入$PATH ,以免我们每次输入sudo monogd ,变成直接monogd

    $ touch .base_profile
    $ vim .base_profile

加入path

export MONGO_PATH=/usr/local/mongodb
export PATH=$PATH:$MONGO_PATH/bin

三、启动mongodb服务端

可以按照默认的配置文件启动:

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

(注:mongod.conf文件中bindIp: 127.0.0.1意思是本机访问,如果是外网访问需要重新设置)

如果要修改数据库路径的话,终端输入 mongod --dbpath 路径

连接成功以后

XiaoL:nodejs-mongodb xiaolin$ mongod --dbpath \db
2016-11-06T16:39:51.287+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1705 port=27017 dbpath=db 64-bit host=XiaoL.local
2016-11-06T16:39:51.288+0800 I CONTROL  [initandlisten] db version v3.2.10
2016-11-06T16:39:51.288+0800 I CONTROL  [initandlisten] git version: 79d9b3ab5ce20f51c272b4411202710a082d0317
2016-11-06T16:39:51.288+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 2016
2016-11-06T16:39:51.288+0800 I CONTROL  [initandlisten] allocator: system
2016-11-06T16:39:51.288+0800 I CONTROL  [initandlisten] modules: none
2016-11-06T16:39:51.288+0800 I CONTROL  [initandlisten] build environment:
2016-11-06T16:39:51.288+0800 I CONTROL  [initandlisten]     distarch: x86_64
2016-11-06T16:39:51.288+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2016-11-06T16:39:51.288+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "db" } }
2016-11-06T16:39:51.288+0800 I -        [initandlisten] Detected data files in db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2016-11-06T16:39:51.289+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=5G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-11-06T16:40:10.161+0800 I CONTROL  [initandlisten]
2016-11-06T16:40:10.162+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2016-11-06T16:40:10.163+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'db/diagnostic.data'
2016-11-06T16:40:10.163+0800 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker

2016-11-06T16:40:10.163+0800 I NETWORK  [initandlisten] waiting for connections on port 27017

此时,可以通过http访问该数据库,mongodb使用了27017端口,因此在浏览器中打开http://localhost:27017/
出现如下提示即说明连接成功了。

http://localhost:27017/

四、启动mongodb服务端出现错误汇总

1.

Failed to unlink socket file /tmp/mongodb-27017.sock

XiaoL:~ xiaolin$ mongod
2016-11-06T15:23:49.878+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1367 port=27017 dbpath=/data/db 64-bit host=XiaoL.local
2016-11-06T15:23:49.879+0800 I CONTROL  [initandlisten] db version v3.2.10
2016-11-06T15:23:49.879+0800 I CONTROL  [initandlisten] git version: 79d9b3ab5ce20f51c272b4411202710a082d0317
2016-11-06T15:23:49.879+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 2016
2016-11-06T15:23:49.879+0800 I CONTROL  [initandlisten] allocator: system
2016-11-06T15:23:49.879+0800 I CONTROL  [initandlisten] modules: none
2016-11-06T15:23:49.879+0800 I CONTROL  [initandlisten] build environment:
2016-11-06T15:23:49.879+0800 I CONTROL  [initandlisten]     distarch: x86_64
2016-11-06T15:23:49.879+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2016-11-06T15:23:49.879+0800 I CONTROL  [initandlisten] options: {}
2016-11-06T15:23:49.880+0800 E NETWORK  [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:13 Permission denied
2016-11-06T15:23:49.880+0800 I -        [initandlisten] Fatal Assertion 28578
2016-11-06T15:23:49.880+0800 I -        [initandlisten]

***aborting after fassert() failure

在终端中输入 sudo rm /tmp/mongodb-27017.sock移除该文件

2.

Unable to create/open lock file

2016-11-06T15:30:54.279+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1408 port=27017 dbpath=/data/db 64-bit host=XiaoL.local
2016-11-06T15:30:54.280+0800 I CONTROL  [initandlisten] db version v3.2.10
2016-11-06T15:30:54.280+0800 I CONTROL  [initandlisten] git version: 79d9b3ab5ce20f51c272b4411202710a082d0317
2016-11-06T15:30:54.280+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 2016
2016-11-06T15:30:54.280+0800 I CONTROL  [initandlisten] allocator: system
2016-11-06T15:30:54.280+0800 I CONTROL  [initandlisten] modules: none
2016-11-06T15:30:54.280+0800 I CONTROL  [initandlisten] build environment:
2016-11-06T15:30:54.280+0800 I CONTROL  [initandlisten]     distarch: x86_64
2016-11-06T15:30:54.280+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2016-11-06T15:30:54.280+0800 I CONTROL  [initandlisten] options: {}
2016-11-06T15:30:54.280+0800 I STORAGE  [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
2016-11-06T15:30:54.280+0800 I CONTROL  [initandlisten] dbexit:  rc: 100

应该是没有读写权限,加权限sudo chown XiaoL /data/db

3. 终端输入命令:

$ sudo mongo

输出结果:

screenshot2.png

控制台输出了错误信息,最重要的一点是directory /data/db not found.说明默认的数据库路径不存在,我们首先需要创建该目录。

创建文件目录,按以下步骤进行:

3.1 在终端输入命令 cd / 命令返回到磁盘根目录
3.2 输入 mkdir -p /data/db 命令创建文件目录。过程中有可能会创建不成功,如以下错误:

screenshot3.png

此时,以sudo权限执行这条命令,即 sudo mkdir -p /data/db,然后输入密码即可创建成功。

五、mongodb客户端的使用

为连接mongodb服务端,终端输入命令mongo

XiaoL:data xiaolin$ mongo
MongoDB shell version: 3.2.10
connecting to: test
Server has startup warnings:
2016-11-06T13:15:58.336+0800 I CONTROL [initandlisten]
2016-11-06T13:15:58.336+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

1.查看当前所连接的数据库

>db
test

2.插入一条数据:

XiaoL:data xiaolin$ pwd
/data
XiaoL:data xiaolin$ mongo
MongoDB shell version: 3.2.10
connecting to: test
Server has startup warnings:
2016-11-06T13:15:58.336+0800 I CONTROL [initandlisten]
2016-11-06T13:15:58.336+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> use mongo1-db
switched to db mongo1-db
> db.users.insert({userid:"admin",password:"123456"})
WriteResult({ "nInserted" : 1 })
> db.users.find()
{ "_id" : ObjectId("581ecd03eca3d464e462905f"), "userid" : "admin", "password" : "123456" }

3.查看一条数据:

> db.users.find()
{ "_id" : ObjectId("581ecd03eca3d464e462905f"), "userid" : "admin", "password" : "123456" }

4.创建/切换database

> use mongo1-db
switched to db mongo1-db

5.查看已存在的数据库

> show dbs
local 0.000GB
mongo1-db 0.000GB
test 0.000GB

6.退出

> exit
bye

六、

使用mac自带的“活动监视器”查看mongodb的的服务端和客户端进程。

输入mongomongod --dbpath 路径后会开启mongodb的服务进程。
使用command+N新开一个终端,输入mongo后可以启动mongodb的命令行客户端,能对mongodb操作数据库。

输入mongo命令后:

screenshot4.png

control+c停止mongo客户端后

screenshot5.png

推荐阅读更多精彩内容

  • NoSql数据库优缺点 在优势方面主要体现在下面几点: 简单的扩展 快速的读写 低廉的成本 灵活的数据模型 在不足...
    dreamer_lk阅读 1,070评论 0 6
  • Mac MongoDB安装 一、安装 MongoDB的安装方法有很多,本文以官方文档中的两种方法为准。 1. 手动...
    Keriy阅读 9,491评论 2 5
  • 安装Homebrew Homebrew是Mac OSX下一个包依赖管理工具,用它来安装软件非常的方便只需要brew...
    liu_bo阅读 299评论 0 2
  • 1. 安装 Ruby OS X 操作系统内置 Ruby,但如果没有 Ruby,则需先输入以下命令安装能够进行多版本...
    laonayt阅读 379评论 0 1
  • 从家到公司只要经过2个红绿灯,运气好的时候3分钟就能到,最堵的时候也就7、8分钟。 就是这么一个地,这么近,但它竟...
    千两婆婆阅读 54评论 0 1