{"id":26135,"date":"2024-02-29T15:38:59","date_gmt":"2024-02-29T15:38:59","guid":{"rendered":"https:\/\/linuxways.net\/?p=26135"},"modified":"2024-02-29T15:39:47","modified_gmt":"2024-02-29T15:39:47","slug":"install-mongodb-debian-12","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/debian\/install-mongodb-debian-12\/","title":{"rendered":"How to Install MongoDB on Debian 12"},"content":{"rendered":"<p><strong>MongoDB <\/strong>is a renowned free-to-use NoSQL database for storing and handling immense amounts of structured and unstructured data. It stores data in adaptable JSON-like documents by utilizing the document-oriented model. It is a popular choice for building scalable and flexible applications because it manages data and high traffic loads by distributing data across multiple servers.<\/p>\n<p><strong>MongoDB <\/strong>offers extensive official support for numerous platforms, such as Windows, macOS, containers, as well as Linux distributions including Debian 12. This write-up will demonstrate the detailed <strong>MongoDB <\/strong>installation method along with its usage on Debian 12.<\/p>\n<p><strong>Outline<\/strong><\/p>\n<ul>\n<li><a href=\"#post-26135-_f5xk7gu8qnlt\"><strong>How to Install MongoDB on Debian 12<\/strong><\/a><\/li>\n<li><a href=\"#post-26135-_erhulgdoo1z9\"><strong>How to Manage MongoDB Services on Debian 12<\/strong><\/a><\/li>\n<li><a href=\"#post-26135-_5oswilfo8mpi\"><strong>How to Use MongoDB on Debian 12<\/strong><\/a><\/li>\n<li><a href=\"#post-26135-_qbmvdlqzetao\"><strong>How to Configure MongoDB Security on Debian 12<\/strong><\/a><\/li>\n<li><a href=\"#post-26135-_f5xk7gu8qnlt\"><strong>How to Uninstall MongoDB on Debian 12<\/strong><\/a><\/li>\n<li><a href=\"#post-26135-_91o02thkz0j\"><strong>Sum Up<\/strong><\/a><\/li>\n<\/ul>\n<h2><a id=\"post-26135-_f5xk7gu8qnlt\"><\/a>How to Install MongoDB on Debian 12<\/h2>\n<p>You can install the <strong>MongoDB <\/strong>on your Debian 12 system from the official <strong>MongoDB <\/strong>repository by importing\/adding it to your system. This method installs the up-to-date version of <strong>MongoDB <\/strong>with all the latest features. You can go through the step-by-step instructions to install <strong>MongoDB <\/strong>on Debian 12:<\/p>\n<h3><a id=\"post-26135-_jqraxszfh37b\"><\/a>Step 1: Update Debian System<\/h3>\n<p>Before installing <strong>MongoDB <\/strong>on Debian 12, make sure to refresh your system\u2019s packages as:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt update<br \/>\n<span class=\"kw2\">sudo<\/span> apt upgrade<\/div><\/div>\n<h3><a id=\"post-26135-_y4pcj36d30p2\"><\/a>Step 2: Install Required Dependencies<\/h3>\n<p>Then, install the following necessary dependencies on your Debian 12 system:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> curl gnupg<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"738\" height=\"218\" class=\"wp-image-26136\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-1.png 738w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-1-300x89.png 300w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/p>\n<h3><a id=\"post-26135-_iplqnduhjxch\"><\/a>Step 3: Import MongoDB GPG Key<\/h3>\n<p>Next, import the <strong>MongoDB <\/strong>GPG key on your Debian system through the given command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">curl <span class=\"re5\">-fsSL<\/span> https:<span class=\"sy0\">\/\/<\/span>www.mongodb.org<span class=\"sy0\">\/<\/span>static<span class=\"sy0\">\/<\/span>pgp<span class=\"sy0\">\/<\/span>server-<span class=\"nu0\">7.0<\/span>.asc <span class=\"sy0\">|<\/span> <span class=\"kw2\">sudo<\/span> gpg <span class=\"re5\">-o<\/span> <span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>share<span class=\"sy0\">\/<\/span>keyrings<span class=\"sy0\">\/<\/span>mongodb-server-<span class=\"nu0\">7.0<\/span>.gpg <span class=\"re5\">--dearmor<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"114\" class=\"wp-image-26137\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-2.png 736w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-2-300x46.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/p>\n<h3><a id=\"post-26135-_4misjkaeaw6j\"><\/a>Step 4: Add MongoDB Repository to Debian 12<\/h3>\n<p>After that, add\/import the official <strong>MongoDB <\/strong>repository to the Debian 12 system:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;deb [ signed-by=\/usr\/share\/keyrings\/mongodb-server-7.0.gpg ] http:\/\/repo.mongodb.org\/apt\/debian bookworm\/mongodb-org\/7.0 main&quot;<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">sudo<\/span> <span class=\"kw2\">tee<\/span> <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>apt<span class=\"sy0\">\/<\/span>sources.list.d<span class=\"sy0\">\/<\/span>mongodb-org-<span class=\"nu0\">7.0<\/span>.list<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"174\" class=\"wp-image-26138\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-3.png 736w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-3-300x71.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/p>\n<h3><a id=\"post-26135-_lytyt9hq6byx\"><\/a>Step 5: Refresh System Packages<\/h3>\n<p>To implement new changes, you are also required to update your system repository:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt update<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"405\" class=\"wp-image-26140\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-4.png 737w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-4-300x165.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/p>\n<h3><a id=\"post-26135-_vdw97p7jzej9\"><\/a>Step 6: Install MongoDB on Debian 12<\/h3>\n<p>Now, write out the following command to install <strong>MongoDB <\/strong>along with additional packages on Debian 12 from the official repository:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> mongodb-org <span class=\"re5\">-y<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"735\" height=\"407\" class=\"wp-image-26142\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-5.png 735w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-5-300x166.png 300w\" sizes=\"auto, (max-width: 735px) 100vw, 735px\" \/><\/p>\n<h3><a id=\"post-26135-_ve1bs7csd39l\"><\/a>Step 7: Ensure MongoDB Installation<\/h3>\n<p>Finally, display the <strong>MongoDB <\/strong>version to confirm its installation of the Debian 12 system:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">mongod <span class=\"re5\">--version<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"366\" class=\"wp-image-26144\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-6.png 736w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-6-300x149.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/p>\n<h2><a id=\"post-26135-_erhulgdoo1z9\"><\/a>How to Manage MongoDB Service on Debian 12<\/h2>\n<p>After the installation of <strong>MongoDB <\/strong>on your Debian 12 system, you can manage its services to control the accessibility of the database and ensure its security. Managing the <strong>MongoDB <\/strong>services involves starting, stopping, restarting, enabling, and disabling <strong>MongoDB <\/strong>services.<\/p>\n<p>You can manage your <strong>MongoDB <\/strong>services on the Debian 12 system as<\/p>\n<h3><a id=\"post-26135-_z1xizxb55dq5\"><\/a>Start MongoDB Service<\/h3>\n<p>Usually, the <strong>MongoDB <\/strong>services start after its installation on the Debian system. However, you can start it manually using the given command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> systemctl start mongod<\/div><\/div>\n<h3><a id=\"post-26135-_t1nrfua9lzwe\"><\/a>Enable MongoDB Service<\/h3>\n<p>If you want <strong>MongoDB <\/strong>services to start automatically whenever the system boots up, utilize the below-mentioned command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> systemctl <span class=\"kw3\">enable<\/span> mongod<\/div><\/div>\n<h3><a id=\"post-26135-_k0kjvvsmumn2\"><\/a>Disable MongoDB Service<\/h3>\n<p>To stop\/prevent <strong>MongoDB <\/strong>services from initiating automatically when the Debian system boots up, type the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> systemctl disable mongod<\/div><\/div>\n<h3><a id=\"post-26135-_7qm5hfwaivm8\"><\/a>Stop MongoDB Service<\/h3>\n<p>To immediately stop the <strong>MongoDB <\/strong>service on the Debian system, run the provided command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> systemctl stop mongod<\/div><\/div>\n<h3><a id=\"post-26135-_pqmo91wff2t7\"><\/a>Restart MongoDB Service<\/h3>\n<p>You can also restart the <strong>MongoDB <\/strong>service as below:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> systemctl restart mongod<\/div><\/div>\n<h3><a id=\"post-26135-_qxs9ssfksfv2\"><\/a>Display Status of MongoDB Service<\/h3>\n<p>If you want to view or display the <strong>MongoDB <\/strong>service\u2019s status, you can utilize the below-listed command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> systemctl status mongod<\/div><\/div>\n<p>The output shows the <strong>MongoDB <\/strong>service\u2019s information:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"366\" class=\"wp-image-26145\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-7.png 739w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-7-300x149.png 300w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/p>\n<h2><a id=\"post-26135-_5oswilfo8mpi\"><\/a>How to Use MongoDB on Debian 12<\/h2>\n<p>You can use <strong>MongoDB <\/strong>through its command-line interface i.e. MongoDB shell. For this purpose, you must connect to the <strong>MongoDB <\/strong>server and access the <strong>MongoDB <\/strong>shell. Then, you will be able to perform various operations, such as viewing, creating, and using MongoDB databases, managing collections, and many more. Go through the following steps to use <strong>MongoDB <\/strong>on Debian 12:<\/p>\n<h3><a id=\"post-26135-_slgv6nooxbgt\"><\/a>Connect to the MongoDB Server<\/h3>\n<p>To interact with <strong>MongoDB<\/strong>, connect to the <strong>MongoDB <\/strong>server using the provided command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">mongosh<\/div><\/div>\n<p>As you can see, the <strong>MongoDB <\/strong>shell has been started:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"783\" height=\"526\" class=\"wp-image-26148\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-8.png 783w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-8-300x202.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-8-768x516.png 768w\" sizes=\"auto, (max-width: 783px) 100vw, 783px\" \/><\/p>\n<h3><a id=\"post-26135-_v0d8o1fjmswf\"><\/a>Display All MongoDB Databases<\/h3>\n<p>To display all the available <strong>MongoDB <\/strong>databases, enter the given command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">show dbs<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"168\" class=\"wp-image-26149\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-9.png 765w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-9-300x66.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/p>\n<h3><a id=\"post-26135-_s4wxupaxfdvb\"><\/a>Switch or Create a Database<\/h3>\n<p>If you want to create a new <strong>MongoDB <\/strong>database or switch to the specific existing database, execute the \u201c<strong>use &lt;database-name&gt;<\/strong>\u201d command and specify the desired database name:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">use DebianDB<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"131\" class=\"wp-image-26150\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-10.png 765w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-10-300x51.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/p>\n<h3><a id=\"post-26135-_d79urzb8y8xm\"><\/a>Create MongoDB Collection (Table)<\/h3>\n<p>To create a <strong>MongoDB <\/strong>collection (table) in the database, utilize the \u201c<strong>db.createCollection(&#8220;&lt;collection-name&gt;&#8221;)<\/strong>\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">db.createCollection<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;TechAuthors&quot;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"766\" height=\"134\" class=\"wp-image-26151\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-11.png 766w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-11-300x52.png 300w\" sizes=\"auto, (max-width: 766px) 100vw, 766px\" \/><\/p>\n<h3><a id=\"post-26135-_gr1q4qsq0ti9\"><\/a>Show All Collections (Tables)<\/h3>\n<p>You can display all the <strong>MongoDB <\/strong>collections via the below \u201c<strong>show<\/strong>\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">show collections<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"155\" class=\"wp-image-26152\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-12.png 765w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-12-300x61.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/p>\n<h3><a id=\"post-26135-_34p301t8k9da\"><\/a>Insert Value in MongoDB Collection<\/h3>\n<p>If you want to insert values in the specific <strong>MongoDB <\/strong>collection\/table, you can do it as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">db.TechAuthors.insert<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#123;<\/span> <span class=\"st_h\">'name'<\/span>: <span class=\"st_h\">'John'<\/span>, <span class=\"st_h\">'category'<\/span>: <span class=\"st_h\">'Linux'<\/span>, <span class=\"st_h\">'experience'<\/span>: <span class=\"nu0\">6<\/span><span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"250\" class=\"wp-image-26153\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-13.png 765w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-13-300x98.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/p>\n<h3><a id=\"post-26135-_51761i1vhs37\"><\/a>Display MongoDB Collection\u2019s Data<\/h3>\n<p>To display all the data of the <strong>MongoDB <\/strong>collection, type out the given command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">db.TechAuthors.find<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"392\" class=\"wp-image-26160\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-14.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-14.png 765w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-14-300x154.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/p>\n<h3><a id=\"post-26135-_a0v84qc2v6l2\"><\/a>Delete Data From the MongoDB Collection<\/h3>\n<p>If you want to delete the specific data of the <strong>MongoDB <\/strong>collection, utilize the \u201c<strong>db.TechAuthors.remove()<\/strong>\u201d command and specify the required condition:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">db.TechAuthors.remove<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#123;<\/span>name: <span class=\"st_h\">'John'<\/span><span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"125\" class=\"wp-image-26162\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-15.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-15.png 765w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-15-300x49.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/p>\n<h3><a id=\"post-26135-_3qd1anqeiuxa\"><\/a>Delete MongoDB Database<\/h3>\n<p>To delete the current <strong>MongoDB <\/strong>database, write out the following command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">db.dropDatabase<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"122\" class=\"wp-image-26164\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-16.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-16.png 765w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-16-300x48.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/p>\n<h3><a id=\"post-26135-_wizpr1g4933j\"><\/a>Exit MongoDB Shell<\/h3>\n<p>You can close the current <strong>MongoDB <\/strong>shell session as:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">quit<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"116\" class=\"wp-image-26166\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-17.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-17.png 765w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-17-300x45.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/p>\n<h2><a id=\"post-26135-_qbmvdlqzetao\"><\/a>How to Configure MongoDB Security on Debian 12<\/h2>\n<p>By default, <strong>MongoDB <\/strong>doesn\u2019t have authentication enabled which means anyone can access it and connect to the server without a password. Therefore, you should configure your <strong>MongoDB <\/strong>security with the username and password on your Debian system to protect your data, prevent unauthorized access, and maintain privacy.<\/p>\n<p>Here are the steps to configure <strong>MongoDB <\/strong>security on your system:<\/p>\n<h3><a id=\"post-26135-_4q3v9y5yenyh\"><\/a>Step 1: Enable Authorization<\/h3>\n<p>First, open the <strong>MongoDB <\/strong>configuration file i.e., \u201c<strong>\/etc\/mongod.conf<\/strong>\u201d with sudo privileges:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">nano<\/span> <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>mongod.conf<\/div><\/div>\n<p>In the \u201c<strong>\/etc\/mongod.conf<\/strong>\u201d file, find the line that starts with \u201c<strong>#security<\/strong>\u201d and uncommit it by removing the \u201c<strong>#<\/strong>\u201d with it. Then, add the provided line below it:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">authorization: enabled<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"763\" height=\"403\" class=\"wp-image-26168\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-18.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-18.png 763w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-18-300x158.png 300w\" sizes=\"auto, (max-width: 763px) 100vw, 763px\" \/><\/p>\n<p>Save the file and close the nano editor by pressing \u201c<strong>CTRL + S<\/strong>\u201d and \u201c<strong>CTRL + X<\/strong>\u201d respectively.<\/p>\n<h3><a id=\"post-26135-_pk1kb16y8t8n\"><\/a>Step 2: Restart MongoDB Service<\/h3>\n<p>After that, you need to restart the <strong>MongoDB <\/strong>service to implement changes:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> systemctl restart mongod<\/div><\/div>\n<h3><a id=\"post-26135-_xp2tr12gigj3\"><\/a>Step 3: Access MongoDB Shell<\/h3>\n<p>Then, connect to the <strong>MongoDB <\/strong>server and access the <strong>MongoDB <\/strong>shell through the below-listed command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">mongosh<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"766\" height=\"260\" class=\"wp-image-26171\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-19.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-19.png 766w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-19-300x102.png 300w\" sizes=\"auto, (max-width: 766px) 100vw, 766px\" \/><\/p>\n<h3><a id=\"post-26135-_hk8uepalgq2l\"><\/a>Step 4: Switch to Admin Database<\/h3>\n<p>Now, switch to the \u201c<strong>admin<\/strong>\u201d database via the \u201c<strong>use<\/strong>\u201d command for creating a new user:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">use admin<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"765\" height=\"123\" class=\"wp-image-26172\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-20.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-20.png 765w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-20-300x48.png 300w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/p>\n<h3><a id=\"post-26135-_xz81dornt68k\"><\/a>Step 5: Create a New User<\/h3>\n<p>To create a new user in the database, utilize the \u201c<strong>db.createUser()<\/strong>\u201d command and specify the username, password, and its role as seen below:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">db.createUser<span class=\"br0\">&#40;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\nuser: <span class=\"st0\">&quot;LWadmin&quot;<\/span>,<br \/>\npwd: <span class=\"st0\">&quot;pass123&quot;<\/span>,<br \/>\nroles: <span class=\"br0\">&#91;<\/span> <span class=\"br0\">&#123;<\/span> role: <span class=\"st0\">&quot;userAdminAnyDatabase&quot;<\/span>, db: <span class=\"st0\">&quot;admin&quot;<\/span> <span class=\"br0\">&#125;<\/span> <span class=\"br0\">&#93;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#41;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"766\" height=\"254\" class=\"wp-image-26173\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-21.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-21.png 766w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-21-300x99.png 300w\" sizes=\"auto, (max-width: 766px) 100vw, 766px\" \/><\/p>\n<h3><a id=\"post-26135-_r59u19f1vtyw\"><\/a>Step 6: Close MongoDB Shell<\/h3>\n<p>Next, use the provided command to exit the <strong>MongoDB <\/strong>Shell:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">quit<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/div>\n<h3><a id=\"post-26135-_7035hmrmvu8m\"><\/a>Step 7: Test MongoDB Authentication<\/h3>\n<p>Finally, connect to the <strong>MongoDB <\/strong>with the new user to ensure its authentication. After executing the below command, you will be asked to enter the password. Enter the desired password that you have set for this user:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\">mongo <span class=\"re5\">--port<\/span> <span class=\"nu0\">27017<\/span> <span class=\"re5\">--authenticationDatabase<\/span> <span class=\"st0\">&quot;admin&quot;<\/span> <span class=\"re5\">-u<\/span> <span class=\"st0\">&quot;LWadmin&quot;<\/span> <span class=\"re5\">-p<\/span><\/div><\/div>\n<p>The output indicates that we have successfully connected to the <strong>MongoDB <\/strong>server with the new username and password:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"312\" class=\"wp-image-26174\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-22.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-22.png 782w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-22-300x120.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-22-768x306.png 768w\" sizes=\"auto, (max-width: 782px) 100vw, 782px\" \/><\/p>\n<h2><a id=\"post-26135-_j56hyjjrpi2d\"><\/a>How to Uninstall MongoDB on Debian 12<\/h2>\n<p>If you do not want to use <strong>MongoDB <\/strong>on the Debian 12 system, you can completely remove\/uninstall it via the given command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt autoremove mongodb-org <span class=\"re5\">-y<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"780\" height=\"406\" class=\"wp-image-26176\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-23.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-23.png 780w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-23-300x156.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-23-768x400.png 768w\" sizes=\"auto, (max-width: 780px) 100vw, 780px\" \/><\/p>\n<p>You should also remove the added GPG key and <strong>MongoDB <\/strong>repository from your Debian 12 system:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> <span class=\"kw2\">rm<\/span> <span class=\"sy0\">\/<\/span>etc<span class=\"sy0\">\/<\/span>apt<span class=\"sy0\">\/<\/span>sources.list.d<span class=\"sy0\">\/<\/span>mongodb-org-<span class=\"nu0\">7.0<\/span>.list <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw2\">sudo<\/span> <span class=\"kw2\">rm<\/span> <span class=\"sy0\">\/<\/span>usr<span class=\"sy0\">\/<\/span>share<span class=\"sy0\">\/<\/span>keyrings<span class=\"sy0\">\/<\/span>mongodb-server-<span class=\"nu0\">7.0<\/span>.gpg<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"785\" height=\"111\" class=\"wp-image-26178\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-24.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-24.png 785w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-24-300x42.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2024\/02\/word-image-26135-24-768x109.png 768w\" sizes=\"auto, (max-width: 785px) 100vw, 785px\" \/><\/p>\n<p>That was all about installing and managing MongoDB on Debian 12.<\/p>\n<p><strong>Note:<\/strong> You can also check out the <a href=\"https:\/\/www.mongodb.com\/docs\/\">official MongoDB documentation<\/a> for more details.<\/p>\n<h2><a id=\"post-26135-_91o02thkz0j\"><\/a>Sum Up<\/h2>\n<p>To install the latest version of <strong>MongoDB <\/strong>on Debian 12, add the official <strong>MongoDB <\/strong>repository to your Debian system. You will get the up-to-date version of <strong>MongoDB <\/strong>directly from the source with all the latest features and updates. You can use <strong>MongoDB <\/strong>for creating and managing databases, documents, and collections\/tables. This guide has explained the method to install, manage, use, manage as well as remove <strong>MongoDB <\/strong>on Debian 12.<\/p>","protected":false},"excerpt":{"rendered":"<p>You can install the latest version of MongoDB on your Debian 12 system from the official MongoDB repository by adding it to your system.<\/p>","protected":false},"author":112,"featured_media":26179,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-26135","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-debian"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/26135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/112"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=26135"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/26135\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/26179"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=26135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=26135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=26135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}