詳解linux 使用docker安裝mongodb方法
1、xshell遠程到一臺安裝了docker和docker-conpose的服務(wù)器(安裝方法見其他學(xué)習(xí)文件)
2、到home目錄下新建Mongotest文件夾,進入這個文件夾,執(zhí)行vi docker-compose.yml,插入如下內(nèi)容

,退出并保存
3、執(zhí)行docker-compose up -d
然后添加防火墻:firewall-cmd --add-port=27017/tcp --permanent
重啟防火墻:firewall-cmd --reload
4、查看進程 docker ps | grep mongotest,有如下進程則證明執(zhí)行成功。

如果沒有中間端口的信息:
先執(zhí)行docker logs mongotest_mongo_1查看日志,
出現(xiàn)很多:

解決:先把docker-compose down,
然后進入 vi /etc/selinux/config
把里面的權(quán)限修改掉

重啟服務(wù)器 shutdown -r now
啟動docker: sudo systemctl restart docker
然后執(zhí)行:docker-compose up -d
5、啟動數(shù)據(jù)庫 docker exec -it ${進程名稱,例如mongotest_mongo_1} mongo

表示啟動成功,進入到操作界面
6、第一次進入,執(zhí)行
use admin //使用admin權(quán)限
user testdb //新建一個“表”
db.createUser({user:‘yyx01',pwd:‘123456',roles:[{role:‘dbOwner',db:‘testdb'}]}) //新建一個用戶
show dbs //查看所有表
user testdb //切換到一個“表”
db.auth(‘yyx01',‘123456') //使用新建的用戶操作新建的表
db.users.insertOne({name:‘yeyanxi',age:30,email:‘489448521@qq.com'}) //插入一條數(shù)據(jù)
db.users.find({}) //查詢數(shù)據(jù)
db.users.updateOne({name:‘yeyanxi1'},{$set: {sex:‘man'}}) //修改數(shù)據(jù)
db.users.deleteOne({name:‘yeyanxi1'}) //刪除數(shù)據(jù)
更多表的操作指令,可以查看官網(wǎng):https://docs.mongodb.com/manual/crud/
7、使用GUI客戶端(robo 3t),下載地址:https://robomongo.org/download
在本地可以連接到服務(wù)器上的數(shù)據(jù)庫

8、如果有需要,可以備份數(shù)據(jù)庫:docker exec -it mongotest_mongo_1 mongodump -h localhost -u root -p example -o /tmp/test
docker cp ${進程id,例如acc79ae5b628}:/tmp/test /tmp/test
到此這篇關(guān)于詳解linux 使用docker安裝mongodb方法的文章就介紹到這了,更多相關(guān)docker安裝mongodb內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
老生常談MongoDB數(shù)據(jù)庫基礎(chǔ)操作
下面小編就為大家?guī)硪黄仙U凪ongoDB數(shù)據(jù)庫基礎(chǔ)操作。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
MongoDB連接數(shù)據(jù)庫并創(chuàng)建數(shù)據(jù)等使用方法
MongoDB?是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。接下來通過本文給大家介紹MongoDB連接數(shù)據(jù)庫并創(chuàng)建數(shù)據(jù)等使用方法,感興趣的朋友一起看看吧2021-11-11

