{"id":7371,"date":"2023-09-11T17:05:12","date_gmt":"2023-09-11T11:35:12","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=7371"},"modified":"2023-09-11T17:05:52","modified_gmt":"2023-09-11T11:35:52","slug":"how-to-create-read-only-access-for-a-user-in-mongodb","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-create-read-only-access-for-a-user-in-mongodb\/","title":{"rendered":"How to create read-only access for a user in MongoDB"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Introduction:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In MongoDB, you can create read-only access for users by assigning appropriate roles that grant read-only privileges to the user. MongoDB provides several built-in roles that can be used to grant read-only access. The two most common built-in roles for read-only access are read and readAnyDatabase.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Prerequisite<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Server root login credentials.<\/li>\n\n\n\n<li>Mongo DB credentials if it is authenticated.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1:<\/strong><br>Log in to your Ubuntu or Centos server as a user with sudo privileges. <\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ ssh root@Ip<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><strong>Step 2:<\/strong><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Login to Mongo Shell. If authenticated, please follow the below steps. <br>Then use admin.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ mongo<br>      or<br>$ mongo -u username -p &#8211;authenticationDatabase admin<br>$ use admin<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><strong>Step 3:<\/strong><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To create the read-only user.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ db.createUser({<br>         user: &#8220;testuser&#8221;,<br>         pwd: &#8220;password &#8220;,<br>         roles: [<br>            { role: &#8220;dbAdmin&#8221;, db: &#8220;database&#8221; },<br>            { role: &#8220;readAnyDatabase&#8221;, db: &#8220;admin&#8221; }<br>    ]<br>})<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><strong>Step 4:<\/strong><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Authenticate the user. If authentication is successful, the result will be <strong>1<\/strong>. If authentication fails, you get an error.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ db.auth(&#8216;testuser&#8217;,&#8217;password&#8217;)<br>$ exit<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">verify the authentication for the user as mentioned below.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$mongo -u testuser -p<br>       or<br>$ mongo -u testuser -p &#8211;authenticationDatabase admin<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><strong>Conclusion<\/strong><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Following the steps mentioned above, we can create read-only access for users in MongoDB.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: In MongoDB, you can create read-only access for users by assigning appropriate roles that grant read-only privileges to the [&hellip;]<\/p>\n","protected":false},"author":499,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-7371","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1UT","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/499"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=7371"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7371\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}