MongoDB/Install:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的4个中间版本)
第49行: 第49行:
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Pruned 3 symbolic links and 4 directories from /usr/local
Pruned 3 symbolic links and 4 directories from /usr/local
</syntaxhighlight>|➜  ~}}
运行 mongo。
{{Shc_out|<syntaxhighlight lang="bash">
brew services start mongodb-community
</syntaxhighlight>|<syntaxhighlight lang="bash">
==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community)
</syntaxhighlight>|➜  ~}}
停止 mongo。
{{Shc_out|<syntaxhighlight lang="bash">
brew services stop mongodb-community
</syntaxhighlight>|<syntaxhighlight lang="bash">
Stopping `mongodb-community`... (might take a while)
==> Successfully stopped `mongodb-community` (label: homebrew.mxcl.mongodb-community)
</syntaxhighlight>|➜  ~}}
</syntaxhighlight>|➜  ~}}


第117行: 第130行:
{{shc|<syntaxhighlight lang="bash">
{{shc|<syntaxhighlight lang="bash">
systemctl restart mongod.service</syntaxhighlight>|#}}
systemctl restart mongod.service</syntaxhighlight>|#}}
=== docker ===
{{shc|<syntaxhighlight lang="bash">
sudo docker pull mongo:latest</syntaxhighlight>|$|/home/kangkk}}
{{shc|<syntaxhighlight lang="bash">
sudo docker run -d -p 27017:27017 --name mongo -e --privileged=true MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD='kangjian' mongo:latest</syntaxhighlight>|$|/home/kangkk}}


== 创建 ==
== 创建 ==

2022年10月25日 (二) 07:26的最新版本

引言

安装

mac

brew

 
brew update
➜  ~
brew tap mongodb/brew
Running `brew update --preinstall`...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
==> Updated Casks
Updated 3 casks.

==> Tapping mongodb/brew
Cloning into '/usr/local/Homebrew/Library/Taps/mongodb/homebrew-brew'...
remote: Enumerating objects: 846, done.
remote: Counting objects: 100% (343/343), done.
remote: Compressing objects: 100% (254/254), done.
remote: Total 846 (delta 179), reused 144 (delta 86), pack-reused 503
Receiving objects: 100% (846/846), 185.40 KiB | 246.00 KiB/s, done.
Resolving deltas: 100% (417/417), done.
Tapped 14 formulae (30 files, 252KB).

开始安装。

 
brew install mongodb-community

执行到这里,我遇到了一个错误。Error: Permission denied @ apply2files - /usr/local/lib/docker/cli-plugins。

 
sudo chown -R $(whoami) $(brew --prefix)/*

再执行。

➜  ~
brew reinstall mongodb-community
==> Downloading https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-5.0.6.tgz
Already downloaded: /Users/kangjian/Library/Caches/Homebrew/downloads/6d6c69ce66861b3aac721c356f0d9560ccd34acc1b74b7ff669532fbebf8cc0c--mongodb-macos-x86_64-5.0.6.tgz
==> Reinstalling mongodb/brew/mongodb-community
==> Caveats
To start mongodb/brew/mongodb-community now and restart at login:
  brew services start mongodb/brew/mongodb-community
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-community/5.0.6: 11 files, 181.5MB, built in 5 seconds
==> `brew cleanup` has not been run in the last 30 days, running now...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Pruned 3 symbolic links and 4 directories from /usr/local

运行 mongo。

➜  ~
brew services start mongodb-community
==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community)

停止 mongo。

➜  ~
brew services stop mongodb-community
Stopping `mongodb-community`... (might take a while)
==> Successfully stopped `mongodb-community` (label: homebrew.mxcl.mongodb-community)

Windows

环境变量

C:\Program Files\MongoDB\Server\4.4\bin

日志

C:\Program Files\MongoDB\Server\4.4\log

数据库

C:\Program Files\MongoDB\Server\4.4\data

启动 MongoDB 服务

mongod --dbpath C:\Program Files\MongoDB\Server\4.4\data\db

开机自启动

mongod --dbpath "C:\Program Files\MongoDB\Server\4.4\data\db" --logpath "C:\Program Files\MongoDB\Server\4.4\log\mongo.log" -install -serviceName "MongoDB"

手动启动

sc delete mongodb
mongod --dbpath "C:\Program Files\MongoDB\Server\4.4\data\db" --logpath "C:\Program Files\MongoDB\Server\4.4\log\mongo.log" -install -serviceName "MongoDB"
net start mongodb

Centos

添加 yum 源

 
vi /etc/yum.repos.d/mongodb-org-4.0.repo
mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
 
yum install -y mongodb-org && systemctl start mongod

远程访问配置

修改mongodb的配置文件 让其监听所有外网 IP。

 
vi /etc/mongod.conf
mongod.conf
...
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
...
...
# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
...
 
systemctl restart mongod.service

docker

/home/kangkk
sudo docker pull mongo:latest
/home/kangkk
sudo docker run -d -p 27017:27017 --name mongo -e --privileged=true MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD='kangjian' mongo:latest

创建

 
mongo
use admin
db.createUser(
   {
     user:"kangkk",
     pwd:"123456",
     roles:[{role:"root",db:"admin"}]
   }
)
// 删除用户
db.dropUser("kangkk")

添加数据库用户

use data
db.createUser(
  {
    user: "user01",
    pwd: "123456",
    roles: ["readWrite"]
  }
)