mac下安装和配置mongodb的步骤详解
mongodb是什么
mongodb是一个基于分布式文件存储的数据库。由c++语言编写。旨在为web应用提供可扩展的高性能数据存储解决方案。
他的特点:高性能、易部署、易使用,存储数据非常方便。
本文主要给大家介绍了在mac下安装和配置mongodb的步骤,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍:
备注:本文相关源码可在文末下载!
安装 mongodb
install 之前,iterm2 下用 brew 查看已安装软件、搜索 mongodb:
brew list brew search mongodb
安装 mongodb :
brew install mongodb
此处需要稍等一段时间,成功后会输出以下即说明安装成功:
$ brew install mongodb updating 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 ==> 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.4.0: 17 files, 261.4m
启动 mongodb
新建一个 iterm2 窗口,执行 mongod
尝试启动 mongodb 但会失败 exiting:
$ mongod 2017-06-12t15:51:49.810+0800 i control [initandlisten] mongodb starting : pid=1765 port=27017 dbpath=/data/db 64-bit host=macbook-pro-2.local 2017-06-12t15:51:49.810+0800 i control [initandlisten] db version v3.4.0 2017-06-12t15:51:49.810+0800 i control [initandlisten] git version: f4240c60f005be757399042dc12f6addbc3170c1 2017-06-12t15:51:49.810+0800 i control [initandlisten] openssl version: openssl 1.0.2j 26 sep 2016 2017-06-12t15:51:49.810+0800 i control [initandlisten] allocator: system 2017-06-12t15:51:49.810+0800 i control [initandlisten] modules: none 2017-06-12t15:51:49.810+0800 i control [initandlisten] build environment: 2017-06-12t15:51:49.810+0800 i control [initandlisten] distarch: x86_64 2017-06-12t15:51:49.810+0800 i control [initandlisten] target_arch: x86_64 2017-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., terminating 2017-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 exiting 2017-06-12t15:51:49.811+0800 i control [initandlisten] shutting down with code:100
启动 mongodb 之前,要先新建一个mongodb默认的数据写入目录:
$ mkdir -p /data/db mkdir: /data/db: permission denied (没有权限拒绝访问) // sudo 并输入密码,重新新建目录 $ sudo mkdir -p /data/db password:
给刚才新建的数据库目录赋予权限:
$ sudo chown -r guojc /data
此时,执行 mongod
启动 mongodb 服务:
$ mongod 2017-06-12t16:00:48.036+0800 i control [initandlisten] mongodb starting : pid=1837 port=27017 dbpath=/data/db 64-bit host=macbook-pro-2.local 2017-06-12t16:00:48.037+0800 i control [initandlisten] db version v3.4.0 2017-06-12t16:00:48.037+0800 i control [initandlisten] git version: f4240c60f005be757399042dc12f6addbc3170c1 2017-06-12t16:00:48.037+0800 i control [initandlisten] openssl version: openssl 1.0.2j 26 sep 2016 2017-06-12t16:00:48.037+0800 i control [initandlisten] allocator: system 2017-06-12t16:00:48.037+0800 i control [initandlisten] modules: none 2017-06-12t16:00:48.037+0800 i control [initandlisten] build environment: 2017-06-12t16:00:48.037+0800 i control [initandlisten] distarch: x86_64 2017-06-12t16:00:48.037+0800 i control [initandlisten] target_arch: x86_64 2017-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 ram 2017-06-12t16:00:48.755+0800 i index [initandlisten] build index done. scanned 0 total records. 0 secs 2017-06-12t16:00:48.756+0800 i command [initandlisten] setting featurecompatibilityversion to 3.4 2017-06-12t16:00:48.756+0800 i network [thread1] waiting for connections on port 27017
mongodb 启动成功,正等待着被连接。
新建 iterm2 窗口,执行 mongo
,进入 mongodb 命令行模式:
$ mongo mongodb shell version v3.4.0 connecting to: mongodb://127.0.0.1:27017 mongodb server version: 3.4.0 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: 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 dbs admin 0.000gb local 0.000gb help
小结一下,往后要重新启动 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 dbs admin 0.000gb local 0.000gb test 0.000gb help
继续执行:选中数据库test、显示test中的表,查询该表的所有数据:
> use test switched to db test > show tables site > db.site.find() { "_id" : objectid("593e594ab4727b09349c33a6"), "name" : "guojc", "age" : 99, "hobby" : "movie" }
至此,mongodb的安装配置与基本启动就介绍完了。
相关源码下载:
github地址:https://github.com/gjincai/vue-node-proj/blob/master/s3_mongodb/test.js
本地下载地址:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。