2022-08-02|閱讀時間 ‧ 約 4 分鐘

MongoDB authentication and replication feat. pymongo

    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 pymongoas 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:

    connect DB in the back-end

    To use both authentication and replication techniques, we would use pymongoas 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.

    分享至
    成為作者繼續創作的動力吧!
    I'm a full-stack engineer in Senao Networks. Vue/Django/MongoDB/Nginx
    從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

    發表回應

    成為會員 後即可發表留言