{"id":4413,"date":"2019-10-15T21:28:10","date_gmt":"2019-10-15T18:28:10","guid":{"rendered":"https:\/\/kifarunix.com\/?p=4413"},"modified":"2024-03-12T22:16:09","modified_gmt":"2024-03-12T19:16:09","slug":"install-mongodb-community-edition-on-centos-8","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-mongodb-community-edition-on-centos-8\/","title":{"rendered":"Install MongoDB Community Edition on CentOS 8"},"content":{"rendered":"\n<p>This guide provides a step-by-step tutorial on how to install MongoDB community edition on CentOS 8. <a rel=\"noreferrer noopener\" aria-label=\"MongoDB (opens in a new tab)\" href=\"https:\/\/www.mongodb.com\/\" target=\"_blank\">MongoDB<\/a> is a cross-platform document-oriented and a NoSQL database program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing MongoDB Community Edition on CentOS 8<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Run system update.<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf update<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create MongoDB RPM Repository<\/h4>\n\n\n\n<p>As of this writing, the official MongoDB rpm repos for centOS 8 are not yet released and thus the repos providing test MongoDB packages are  available.<\/p>\n\n\n\n<p>To create the MongoDB rpm repository file, run the command below and enter content below;<\/p>\n\n\n\n<p>The repository created below is for installing MongoDB 4.2 on CentOS 8.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\ncat > \/etc\/yum.repos.d\/mongodb.repo << 'EOL'\n[mongodb-org-4.2]\nname=MongoDB Repository\nbaseurl=https:\/\/repo.mongodb.org\/yum\/redhat\/8\/mongodb-org\/testing\/x86_64\/\ngpgcheck=1\nenabled=1\ngpgkey=https:\/\/www.mongodb.org\/static\/pgp\/server-4.2.asc\nEOL\n<\/code><\/pre>\n\n\n\n<p>You can as well use the official repos for previous CentOS releases. For example, to use the RPM repos for CentOS 7;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\ncat > \/etc\/yum.repos.d\/mongodb.repo << 'EOL'\n[mongodb-org-4.2]\nname=MongoDB Repositorybaseurl=https:\/\/repo.mongodb.org\/yum\/redhat\/7\/mongodb-org\/4.2\/x86_64\/\ngpgcheck=1\nenabled=1\ngpgkey=https:\/\/www.mongodb.org\/static\/pgp\/server-4.2.asc\nEOL\n<\/code><\/pre>\n\n\n\n<p>Once the MongoDB repository, you can install the MongoDB and associated packages by running the command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf install mongodb-org<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n...\n=======================================================================================================================================================\n Package                            Arch                   Version                                               Repository                       Size\n=======================================================================================================================================================\nInstalling:\n mongodb-org                        x86_64                 4.2.1-0.1.rc0.el8                                     mongodb-org-4.2                  10 k\nInstalling dependencies:\n python2                            x86_64                 2.7.15-24.module_el8.0.0+193+7850e68f                 AppStream                       107 k\n python2-libs                       x86_64                 2.7.15-24.module_el8.0.0+193+7850e68f                 AppStream                       6.0 M\n python2-pip                        noarch                 9.0.3-13.module_el8.0.0+32+017b2cba                   AppStream                       2.0 M\n python2-setuptools                 noarch                 39.0.1-11.module_el8.0.0+32+017b2cba                  AppStream                       643 k\n mongodb-org-mongos                 x86_64                 4.2.1-0.1.rc0.el8                                     mongodb-org-4.2                  14 M\n mongodb-org-server                 x86_64                 4.2.1-0.1.rc0.el8                                     mongodb-org-4.2                  25 M\n mongodb-org-shell                  x86_64                 4.2.1-0.1.rc0.el8                                     mongodb-org-4.2                  17 M\n mongodb-org-tools                  x86_64                 4.2.1-0.1.rc0.el8                                     mongodb-org-4.2                  47 M\nEnabling module streams:\n python27                                                  2.7                                                                                        \n\nTransaction Summary\n=======================================================================================================================================================\nInstall  9 Packages\n\nTotal download size: 111 M\nInstalled size: 295 M\nIs this ok [y\/N]: y\n<\/code><\/pre>\n\n\n\n<p>Once the installation is done, you can verify the installed version by running the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mongod --version<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\ndb version v4.2.1-rc0\ngit version: edf6d45851c0b9ee15548f0f847df141764a317e\nOpenSSL version: OpenSSL 1.1.1 FIPS  11 Sep 2018\nallocator: tcmalloc\nmodules: none\nbuild environment:\n    distmod: rhel80\n    distarch: x86_64\n    target_arch: x86_64\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Running MongoDB on CentOS 8<\/h3>\n\n\n\n<p>MongoDB runs as a non-privileged <code>mongod<\/code>&nbsp;user and it uses the <code>\/var\/lib\/mongo<\/code>&nbsp;(the data directory) and <code>\/var\/log\/mongodb<\/code>&nbsp;(the log directory) default directories.<\/p>\n\n\n\n<p>MongoDB daemon is managed by systemd. Hence, you can start it by executing the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl start mongod<\/code><\/pre>\n\n\n\n<p>To check if MongoDB has run successfully, check its log file, <strong><code>\/var\/log\/mongodb\/mongod.log<\/code><\/strong>. You should be able to see a line indicating the MongoDB is ready and waiting for the connections.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tail \/var\/log\/mongodb\/mongod.log<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>...\n2019-10-15T12:20:32.490-0400 I  NETWORK  [initandlisten] Listening on \/tmp\/mongodb-27017.sock\n2019-10-15T12:20:32.490-0400 I  NETWORK  [initandlisten] Listening on 127.0.0.1\n<strong>2019-10-15T12:20:32.491-0400 I  NETWORK  [initandlisten] waiting for connections on port 27017<\/strong>\n...<\/code><\/pre>\n\n\n\n<p>Enable MongoDB to run on system boot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl enable mongod<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using MongoDB on CentOS 8<\/h3>\n\n\n\n<p>Once the installation completes, you can now start using MongoDB. MongoDB listens on local address on port 27017 by default. Hence, from the localhost, you can simply login to MongoDB shell by running;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mongo<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>...\nMongoDB shell version v4.2.1-rc0\nconnecting to: mongodb:\/\/127.0.0.1:27017\/?compressors=disabled&amp;gssapiServiceName=mongodb\nImplicit session: session { \"id\" : UUID(\"1f77f765-eb9f-414c-9a33-d40d4ad3c2dd\") }\nMongoDB server version: 4.2.1-rc0\nWelcome to the MongoDB shell.\nFor interactive help, type \"help\".\n...\n&gt;<\/code><\/pre>\n\n\n\n<p>From the MongoDB shell, you can run your database management commands. For example to list available databases;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&gt; db\ntest<\/code><\/pre>\n\n\n\n<p>To use a specific MongoDB database, simply run;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>use database-name<\/code><\/pre>\n\n\n\n<p>To create MongoDB database, simply switch to a new database (non-existing databases) by specifying the name of the database to create and insert data into it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&gt; use testdatabase\nswitched to db testdatabase<\/code><\/pre>\n\n\n\n<p>You can now insert data into your new database.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>db.userdetails.insertOne(\n   { \"F_Name\" : \"fname\",\n     \"L_NAME\" : \"lname\",\n     \"ID_NO\" : \"12345\",\n     \"AGE\" : \"19\",\n     \"TEL\" : \"654321\"\n   }\n)<\/code><\/pre>\n\n\n\n<p>Press Enter to insert the data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>{\n\t\"acknowledged\" : true,\n\t\"insertedId\" : ObjectId(\"5da6036c387fad741503e4a1\")\n}<\/code><\/pre>\n\n\n\n<p>To list database collections;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>show collections<\/code><\/pre>\n\n\n\n<p>To show the data contained in a MongoDB database collection;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><code>db.<strong>NAME-OF-COLLECTION<\/strong>.find().pretty()<\/code><\/pre>\n\n\n\n<p>Take for example, to list the content of the collection created above;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&gt; use testdatabase\nswitched to db testdatabase<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&gt; show collections\nuserdetails<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&gt; db.userdetails.find().pretty()\n{\n\t\"_id\" : ObjectId(\"5da6036c387fad741503e4a1\"),\n\t\"F_Name\" : \"fname\",\n\t\"L_NAME\" : \"lname\",\n\t\"ID_NO\" : \"12345\",\n\t\"AGE\" : \"19\",\n\t\"TEL\" : \"654321\"\n}\n&gt;<\/code><\/pre>\n\n\n\n<p>To create MongoDB database user with read\/write privileges.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>use testdatabase<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>db.createUser(\n   {\n     user: 'testuser',\n     pwd: 'P@ssWord',\n     roles: [ { role: '<strong>readWrite<\/strong>', db: 'testdatabase' } ]\n   }\n );<\/code><\/pre>\n\n\n\n<p>Press Enter to add the user.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Successfully added user: {\n     \"user\" : \"testuser\",\n     \"roles\" : [\n         {\n             \"role\" : \"readWrite\",\n             \"db\" : \"testdatabase\"\n         }\n     ]\n }<\/code><\/pre>\n\n\n\n<p>To list database users;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>db.getUsers()<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\n[\n\t{\n\t\t\"_id\" : \"testdb.testuser\",\n\t\t\"userId\" : UUID(\"e0e3911b-c2b3-4580-b043-1c044b0f51f5\"),\n\t\t\"user\" : \"testuser\",\n\t\t\"db\" : \"testdb\",\n\t\t\"roles\" : [\n\t\t\t{\n\t\t\t\t\"role\" : \"readWrite\",\n\t\t\t\t\"db\" : \"testdatabase\"\n\t\t\t}\n\t\t],\n\t\t\"mechanisms\" : [\n\t\t\t\"SCRAM-SHA-1\",\n\t\t\t\"SCRAM-SHA-256\"\n\t\t]\n\t}\n]\n<\/code><\/pre>\n\n\n\n<p>If you want to create an administrator for a single database;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>use testdatabase<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>db.createUser(\n  {\n    user: 'testadmin',\n    pwd: 'P@ssW0rd',\n    roles: [ { role: '<strong>userAdmin<\/strong>', db: 'testdatabase' } ]\n  }\n);<\/code><\/pre>\n\n\n\n<p>To create an overall database admin with all administrative rights on all databases, use the administrative database and create admin user as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>use admin<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>db.createUser(\n  {\n    user: 'admin',\n    pwd: 'P@ssW0rd',\n    roles: [ { role: '<strong>userAdminAnyDatabase<\/strong>', db: 'admin' } ]\n  }\n);<\/code><\/pre>\n\n\n\n<p>Well, there is a lot more to learn on how to use MongoDB. Refer to <a aria-label=\"MongoDB Getting Started Guides (opens in a new tab)\" href=\"https:\/\/docs.mongodb.com\/manual\/tutorial\/getting-started\/\" target=\"_blank\" rel=\"noreferrer noopener\">MongoDB Getting Started Guides<\/a> for more information.<\/p>\n\n\n\n<p>Learn how to manage MongoDB from graphical user interface using Robo 3T by following the link below;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-robo-3t-mongodb-gui-tool-on-centos-8\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Install Robo 3T MongoDB GUI Tool on CentOS 8<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Related Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-mongodb-4-on-fedora-30-29-centos-7\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Install MongoDB 4 on Fedora 30\/29\/CentOS 7<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-mariadb-10-4-on-centos-8\/\" target=\"_blank\">Install MariaDB 10.4 on CentOS 8<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-mysql-8-on-centos-8\/\" target=\"_blank\">Install MySQL 8 on CentOS 8<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/install-lamp-stack-on-centos-8\/\" target=\"_blank\">Install LAMP Stack on CentOS 8<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-lemp-stack-on-centos-8\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Install LEMP Stack on CentOS 8<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide provides a step-by-step tutorial on how to install MongoDB community edition on CentOS 8. MongoDB is a cross-platform document-oriented and a NoSQL database<\/p>\n","protected":false},"author":1,"featured_media":9425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[299,121,959],"tags":[1142,3776,1000,3775,961,3777],"class_list":["post-4413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database","category-howtos","category-mongodb","tag-centos-8","tag-centos-8-mongodb","tag-database","tag-install-mongodb-on-centos-8","tag-mongodb","tag-mongodb-centos-8","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4413"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=4413"}],"version-history":[{"count":14,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4413\/revisions"}],"predecessor-version":[{"id":21229,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4413\/revisions\/21229"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/9425"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=4413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=4413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=4413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}