MongoDB authentication and replication feat. pymongo

更新於 發佈於 閱讀時間約 3 分鐘

Authentication

It’s dangerous to deploy a DB server without authentication. People could just access your DB by knowing where you host your DB. At least we should create a username and password to do the basic authentication.

create root user

I would like to take a quick exercise about authentication, so let’s cut the shit about different kinds of users and permission.
In the mongo shell, create a root user to handle everything as follows.
$ use admin
$ db.createUser( { user: “superuser”, pwd: “yourpwd”, roles: [ { role: “root”, db: “admin” } ] } )

mongod.cfg

Enable the authorization in the MongoDB config as follows.

connection

Let’s use pymongo as the driver in python back-end for example.
'mongodb://'+db_root_user+':'+db_root_password+'@'+db_host+':'+str(db_port)+'?authSource=admin' would pass the username and password by url.

Replication

I wrote an article about MongoDB replication before. Please check it in this url: https://medium.com/@xdxxxx4713/mongodb-replication-45ac3e6d6b30

connect DB in the back-end

To use both authentication and replication techniques, we would use pymongo as follows.
Set the db_host as the combination of the hosts of primary servers and secondary servers. Then we can use the both authentication and replication technique right now.
為什麼會看到廣告
avatar-img
9會員
49內容數
比起詳細教學單一技能,網路上或者市面上整合多項技能的教學相對少很多,但要真的完成一項專案往往不是只靠一項技能便能做到。 而且教科書式的完整教學也比較枯燥乏味,因此我想以自身的經驗為例,以全端開發各項技能中最常用到的部分進行講解,讓讀者能夠快速上手掌握全端技能。
留言0
查看全部
avatar-img
發表第一個留言支持創作者!
張哲嘉的沙龍 的其他內容
I’m sure that I would forget it again after a few months, so let’s take note of it. Installation We need to use homebrew to install certbot. renew
Introduction There are some differences between deploying a front-end framework project on the web server and deploying a back-end one. uWSGI Nginx
Introduction Flutter is a powerful framework for developing the app for different platforms without redevelopment. main.dart route routing map
msix After comparing different methods, I think the most stable way is using msix to package the windows desktop app. Installation download
Introduction Set DDNS on Google Domains To set your DDNS record on Google Domains, just follow this document. ddclient installation ddclient.conf
I’m sure that I would forget it again after a few months, so let’s take note of it. Installation We need to use homebrew to install certbot. renew
Introduction There are some differences between deploying a front-end framework project on the web server and deploying a back-end one. uWSGI Nginx
Introduction Flutter is a powerful framework for developing the app for different platforms without redevelopment. main.dart route routing map
msix After comparing different methods, I think the most stable way is using msix to package the windows desktop app. Installation download
Introduction Set DDNS on Google Domains To set your DDNS record on Google Domains, just follow this document. ddclient installation ddclient.conf
你可能也想看
Google News 追蹤
Thumbnail
在創作的路上真的很多人問我說 到底要怎麼做出符合自己期待 但又可以表現得很有美感的作品?🥹 這個問題真的應該是每個創作者都一直在學習的課題吧!
提問的內容越是清晰,強者、聰明人越能在短時間內做判斷、給出精準的建議,他們會對你產生「好印象」,認定你是「積極」的人,有機會、好人脈會不自覺地想引薦給你
Thumbnail
※ 具備三項工具: 編輯器 終端機 瀏覽器 ※ 步驟一:建立資料夾 mkdir mongodb-demo cd mongodb-demo code . ※ 步驟二:初始化,建立package.json npm init -y ※ 步驟三:安裝網頁伺服器 – Express 因為
Thumbnail
※建置 MongoDB 資料庫,可以有兩種方式: 上 MongoDB 官網,把 MongoDB 的軟體下載下來,在本地安裝資料庫。 使用 MongoDB Atlas,這是 MongoDB 官方提供的全球雲端文件資料庫服務。 兩者的功能和操作上是相同的,在本地安裝與啟動資料庫會有連線速度上的優勢
安裝官方 nginx 先在/etc/yum.repos.d新增一個nginx.repo的檔案 並加入以下內容 [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgc
Thumbnail
學習如何使用Python連接MongoDB進行憑證監控,包括建立MongoDB docker-compose、連接MongoDB、讀取yaml並寫入MongoDB、傳入env以及domain寫入MongoDB、讀取MongoDB、修改MongoDB、刪除MongoDB。
Thumbnail
上一篇說明了如何在Kubernetes上建立基本的MySQL standalone,並加入phpmyadmin(PMA)來進行圖形化的管理,本篇就再進階一步,實作MySQL replication架構(master-salve),並進行驗證是否成功。
Thumbnail
本文記錄如何在Kubernetes環境下,部署Standalone架構的MySQL Database,並透過phpmyadmin進行管理。這篇文章將分成MySQL部署在K8S內的優勢、部署MySQL DB standalone、部署PhpMyAdmin (PMA)、結論四個部分進行說明與實作的流程。
Thumbnail
本篇文章將教你如何在Kubernetes cluster內部署一個MongoDB,包括取得Manifests、建立Volume、部署實務、基本操作和結論。透過操作演示,讓你瞭解在實務上如何成功建立MongoDB,並進行基本操作。
Thumbnail
登入資料庫方式,請執行以下的命令: sudo mysql -u root -p 建立一個新資料庫,我們建立“itslinuxfoss”資料庫為例: CREATE DATABASE itslinuxfoss; 為新建立的資料庫設定新的使用者名稱和密碼: GRANT ALL PRIVILEGE
※ 什麼是MongoDB? MongoDB是一個開源、跨平台的非關聯式資料庫,屬於一種文件導向(Document-oriented database)的資料庫管理系統,也就是 NoSQL 數據庫管理系統(DBMS)。 ※ 什麼是非關聯式資料庫(NoSQL)? 非關聯式資料庫(NoSQL)的意思
Thumbnail
詳細解說如何在 Mac 上透過 Docker 安裝 MongoDB 社群版。包括 MongoDB Compass 的安裝與配置,以及 MongoDB Shell 的使用方法,為開發者提供 MongoDB 學習資源。
Thumbnail
在創作的路上真的很多人問我說 到底要怎麼做出符合自己期待 但又可以表現得很有美感的作品?🥹 這個問題真的應該是每個創作者都一直在學習的課題吧!
提問的內容越是清晰,強者、聰明人越能在短時間內做判斷、給出精準的建議,他們會對你產生「好印象」,認定你是「積極」的人,有機會、好人脈會不自覺地想引薦給你
Thumbnail
※ 具備三項工具: 編輯器 終端機 瀏覽器 ※ 步驟一:建立資料夾 mkdir mongodb-demo cd mongodb-demo code . ※ 步驟二:初始化,建立package.json npm init -y ※ 步驟三:安裝網頁伺服器 – Express 因為
Thumbnail
※建置 MongoDB 資料庫,可以有兩種方式: 上 MongoDB 官網,把 MongoDB 的軟體下載下來,在本地安裝資料庫。 使用 MongoDB Atlas,這是 MongoDB 官方提供的全球雲端文件資料庫服務。 兩者的功能和操作上是相同的,在本地安裝與啟動資料庫會有連線速度上的優勢
安裝官方 nginx 先在/etc/yum.repos.d新增一個nginx.repo的檔案 並加入以下內容 [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgc
Thumbnail
學習如何使用Python連接MongoDB進行憑證監控,包括建立MongoDB docker-compose、連接MongoDB、讀取yaml並寫入MongoDB、傳入env以及domain寫入MongoDB、讀取MongoDB、修改MongoDB、刪除MongoDB。
Thumbnail
上一篇說明了如何在Kubernetes上建立基本的MySQL standalone,並加入phpmyadmin(PMA)來進行圖形化的管理,本篇就再進階一步,實作MySQL replication架構(master-salve),並進行驗證是否成功。
Thumbnail
本文記錄如何在Kubernetes環境下,部署Standalone架構的MySQL Database,並透過phpmyadmin進行管理。這篇文章將分成MySQL部署在K8S內的優勢、部署MySQL DB standalone、部署PhpMyAdmin (PMA)、結論四個部分進行說明與實作的流程。
Thumbnail
本篇文章將教你如何在Kubernetes cluster內部署一個MongoDB,包括取得Manifests、建立Volume、部署實務、基本操作和結論。透過操作演示,讓你瞭解在實務上如何成功建立MongoDB,並進行基本操作。
Thumbnail
登入資料庫方式,請執行以下的命令: sudo mysql -u root -p 建立一個新資料庫,我們建立“itslinuxfoss”資料庫為例: CREATE DATABASE itslinuxfoss; 為新建立的資料庫設定新的使用者名稱和密碼: GRANT ALL PRIVILEGE
※ 什麼是MongoDB? MongoDB是一個開源、跨平台的非關聯式資料庫,屬於一種文件導向(Document-oriented database)的資料庫管理系統,也就是 NoSQL 數據庫管理系統(DBMS)。 ※ 什麼是非關聯式資料庫(NoSQL)? 非關聯式資料庫(NoSQL)的意思
Thumbnail
詳細解說如何在 Mac 上透過 Docker 安裝 MongoDB 社群版。包括 MongoDB Compass 的安裝與配置,以及 MongoDB Shell 的使用方法,為開發者提供 MongoDB 學習資源。