{"id":9421,"date":"2021-07-01T18:10:30","date_gmt":"2021-07-01T15:10:30","guid":{"rendered":"https:\/\/kifarunix.com\/?p=9421"},"modified":"2024-03-18T19:53:08","modified_gmt":"2024-03-18T16:53:08","slug":"install-mongodb-on-rocky-linux-8","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-mongodb-on-rocky-linux-8\/","title":{"rendered":"Install MongoDB on Rocky Linux 8"},"content":{"rendered":"\n
This guide provides a step-by-step tutorial on how to install MongoDB on Rocky Linux 8. MongoDB<\/a> is a cross-platform document-oriented and a NoSQL database program.<\/p>\n\n\n\n As of this writing, the official default repositories for Rocky Linux 8 do not provide MongoDB packages.<\/p>\n\n\n\n Thus, in order to install MongoDB on Rocky Linux, you need to create the MongoDB repository.<\/p>\n\n\n\n Note that MongoDB is available as a Community Edition and as an Enterprise Edition. This guide installs CE version.<\/p>\n\n\n\n The repository created below is for installing MongoDB 4.4<\/a>, which is the current stable release version.<\/p>\n\n\n\n Run the command below to install MongoDB 4.4 repository on Rocky Linux 8.<\/p>\n\n\n\n Once the MongoDB repository, you can install MongoDB by running the command;<\/p>\n\n\n\n Once the installation is done, you can verify the installed version by running the command below;<\/p>\n\n\n\n MongoDB runs as a non-privileged MongoDB daemon is managed by systemd. Hence, you can start it by executing the command below;<\/p>\n\n\n\n To check if MongoDB has run successfully, check its log file, You can also check the status of the service by running the command below;<\/p>\n\n\n\n Enable MongoDB to run on system boot.<\/p>\n\n\n\n Once the installation completes, you can now start using MongoDB.<\/p>\n\n\n\n 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 From the MongoDB shell, you can run your database management commands. For example to list available databases;<\/p>\n\n\n\n To use a specific MongoDB database, simply run;<\/p>\n\n\n\n To create MongoDB database, simply switch to a new database (non-existing database) by specifying the name of the database to create and insert data into it.<\/p>\n\n\n\n You can now insert data into your new database.<\/p>\n\n\n\n Press Enter to insert the data.<\/p>\n\n\n\n To list database collections;<\/p>\n\n\n\n To show the data contained in a MongoDB database collection;<\/p>\n\n\n\n Take for example, to list the content of the collection created above;<\/p>\n\n\n\n Sample output;<\/p>\n\n\n\n To create MongoDB database user with read\/write privileges.<\/p>\n\n\n\n Press Enter to add the user.<\/p>\n\n\n\n To list database users;<\/p>\n\n\n\n If you want to create an administrator for a single database;<\/p>\n\n\n\n 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 Well, there is a lot more to learn on how to use MongoDB. Refer to MongoDB Getting Started Guides<\/a> for more information.<\/p>\n\n\n\n If you note, Access control is not enabled for the database by default. To learn how to enable password authentication for MongoDB, check the guide below;<\/p>\n\n\n\n Enable MongoDB Authentication<\/a><\/p>\n\n\n\n Install Robo 3T MongoDB GUI Tool on CentOS 8<\/a><\/p>\n\n\n\nInstalling MongoDB on Rocky Linux 8<\/h2>\n\n\n\n
Create MongoDB RPM Repository<\/h4>\n\n\n\n
\ncat > \/etc\/yum.repos.d\/mongodb.repo << 'EOL'\n[mongodb-org-4.4]\nname=MongoDB Repository\nbaseurl=https:\/\/repo.mongodb.org\/yum\/redhat\/$releasever\/mongodb-org\/4.4\/x86_64\/\ngpgcheck=1\nenabled=1\ngpgkey=https:\/\/www.mongodb.org\/static\/pgp\/server-4.4.asc\nEOL\n<\/code><\/pre>\n\n\n\n
Install MongoDB<\/h4>\n\n\n\n
dnf install mongodb-org<\/code><\/pre>\n\n\n\n
\nMongoDB Repository 21 kB\/s | 23 kB 00:01 \nDependencies resolved.\n============================================================================================================================================================================\n Package Architecture Version Repository Size\n============================================================================================================================================================================\nInstalling:\n mongodb-org x86_64 4.4.6-1.el8 mongodb-org-4.4 11 k\nInstalling dependencies:\n cyrus-sasl x86_64 2.1.27-5.el8 baseos 95 k\n cyrus-sasl-gssapi x86_64 2.1.27-5.el8 baseos 49 k\n cyrus-sasl-plain x86_64 2.1.27-5.el8 baseos 46 k\n mongodb-database-tools x86_64 100.3.1-1 mongodb-org-4.4 54 M\n mongodb-org-database-tools-extra x86_64 4.4.6-1.el8 mongodb-org-4.4 23 k\n mongodb-org-mongos x86_64 4.4.6-1.el8 mongodb-org-4.4 17 M\n mongodb-org-server x86_64 4.4.6-1.el8 mongodb-org-4.4 22 M\n mongodb-org-shell x86_64 4.4.6-1.el8 mongodb-org-4.4 14 M\n mongodb-org-tools x86_64 4.4.6-1.el8 mongodb-org-4.4 11 k\n python3-pip noarch 9.0.3-19.el8.rocky appstream 19 k\n python3-setuptools noarch 39.2.0-6.el8 baseos 162 k\n python36 x86_64 3.6.8-2.module+el8.3.0+120+426d8baf appstream 18 k\nEnabling module streams:\n python36 3.6 \n\nTransaction Summary\n============================================================================================================================================================================\nInstall 13 Packages\n\nTotal download size: 106 M\nInstalled size: 342 M\nIs this ok [y\/N]: y\n<\/code><\/pre>\n\n\n\n
mongod --version<\/code><\/pre>\n\n\n\n
\ndb version v4.4.6\nBuild Info: {\n \"version\": \"4.4.6\",\n \"gitVersion\": \"72e66213c2c3eab37d9358d5e78ad7f5c1d0d0d7\",\n \"openSSLVersion\": \"OpenSSL 1.1.1g FIPS 21 Apr 2020\",\n \"modules\": [],\n \"allocator\": \"tcmalloc\",\n \"environment\": {\n \"distmod\": \"rhel80\",\n \"distarch\": \"x86_64\",\n \"target_arch\": \"x86_64\"\n }\n}\n<\/code><\/pre>\n\n\n\n
Running MongoDB on Rocky Linux 8<\/h3>\n\n\n\n
mongod<\/code> user and it uses the
\/var\/lib\/mongo<\/strong><\/code> (the data directory) and
\/var\/log\/mongodb<\/code><\/strong> (the log directory) default directories.<\/p>\n\n\n\n
systemctl start mongod<\/code><\/pre>\n\n\n\n
\/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
tail \/var\/log\/mongodb\/mongod.log<\/code><\/pre>\n\n\n\n
...\n{\"t\":{\"$date\":\"2021-07-01T17:55:47.409+03:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":23015, \"ctx\":\"listener\",\"msg\":\"Listening on\",\"attr\":{\"address\":\"\/tmp\/mongodb-27017.sock\"}}\n {\"t\":{\"$date\":\"2021-07-01T17:55:47.409+03:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":23015, \"ctx\":\"listener\",\"msg\":\"Listening on\",\"attr\":{\"address\":\"127.0.0.1\"}}\n {\"t\":{\"$date\":\"2021-07-01T17:55:47.409+03:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":23016, \"ctx\":\"listener\",\"msg\":\"Waiting for connections<\/strong>\",\"attr\":{\"port\":27017,\"ssl\":\"off\"}}\n...<\/code><\/pre>\n\n\n\n
systemctl status mongod<\/code><\/pre>\n\n\n\n
\n\u25cf mongod.service - MongoDB Database Server\n Loaded: loaded (\/usr\/lib\/systemd\/system\/mongod.service; enabled; vendor preset: disabled)\n Active: active (running) since Thu 2021-07-01 17:55:47 EAT; 1min 34s ago\n Docs: https:\/\/docs.mongodb.org\/manual\n Process: 2191 ExecStart=\/usr\/bin\/mongod $OPTIONS (code=exited, status=0\/SUCCESS)\n Process: 2189 ExecStartPre=\/usr\/bin\/chmod 0755 \/var\/run\/mongodb (code=exited, status=0\/SUCCESS)\n Process: 2187 ExecStartPre=\/usr\/bin\/chown mongod:mongod \/var\/run\/mongodb (code=exited, status=0\/SUCCESS)\n Process: 2186 ExecStartPre=\/usr\/bin\/mkdir -p \/var\/run\/mongodb (code=exited, status=0\/SUCCESS)\n Main PID: 2194 (mongod)\n Memory: 76.1M\n CGroup: \/system.slice\/mongod.service\n \u2514\u25002194 \/usr\/bin\/mongod -f \/etc\/mongod.conf\n\nJul 01 17:55:39 localhost.localdomain systemd[1]: Starting MongoDB Database Server...\nJul 01 17:55:40 localhost.localdomain mongod[2191]: about to fork child process, waiting until server is ready for connections.\nJul 01 17:55:40 localhost.localdomain mongod[2191]: forked process: 2194\nJul 01 17:55:47 localhost.localdomain mongod[2191]: child process started successfully, parent exiting\nJul 01 17:55:47 localhost.localdomain systemd[1]: Started MongoDB Database Server.\n<\/code><\/pre>\n\n\n\n
systemctl enable mongod<\/code><\/pre>\n\n\n\n
Using MongoDB on Rocky Linux 8<\/h3>\n\n\n\n
mongo<\/code><\/pre>\n\n\n\n
\nMongoDB shell version v4.4.6\nconnecting to: mongodb:\/\/127.0.0.1:27017\/?compressors=disabled&gssapiServiceName=mongodb\nImplicit session: session { \"id\" : UUID(\"678ce04a-f894-44a3-b70d-48c9bee1ab8f\") }\nMongoDB server version: 4.4.6\nWelcome to the MongoDB shell.\nFor interactive help, type \"help\".\n...\n>\n<\/code><\/pre>\n\n\n\n
> db\ntest<\/code><\/pre>\n\n\n\n
use database-name<\/strong><\/code><\/pre>\n\n\n\n
> use testdatabase\nswitched to db testdatabase<\/code><\/pre>\n\n\n\n
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
{\n\t\"acknowledged\" : true,\n\t\"insertedId\" : ObjectId(\"5da6036c387fad741503e4a1\")\n}<\/code><\/pre>\n\n\n\n
show collections<\/code><\/pre>\n\n\n\n
db.NAME-OF-COLLECTION<\/strong>.find().pretty()<\/code><\/pre>\n\n\n\n
> use testdatabase\nswitched to db testdatabase<\/code><\/pre>\n\n\n\n
> show collections\nuserdetails<\/code><\/pre>\n\n\n\n
> db.userdetails.find().pretty()<\/code><\/pre>\n\n\n\n
{\n\t\"_id\" : ObjectId(\"60ddd89d60fdb735de85a0c6\"),\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><\/code><\/pre>\n\n\n\n
use testdatabase<\/code><\/pre>\n\n\n\n
db.createUser(\n {\n user: 'testuser',\n pwd: 'P@ssWord',\n roles: [ { role: 'readWrite<\/strong>', db: 'testdatabase' } ]\n }\n );<\/code><\/pre>\n\n\n\n
Successfully added user: {\n \"user\" : \"testuser\",\n \"roles\" : [\n {\n \"role\" : \"readWrite\",\n \"db\" : \"testdatabase\"\n }\n ]\n }<\/code><\/pre>\n\n\n\n
db.getUsers()<\/code><\/pre>\n\n\n\n
\n[\n\t{\n\t\t\"_id\" : \"testdatabase.testuser\",\n\t\t\"userId\" : UUID(\"ffd6f3ae-9b07-4b5a-84e0-9679923c625e\"),\n\t\t\"user\" : \"testuser\",\n\t\t\"db\" : \"testdatabase\",\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
use testdatabase<\/code><\/pre>\n\n\n\n
db.createUser(\n {\n user: 'testadmin',\n pwd: 'P@ssW0rd',\n roles: [ { role: 'userAdmin<\/strong>', db: 'testdatabase' } ]\n }\n);<\/code><\/pre>\n\n\n\n
use admin<\/code><\/pre>\n\n\n\n
db.createUser(\n {\n user: 'admin',\n pwd: 'P@ssW0rd',\n roles: [ { role: 'userAdminAnyDatabase<\/strong>', db: 'admin' } ]\n }\n);<\/code><\/pre>\n\n\n\n
Enable password Authentication on MongoDB<\/h3>\n\n\n\n
Other Tutorials<\/h3>\n\n\n\n