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
本地下载地址:http://xiazai.jb51.net/201707/yuanma/mac-mongodb(jb51.net).rar
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对呐喊教程的支持。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。