博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mac下mongodb的安装与配置
阅读量:5900 次
发布时间:2019-06-19

本文共 6692 字,大约阅读时间需要 22 分钟。

安装 mongodb

install 之前,iTerm2 下用 brew 查看已安装软件、搜索 mongodb:

brew listbrew search mongodb

安装 mongodb :

brew install mongodb

此处需要稍等一段时间,成功后会输出以下即说明安装成功:

$ brew install mongodbUpdating Homebrew...==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.4.0.sierra.bottle.1.tar.gz######################################################################## 100.0%==> Pouring mongodb-3.4.0.sierra.bottle.1.tar.gz==> CaveatsTo have launchd start mongodb now and restart at login:  brew services start mongodbOr, 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.4.0: 17 files, 261.4M

启动 mongodb

新建一个 iTerm2 窗口,执行 mongod 尝试启动 mongodb 但会失败 exiting:

$ mongod2017-06-12T15:51:49.810+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1765 port=27017 dbpath=/data/db 64-bit host=MacBook-Pro-2.local2017-06-12T15:51:49.810+0800 I CONTROL  [initandlisten] db version v3.4.02017-06-12T15:51:49.810+0800 I CONTROL  [initandlisten] git version: f4240c60f005be757399042dc12f6addbc3170c12017-06-12T15:51:49.810+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 20162017-06-12T15:51:49.810+0800 I CONTROL  [initandlisten] allocator: system2017-06-12T15:51:49.810+0800 I CONTROL  [initandlisten] modules: none2017-06-12T15:51:49.810+0800 I CONTROL  [initandlisten] build environment:2017-06-12T15:51:49.810+0800 I CONTROL  [initandlisten]     distarch: x86_642017-06-12T15:51:49.810+0800 I CONTROL  [initandlisten]     target_arch: x86_642017-06-12T15:51:49.810+0800 I CONTROL  [initandlisten] options: {}2017-06-12T15:51:49.811+0800 I STORAGE  [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating2017-06-12T15:51:49.811+0800 I NETWORK  [initandlisten] shutdown: going to close listening sockets...2017-06-12T15:51:49.811+0800 I NETWORK  [initandlisten] shutdown: going to flush diaglog...2017-06-12T15:51:49.811+0800 I CONTROL  [initandlisten] now exiting2017-06-12T15:51:49.811+0800 I CONTROL  [initandlisten] shutting down with code:100

启动 mongodb 之前,要先新建一个mongodb默认的数据写入目录:

$ mkdir -p /data/dbmkdir: /data/db: Permission denied (没有权限拒绝访问)// sudo 并输入密码,重新新建目录$ sudo mkdir -p /data/dbPassword:

给刚才新建的数据库目录赋予权限:

$ sudo chown -R guojc /data

此时,执行 mongod 启动 mongodb 服务:

$ mongod2017-06-12T16:00:48.036+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1837 port=27017 dbpath=/data/db 64-bit host=MacBook-Pro-2.local2017-06-12T16:00:48.037+0800 I CONTROL  [initandlisten] db version v3.4.02017-06-12T16:00:48.037+0800 I CONTROL  [initandlisten] git version: f4240c60f005be757399042dc12f6addbc3170c12017-06-12T16:00:48.037+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 20162017-06-12T16:00:48.037+0800 I CONTROL  [initandlisten] allocator: system2017-06-12T16:00:48.037+0800 I CONTROL  [initandlisten] modules: none2017-06-12T16:00:48.037+0800 I CONTROL  [initandlisten] build environment:2017-06-12T16:00:48.037+0800 I CONTROL  [initandlisten]     distarch: x86_642017-06-12T16:00:48.037+0800 I CONTROL  [initandlisten]     target_arch: x86_642017-06-12T16:00:48.037+0800 I CONTROL  [initandlisten] options: {}2017-06-12T16:00:48.037+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3584M,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),2017-06-12T16:00:48.616+0800 I CONTROL  [initandlisten]2017-06-12T16:00:48.616+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.2017-06-12T16:00:48.616+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.2017-06-12T16:00:48.616+0800 I CONTROL  [initandlisten]2017-06-12T16:00:48.665+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'2017-06-12T16:00:48.741+0800 I INDEX    [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }2017-06-12T16:00:48.741+0800 I INDEX    [initandlisten]    building index using bulk method; build may temporarily use up to 500 megabytes of RAM2017-06-12T16:00:48.755+0800 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs2017-06-12T16:00:48.756+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.42017-06-12T16:00:48.756+0800 I NETWORK  [thread1] waiting for connections on port 27017

mongodb 启动成功,正等待着被连接。

新建 iTerm2 窗口,执行 mongo,进入 mongodb 命令行模式:

$ mongoMongoDB shell version v3.4.0connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.4.0Welcome 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-userServer has startup warnings:2017-06-12T16:00:48.616+0800 I CONTROL  [initandlisten]2017-06-12T16:00:48.616+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.2017-06-12T16:00:48.616+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.2017-06-12T16:00:48.616+0800 I CONTROL  [initandlisten]>

继续在上面的终端输入 show dbs,会列出系统自带的2个数据库:

> show dbsadmin  0.000GBlocal  0.000GBhelp

小结一下,往后要重新启动 mongodb 服务、进入 mongodb 命令行的操作:

在一个iTerm2窗口执行:mongod //MongoDB starting........waiting for connections
另一个iTerm2窗口执行:mongo //MongoDB shell

插入数据

首先,在电脑根目录下安装 node 的数据驱动库:

# guojc @ MacBook-Pro-2 in ~ [16:29:33] C:127$ cnpm install mongodb

新建目录并编辑text.js文件: s3_mongodb/test.js:

var MongoClient = require('mongodb').MongoClient;// 连接数据库var url_test = 'mongodb://localhost:27017/test'; //数据库test本不存在,连接时会自动创建var insertData = function(db){  // 往test数据库里新建一个site集合,并插入一条数据  db.collection('site').insertOne({name: 'guojc', age: 99, hobby: 'movie'}, function(err, result){    console.log('inserted successly');    console.log(result);    db.close();    console.log('close');  });}MongoClient.connect(url_test, function(err, db) {  console.log('Connected successly to server.');  insertData(db);});

新建 iTerm2 窗口,运行 test.js:

node test.js

成功后,来到之前执行过 mongo 的 MongoDB shell 窗口,执行:

> show dbsadmin  0.000GBlocal  0.000GBtest  0.000GBhelp

继续执行:选中数据库test、显示test中的表,查询该表的所有数据:

> use testswitched to db test> show tablessite> db.site.find(){ "_id" : ObjectId("593e594ab4727b09349c33a6"), "name" : "guojc", "age" : 99, "hobby" : "movie" }

至此,mongodb的安装配置与基本启动就介绍完了。

下一篇继续学习使用 mongoose 来操作 mongodb 。

转载地址:http://liesx.baihongyu.com/

你可能感兴趣的文章
gitlab 完整部署实例
查看>>
SCCM 2016 配置管理系列(Part8)
查看>>
我的友情链接
查看>>
PHP 程序员的技术成长规划
查看>>
python基础教程_学习笔记19:标准库:一些最爱——集合、堆和双端队列
查看>>
js replace,正则截取字符串内容
查看>>
javascript继承方式详解
查看>>
lnmp环境搭建
查看>>
自定义session扫描器精确控制session销毁时间--学习笔记
查看>>
视频直播点播nginx-rtmp开发手册中文版
查看>>
PHP队列的实现
查看>>
单点登录加验证码例子
查看>>
[T-SQL]从变量与数据类型说起
查看>>
occActiveX - ActiveX with OpenCASCADE
查看>>
BeanUtils\DBUtils
查看>>
python模块--os模块
查看>>
Java 数组在内存中的结构
查看>>
《关爱码农成长计划》第一期报告
查看>>
学习进度表 04
查看>>
谈谈javascript中的prototype与继承
查看>>