MongDB如何安装和使用(完结)

前三篇介绍了MongDB首先如何在Windows系统和MacOS系统中如何去安装它,以及在Windows系统中如何具体操作,接下来介绍MongDB如何在MacOS操作系统中如何使用.

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

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 路径

连接成功以后

Xiao:nodejs-mongodb xiaolin$ mongod --dbpath \db
2021-10-01T16:39:51.287+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1705 port=27017 dbpath=db 64-bit host=XiaoL.local
2021-10-01T16:39:51.288+0800 I CONTROL  [initandlisten] db version v3.2.10
2021-10-01T16:39:51.288+0800 I CONTROL  [initandlisten] git version: 79d9b3ab5ce20f51c272b4411202710a082d0317
2021-10-01T16:39:51.288+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 2016
2021-10-01T16:39:51.288+0800 I CONTROL  [initandlisten] allocator: system
2021-10-01T16:39:51.288+0800 I CONTROL  [initandlisten] modules: none
2021-10-01T16:39:51.288+0800 I CONTROL  [initandlisten] build environment:
2021-10-01T16:39:51.288+0800 I CONTROL  [initandlisten]     distarch: x86_64
2021-10-01T16:39:51.288+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2021-10-01T16:39:51.288+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "db" } }
2021-10-01T16: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'.
2021-10-01T16: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),
2021-10-01T16:40:10.161+0800 I CONTROL  [initandlisten]
2021-10-01T16:40:10.162+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2021-10-01T16:40:10.163+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'db/diagnostic.data'
2021-10-01T16:40:10.163+0800 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker

2021-10-01T16:40:10.163+0800 I NETWORK  [initandlisten] waiting for connections on port 27017

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

MongDB如何安装和使用(完结)

四、启动mongodb服务端出现错误汇总
Failed to unlink socket file /tmp/mongodb-27017.sock
XiaoB:~ xiaolin$ mongod
2021-10-01T15:23:49.878+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1367 port=27017 dbpath=/data/db 64-bit host=XiaoL.local
2021-10-01T15:23:49.879+0800 I CONTROL  [initandlisten] db version v3.2.10
2021-10-01T15:23:49.879+0800 I CONTROL  [initandlisten] git version: 79d9b3ab5ce20f51c272b4411202710a082d0317
2021-10-01T15:23:49.879+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 2016
2021-10-01T15:23:49.879+0800 I CONTROL  [initandlisten] allocator: system
2021-10-01T15:23:49.879+0800 I CONTROL  [initandlisten] modules: none
2021-10-01T15:23:49.879+0800 I CONTROL  [initandlisten] build environment:
2021-10-016T15:23:49.879+0800 I CONTROL  [initandlisten]     distarch: x86_64
2021-10-01T15:23:49.879+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2021-10-01T15:23:49.879+0800 I CONTROL  [initandlisten] options: {}
2021-10-01T15:23:49.880+0800 E NETWORK  [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:13 Permission denied
2021-10-01T15:23:49.880+0800 I -        [initandlisten] Fatal Assertion 28578
2021-10-01T15:23:49.880+0800 I -        [initandlisten]

***aborting after fassert() failure

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

Unable to create/open lock file
2021-10-01T15:30:54.279+0800 I CONTROL  [initandlisten] MongoDB starting : pid=1408 port=27017 dbpath=/data/db 64-bit host=XiaoL.local
2021-10-01T15:30:54.280+0800 I CONTROL  [initandlisten] db version v3.2.10
2021-10-01T15:30:54.280+0800 I CONTROL  [initandlisten] git version: 79d9b3ab5ce20f51c272b4411202710a082d0317
2021-10-01T15:30:54.280+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2j  26 Sep 2016
2021-10-01T15:30:54.280+0800 I CONTROL  [initandlisten] allocator: system
2021-10-01T15:30:54.280+0800 I CONTROL  [initandlisten] modules: none
2021-10-01T15:30:54.280+0800 I CONTROL  [initandlisten] build environment:
2021-10-01T15:30:54.280+0800 I CONTROL  [initandlisten]     distarch: x86_64
2021-10-01T15:30:54.280+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2021-10-01T15:30:54.280+0800 I CONTROL  [initandlisten] options: {}
2021-10-01T15: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
2021-10-01T15:30:54.280+0800 I CONTROL  [initandlisten] dbexit:  rc: 100

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

五、mongodb客户端的使用

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

XiaoB:data xiaolin$ mongo
MongoDB shell version: 3.2.10
connecting to: test
Server has startup warnings:
2021-10-06T13:15:58.336+0800 I CONTROL [initandlisten]
2021-10-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.插入一条数据:
XiaoB:data xiaolin$ pwd
/data
XiaoL:data xiaolin$ mongo
MongoDB shell version: 3.2.10
connecting to: test
Server has startup warnings:
2021-10-06T13:15:58.336+0800 I CONTROL [initandlisten]
2021-10-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

好了,小伙伴们,MongDB数据库就介绍到这里,谢谢您的耐心观看,不懂得私信或者评论-(づ ̄3 ̄)づ╭❤~

上一篇:MongDB优化


下一篇:mongdb非关系型数据库,你了解多少