{"id":20466,"date":"2017-01-10T12:45:18","date_gmt":"2017-01-10T18:15:18","guid":{"rendered":"https:\/\/www.solutionanalysts.com\/blog\/8-simple-steps-to-install-mongodb-with-authentication-on-ec2-ami-linux\/"},"modified":"2024-10-01T05:05:07","modified_gmt":"2024-10-01T10:35:07","slug":"8-simple-steps-to-install-mongodb-with-authentication-on-ec2-ami-linux","status":"publish","type":"post","link":"https:\/\/www.solutionanalysts.com\/blog\/8-simple-steps-to-install-mongodb-with-authentication-on-ec2-ami-linux\/","title":{"rendered":"8 Simple steps to install MongoDB with authentication on EC2 AMI Linux"},"content":{"rendered":"<p>In this tutorial, we will show you how to install MongoDB with authentication on EC2 AMI Linux.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span><strong>Prerequisites:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>You have ec2 instance running<\/li>\n<li>You have root access to ec2 instance<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Step_1_Connect_to_ec2_instance_using_pemppk_file\"><\/span>Step 1: Connect to ec2 instance using pem\/ppk file<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For MongoDB 3.0, create below file using vi or any other editor<\/p>\n<p><code>vi \/etc\/yum.repos.d\/mongodb-org-3.0.repo<\/code><\/p>\n<p>Add below content in above created file<\/p>\n<p><code>[mongodb-org-3.0]<br \/>\nname=MongoDB Repository<br \/>\nbaseurl=https:\/\/repo.mongodb.org\/yum\/amazon\/2013.03\/mongodb-org\/3.0\/x86_64\/<br \/>\ngpgcheck=0<br \/>\nenabled=1 <\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_2_Install_mongodb_using_below_command\"><\/span>Step 2: Install mongodb using below command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>sudo yum install -y mongodb-org<\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_3_Start_MongoDB_service_using_below_command\"><\/span>Step 3: Start MongoDB service using below command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>sudo service mongod start<\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_4_Start_MongoDB_on_reboot\"><\/span>Step 4: Start MongoDB on reboot<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:<\/p>\n<p><code>sudo chkconfig mongod on<\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_5_Connect_to_mongo_shell\"><\/span>Step 5: Connect to mongo shell<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once service is started you need to connect to mongo shell for creating user. To connect to mongo shell use below command<\/p>\n<p><code>mongo<\/code><\/p>\n<p>If you find below error when using mongo command<\/p>\n<p>&#8220;Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and\/or LC_* environment variables are set correctly&#8221; Add export as mentioned below<\/p>\n<p><code>export LC_ALL=C<\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_6_Select_Admin\"><\/span>Step 6: Select Admin<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once connected successfully to mongo, select admin<\/p>\n<p><code>use admin<\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_7_Create_User\"><\/span>Step 7: Create User<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Create user as per below:<\/p>\n<p><code>db.createUser(<br \/>\n{<br \/>\nuser: \"myUserAdmin\",<br \/>\npwd: \"abc123\",<br \/>\nroles: [ { role: \"userAdminAnyDatabase\", db: \"admin\" } ]<br \/>\n}<br \/>\n)<\/code><\/p>\n<p>Create user for specific database<br \/>\nTo create user for a particular database, repeat step 6 with below command<\/p>\n<p><code>use <\/code><\/p>\n<p>To create user for above database:<\/p>\n<p><code>db.createUser(<br \/>\n{<br \/>\nuser: \"\",<br \/>\npwd: \"mypassword\",<br \/>\nroles: [ { role: \"readWrite\", db: \"muddle\" }]<br \/>\n}<br \/>\n)<\/code><\/p>\n<p>Edit \/etc\/mongod.conf<br \/>\nFor mongo 3.x, Add this to the config<\/p>\n<p><code>security:<br \/>\nauthorization: \"enabled\"<br \/>\nThen run below command<\/code><\/p>\n<p>service mongod restart<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_8\"><\/span>Step 8:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you want to connect MongoDB remotely, edit below file with vi or any other editor<\/p>\n<p><code>vi \/etc\/mongod.conf<\/code><\/p>\n<p>Add remove IP in bindIp as per below and restart mongodb service<br \/>\n<code>bindIp: 127.0.0.1,8.8.8.8<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will show you how to install MongoDB with authentication on EC2 AMI Linux. Prerequisites: You have ec2 instance running You have root access to ec2 instance Step 1: Connect to ec2 instance using pem\/ppk file For MongoDB 3.0, create below file using vi or any other editor vi \/etc\/yum.repos.d\/mongodb-org-3.0.repo Add below [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":38513,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[132],"tags":[],"class_list":["post-20466","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-big-data"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/comments?post=20466"}],"version-history":[{"count":1,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20466\/revisions"}],"predecessor-version":[{"id":32870,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20466\/revisions\/32870"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media\/38513"}],"wp:attachment":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media?parent=20466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/categories?post=20466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/tags?post=20466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}