「MongoDB」:修訂間差異
跳至導覽
跳至搜尋
(未顯示同一使用者於中間所作的 9 次修訂) | |||
第10行: | 第10行: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
cd /tmp; wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb; sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb; sudo percona-release enable psmdb-36 release; sudo apt update; sudo apt install -y percona-server-mongodb-36 | cd /tmp; wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb; sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb; sudo percona-release enable psmdb-36 release; sudo apt update; sudo apt install -y percona-server-mongodb-36; sudo apt clean | ||
</syntaxhighlight> | </syntaxhighlight> | ||
第16行: | 第16行: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
cd /tmp; wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb; sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb; sudo percona-release enable psmdb-42 release; sudo apt update; sudo apt install -y percona-server-mongodb | cd /tmp; wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb; sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb; sudo percona-release enable psmdb-42 release; sudo apt update; sudo apt install -y percona-server-mongodb; sudo apt clean | ||
</syntaxhighlight> | </syntaxhighlight> | ||
或是比較新的6.0版本: | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
sudo apt install -y percona-server-mongodb- | cd /tmp; wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb; sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb; sudo percona-release enable psmdb-60 release; sudo apt update; sudo apt install -y percona-server-mongodb; sudo apt clean | ||
</syntaxhighlight> | |||
用戶端套件(<code>mongosh</code>指令): | |||
<syntaxhighlight lang="bash"> | |||
sudo apt install -y percona-mongodb-mongosh; sudo apt clean | |||
</syntaxhighlight> | </syntaxhighlight> | ||
第64行: | 第70行: | ||
port: 27017 | port: 27017 | ||
bindIp: 0.0.0.0 | bindIp: 0.0.0.0 | ||
</syntaxhighlight> | |||
== 連線 == | |||
本地端的機器: | |||
<syntaxhighlight lang="bash"> | |||
mongosh -u root | |||
</syntaxhighlight> | |||
連到遠端的機器: | |||
<syntaxhighlight lang="bash"> | |||
mongosh mongodb://db.example.com/ -u root | |||
</syntaxhighlight> | |||
== 常用指令 == | |||
在mongosh下面可以這樣跑: | |||
<syntaxhighlight lang="sql"> | |||
show dbs | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="sql"> | |||
show collections | |||
show tables | |||
db.getCollectionNames() | |||
</syntaxhighlight> | </syntaxhighlight> | ||
第69行: | 第103行: | ||
* {{Official|https://www.mongodb.com/}} {{en}} | * {{Official|https://www.mongodb.com/}} {{en}} | ||
* [https://www.percona.com/software/mongodb Percona Distribution for MongoDB] {{en}} | |||
[[Category:軟體]] | [[Category:軟體]] |
於 2023年10月23日 (一) 16:00 的最新修訂
MongoDB是一套NoSQL類型資料庫。
簡介
MongoDB是一套以JSON為資料儲存型態的資料庫。要注意軟體授權,在4.0.4之後的版本,以及4.1.5之後的版本為SSPL授權,社群一般不認可為開源軟體。
安裝
這邊安裝的是Percona包裝的版本(Percona Server for MongoDB 3.6,對應到官方的3.6版):
cd /tmp; wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb; sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb; sudo percona-release enable psmdb-36 release; sudo apt update; sudo apt install -y percona-server-mongodb-36; sudo apt clean
如果沒有授權條款的疑慮,可以安裝4.2版(Percona Server for MongoDB 4.2,對應到官方的4.2版):
cd /tmp; wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb; sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb; sudo percona-release enable psmdb-42 release; sudo apt update; sudo apt install -y percona-server-mongodb; sudo apt clean
或是比較新的6.0版本:
cd /tmp; wget https://repo.percona.com/apt/percona-release_latest.$(lsb_release -sc)_all.deb; sudo dpkg -i percona-release_latest.$(lsb_release -sc)_all.deb; sudo percona-release enable psmdb-60 release; sudo apt update; sudo apt install -y percona-server-mongodb; sudo apt clean
用戶端套件(mongosh
指令):
sudo apt install -y percona-mongodb-mongosh; sudo apt clean
基本設定
MongoDB發現沒有使用者設定時會強制開放本地的anonymous登入,所以如果要關掉本地的anonymous操作,一定要先設定一組帳號與密碼。
這邊先建立root帳號:
echo -e 'use admin\ndb.createUser({user:"root",pwd:"password",roles:["root"]})' | mongo
然後在/etc/mongod.conf
裡面啟動帳號認證:
security:
authorization: "enabled"
接著重跑MongoDB:
sudo service mongod restart
最後可以測試anonymous模式與登入後的差異:
echo "show dbs" | mongo
echo "show dbs" | mongo -u root
再來是開放讓外部可以連,把bindIp
的127.0.0.1
改成0.0.0.0
:
net:
port: 27017
bindIp: 0.0.0.0
連線
本地端的機器:
mongosh -u root
連到遠端的機器:
mongosh mongodb://db.example.com/ -u root
常用指令
在mongosh下面可以這樣跑:
show dbs
show collections
show tables
db.getCollectionNames()
外部連結
- 官方網站 (英文)
- Percona Distribution for MongoDB (英文)