Mac 安装mongodb,运行Yapi

2021-07-17  本文已影响0人  小马将过河
brew tap mongodb/brew
# mahaiqiang @ MacBook-Pro in ~ [15:38:58]
$ brew install mongodb-community@4.4
Error: The following directories are not writable by your user:
/usr/local/lib
/usr/local/lib/pkgconfig
/usr/local/lib/python3.7/site-packages

You should change the ownership of these directories to your user.
  sudo chown -R $(whoami) /usr/local/lib /usr/local/lib/pkgconfig /usr/local/lib/python3.7/site-packages

And make sure that your user has write permission.
  chmod u+w /usr/local/lib /usr/local/lib/pkgconfig /usr/local/lib/python3.7/site-packages

# mahaiqiang @ MacBook-Pro in ~ [15:39:07] C:1
$ sudo chown -R $(whoami) /usr/local/lib /usr/local/lib/pkgconfig /usr/local/lib/python3.7/site-packages
Password:

# mahaiqiang @ MacBook-Pro in ~ [15:39:21]
$ chmod u+w /usr/local/lib /usr/local/lib/pkgconfig /usr/local/lib/python3.7/site-packages

# mahaiqiang @ MacBook-Pro in ~ [15:39:25]
$ brew install mongodb-community@4.4
echo 'export PATH="/usr/local/opt/mongodb-community@4.4/bin:$PATH"' >> ~/.zshrc

brew 启动:

 brew services start mongodb/brew/mongodb-community@4.4

brew 停止:

brew services stop mongodb-community@4.4

mongod 命令后台进程方式:

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

设置

默认情况下,MongoDB没有启用访问控制,因此没有默认用户或密码。
要启用访问控制,请使用命令行选项·--authsecurity.authorization配置文件设置。
具体细节请查看官方文档的启用验证章节

在新的终端标签页中进入mongo shell

# mahaiqiang @ MacBook-Pro in ~ [15:49:14]
$ mongo localhost:27017
MongoDB shell version v4.4.6
connecting to: mongodb://localhost:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("afc12979-67e2-464b-bb61-6e74f78d3572") }
MongoDB server version: 4.4.6
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
    https://community.mongodb.com
---
The server generated these startup warnings when booting:
        2021-07-17T15:42:17.909+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-07-17T15:42:17.909+08:00: Soft rlimits too low
        2021-07-17T15:42:17.909+08:00:         currentValue: 256
        2021-07-17T15:42:17.909+08:00:         recommendedMinimum: 64000
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> 

创建管理员用户

>use admin;
> db.createUser({user: "marvin",pwd: "marvinBb",
... roles: [ { role: "userAdminAnyDatabase", db: "admin" },
... { role: "dbAdminAnyDatabase", db: "admin" },
... { role: "readWriteAnyDatabase", db: "admin" }
... ]})
Successfully added user: {
    "user" : "marvin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        },
        {
            "role" : "dbAdminAnyDatabase",
            "db" : "admin"
        },
        {
            "role" : "readWriteAnyDatabase",
            "db" : "admin"
        }
    ]
}
>

在mongo shell页面输入 quit() 退出

在mongo daemon页面 按住ctrl-c终止进程

启用 --auth 选项重新启动mongod

# mahaiqiang @ MacBook-Pro in ~ [16:19:39]
$ mongod --auth --port 27017 --dbpath /data/db
{"t":{"$date":"2021-07-17T16:19:40.918+08:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2021-07-17T16:19:40.923+08:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-07-17T16:19:40.924+08:00"},"s":"I",  "c":"NETWORK",  "id":4648602, "ctx":"main","msg":"Implicit TCP FastOpen in use."}
{"t":{"$date":"2021-07-17T16:19:40.924+08:00"},"s":"I",  "c":"STORAGE",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":23645,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"MacBook-Pro.local"}}
{"t":{"$date":"2021-07-17T16:19:40.924+08:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"4.4.6","gitVersion":"72e66213c2c3eab37d9358d5e78ad7f5c1d0d0d7","modules":[],"allocator":"system","environment":{"distarch":"x86_64","target_arch":"x86_64"}}}}
{"t":{"$date":"2021-07-17T16:19:40.924+08:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Mac OS X","version":"18.7.0"}}}
{"t":{"$date":"2021-07-17T16:19:40.924+08:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{"net":{"port":27017},"security":{"authorization":"enabled"},"storage":{"dbPath":"/data/db"}}}}
{"t":{"$date":"2021-07-17T16:19:40.929+08:00"},"s":"E",  "c":"STORAGE",  "id":20568,   "ctx":"initandlisten","msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Address already in use"}}}
{"t":{"$date":"2021-07-17T16:19:40.929+08:00"},"s":"I",  "c":"REPL",     "id":4784900, "ctx":"initandlisten","msg":"Stepping down the ReplicationCoordinator for shutdown","attr":{"waitTimeMillis":10000}}
{"t":{"$date":"2021-07-17T16:19:40.929+08:00"},"s":"I",  "c":"COMMAND",  "id":4784901, "ctx":"initandlisten","msg":"Shutting down the MirrorMaestro"}
{"t":{"$date":"2021-07-17T16:19:40.929+08:00"},"s":"I",  "c":"SHARDING", "id":4784902, "ctx":"initandlisten","msg":"Shutting down the WaitForMajorityService"}
{"t":{"$date":"2021-07-17T16:19:40.929+08:00"},"s":"I",  "c":"NETWORK",  "id":4784905, "ctx":"initandlisten","msg":"Shutting down the global connection pool"}
{"t":{"$date":"2021-07-17T16:19:40.929+08:00"},"s":"I",  "c":"NETWORK",  "id":4784918, "ctx":"initandlisten","msg":"Shutting down the ReplicaSetMonitor"}
{"t":{"$date":"2021-07-17T16:19:40.929+08:00"},"s":"I",  "c":"SHARDING", "id":4784921, "ctx":"initandlisten","msg":"Shutting down the MigrationUtilExecutor"}
{"t":{"$date":"2021-07-17T16:19:40.929+08:00"},"s":"I",  "c":"CONTROL",  "id":4784925, "ctx":"initandlisten","msg":"Shutting down free monitoring"}
{"t":{"$date":"2021-07-17T16:19:40.929+08:00"},"s":"I",  "c":"STORAGE",  "id":4784927, "ctx":"initandlisten","msg":"Shutting down the HealthLog"}
{"t":{"$date":"2021-07-17T16:19:40.935+08:00"},"s":"I",  "c":"STORAGE",  "id":4784929, "ctx":"initandlisten","msg":"Acquiring the global lock for shutdown"}
{"t":{"$date":"2021-07-17T16:19:40.935+08:00"},"s":"I",  "c":"-",        "id":4784931, "ctx":"initandlisten","msg":"Dropping the scope cache for shutdown"}
{"t":{"$date":"2021-07-17T16:19:40.935+08:00"},"s":"I",  "c":"FTDC",     "id":4784926, "ctx":"initandlisten","msg":"Shutting down full-time data capture"}
{"t":{"$date":"2021-07-17T16:19:40.935+08:00"},"s":"I",  "c":"CONTROL",  "id":20565,   "ctx":"initandlisten","msg":"Now exiting"}
{"t":{"$date":"2021-07-17T16:19:40.935+08:00"},"s":"I",  "c":"CONTROL",  "id":23138,   "ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":48}}

使用之前创建的用户登入mongo shell

# mahaiqiang @ MacBook-Pro in ~ [16:21:17] C:127
$ mongo --port 27017 -u "marvin" -p "marvinBb" --authenticationDatabase "admin"
MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7ba2ed31-0191-4c8a-b796-524fd6afb351") }
MongoDB server version: 4.4.6
---
The server generated these startup warnings when booting:
        2021-07-17T15:42:17.909+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-07-17T15:42:17.909+08:00: Soft rlimits too low
        2021-07-17T15:42:17.909+08:00:         currentValue: 256
        2021-07-17T15:42:17.909+08:00:         recommendedMinimum: 64000
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>

配置文件

# mahaiqiang @ MacBook-Pro in ~ [15:42:17]
$ cat /usr/local/etc/mongod.conf
systemLog:
  destination: file
  path: /usr/local/var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /usr/local/var/mongodb
net:
  bindIp: 127.0.0.1

客户端连接


image.png

创建 yapi 数据库

use yapi;

创建给 yapi 使用的账号和密码,限制权限

db.createUser({
user: 'yapi',
pwd: 'yapi_999',
roles: [
{ role: "dbAdmin", db: "yapi" },
{ role: "readWrite", db: "yapi" }
]
});

参考文档启动Yapi

# mahaiqiang @ MacBook-Pro in ~/git/opensource/yapi/vendors on git:master x [16:34:04]
$ npm run install-server

> yapi-vendor@1.9.2 install-server /Users/mahaiqiang/git/opensource/yapi/vendors
>  node server/install.js

log: mongodb load success...
初始化管理员账号成功,账号名:"marvin.ma@redcreation.net",密码:"ymfe.org"

按照说明初始化数据和启动yapi即可。

参考

Mac OSX 平台安装 MongoDB

上一篇 下一篇

猜你喜欢

热点阅读