<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://abresting.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://abresting.github.io/" rel="alternate" type="text/html" /><updated>2025-02-18T12:36:32-08:00</updated><id>https://abresting.github.io/feed.xml</id><title type="html">Abhimanyu Rawat</title><subtitle></subtitle><author><name>Abhimanyu Rawat</name><email>work.Abhimanyu@gmail.com</email></author><entry><title type="html">MongoDB-add-user-and-auth</title><link href="https://abresting.github.io/posts/2021/MongoDB-add-user-and-auth/" rel="alternate" type="text/html" title="MongoDB-add-user-and-auth" /><published>2021-09-05T00:00:00-07:00</published><updated>2021-09-05T00:00:00-07:00</updated><id>https://abresting.github.io/posts/2021/MongoDB-add-user-and-auth</id><content type="html" xml:base="https://abresting.github.io/posts/2021/MongoDB-add-user-and-auth/"><![CDATA[<h1 id="about-mongodb">About MongoDB</h1>

<p>MongoDB is a persistent data storage solution where users/applications store data. It falls under NoSQL category i.e. instead of traditional table format used by SQL databases it follows JSON notation to store data. It provides more felxibility than SQL based databases.
So be it user data, images, binaries, one can store almost anything is MongoDB.<br /></p>

<p>Mostly MongoDB is installed on the local system in the form of binaries. There are various sources via which users can install MongoDB on their machines. For Ubuntu <a href="https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/">follow the guide</a>.</p>

<p>Databases in general should be accessed using credentials and not the open access that everyone who knows your IP and port number can get access. Best practise is to add a user and start MongoDB with authentication mode on.</p>

<h1 id="how-to-add-users-in-local-mongodb-installation">How to add users in local MongoDB installation</h1>

<p>This tutorial is a quick guide to add a user so that the MongoDB can be accessed using credentials.</p>

<pre><code class="language-Ruby"># first go inside mongodb
$ mongo

# create a user and password along with a role, e.g. username as "superuser" and password as "password"
# However it is adviced to not only use a stronger password but a complex username as well.
$ db.createUser( { user: "superuser", pwd: "password", roles: ["readWriteAnyDatabase" ] } )

</code></pre>

<p>Upon successful exectuion of above commands, now makes changes in the MongoDB config files to let only authenticated users use MongoDB. Usually, the config file is present in a standard location in Ubuntu(linux based machines) i.e.</p>

<p><code class="language-plaintext highlighter-rouge">/etc/mongodb.conf</code></p>

<p>Open this file in editor and find the tag with the name <code class="language-plaintext highlighter-rouge">auth</code> and set it to <code class="language-plaintext highlighter-rouge">true</code>:</p>

<pre><code class="language-Ruby">$ vim /etc/mongodb.conf

</code></pre>

<p>It should look like the following:</p>
<pre><code class="language-Text">  # Turn on/off security.  Off is currently the default
  auth = true
</code></pre>

<p>Now, restart the MongoDB instance:</p>

<pre><code class="language-Ruby">$ systemctl restart mongodb.service

</code></pre>

<p>Also please check if the restart has been successful:</p>

<pre><code class="language-Ruby">$ systemctl status mongodb.service

</code></pre>
<p>If it shows <code class="language-plaintext highlighter-rouge">Active: active (running)</code> in the output then proceed to the final step.</p>

<h1 id="authenticate-using-the-credentials">Authenticate using the credentials:</h1>

<p>Now use the following command from the terminal to make sure that the credentials are working:</p>

<pre><code class="language-Ruby">$ mongo --port 27017 --host 127.0.0.1 -u "superuser" -p "password" --authenticationDatabase "admin"

</code></pre>

<p>If everything goes perfect then following will be displayed:</p>
<pre><code class="language-Text">MongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017/
Implicit session: session { "id" : UUID("e949c438-d914-4067-8762-a28b3bc88d3d") }
MongoDB server version: 3.6.8

</code></pre>

<p>happy hacking!</p>]]></content><author><name>Abhimanyu Rawat</name><email>work.Abhimanyu@gmail.com</email></author><category term="MongoDB" /><category term="NoSQL" /><category term="Database" /><category term="Setup" /><category term="auth" /><category term="Ubuntu" /><summary type="html"><![CDATA[About MongoDB]]></summary></entry><entry><title type="html">MongoDB-QuickSetup</title><link href="https://abresting.github.io/posts/2021/MongoDB-QuickSetup/" rel="alternate" type="text/html" title="MongoDB-QuickSetup" /><published>2021-08-25T00:00:00-07:00</published><updated>2021-08-25T00:00:00-07:00</updated><id>https://abresting.github.io/posts/2021/MongoDB-QuickSetup</id><content type="html" xml:base="https://abresting.github.io/posts/2021/MongoDB-QuickSetup/"><![CDATA[<h1 id="about-mongodb">About MongoDB</h1>

<p>MongoDB is a persistent data storage solution where users/applications store data. It falls under NoSQL category i.e. instead of traditional table format used by SQL databases it follows JSON notation to store data. It provides more felxibility than SQL based databases.
So be it user data, images, binaries, one can store almost anything is MongoDB.<br /></p>

<p>This tutorial is a quick guide to setup a MongoDB instance.</p>

<p>Either one can install the MongoDB binaries locally, which obviously takes a little more time and might get conflict with any existing MongoDB installation, or, one can avoid all that mess by starting MongoDB instance inside a docker container which takes less time and easier to maintain. So, let’s learn how to deploy a MongoDB container in less than 2 min.</p>

<h2 id="step-1---install-docker">Step 1 - Install Docker</h2>

<p>It is highly recommended to install docker from the official <a href="https://docs.docker.com/engine/install/">Docker website</a>.</p>

<p>Also, docker-compose is needed in order to make sure you are doing things neat and earier to debug in case of an error. <a href="https://docs.docker.com/compose/install/">Install docker-compose</a>.</p>

<h2 id="step-2---create-an-installation-directory">Step 2 - Create an installation directory</h2>

<p>It is a <strong>good practise</strong> to create a directory where everything related to the MongoDB you are about to use should be put.</p>

<pre><code class="language-Ruby">$ mkdir mongo

# create a volume to store the MongoDB data from container
$ mkdir mongodb_data

# now enter the created directory
$ cd mongo
</code></pre>

<h4 id="create-docker-compose-file">Create docker-compose file:</h4>

<p>To quickly setup a MongoDB instance inside a container there are a few things you need to take under consideration:</p>

<ul>
  <li>Requiring user authentication or not?</li>
  <li>Attach a local volume to get the data from inside your MongoDB container?</li>
  <li>Choosing appropriate port, standard port is 27017 but you can choose your own as well</li>
</ul>

<p>Code to start the MongoDB container should be put inside a docker-compose.yml file.</p>

<p>Assume you <strong>want authentication based MongoDB access</strong> running on port number 27017 with a volume on the host machine to get the MongoDB data, you can use the following template:</p>

<pre><code class="language-Ruby">version: '3.8'

volumes:
        mongodb_data:
services:
      mongodb:
        image: mongo
        container_name: mongodb
        restart: always
        volumes:
                - 'mongodb_data:/data/db'
        environment:
          MONGO_INITDB_ROOT_USERNAME: 'superuser'
          MONGO_INITDB_ROOT_PASSWORD: 'password'
          MONGO_INITDB_DATABASE: mongodb
        ports:
          - 27017:27017
</code></pre>

<p>In the snippet above you can choose replace the environment variables such as username, password, database etc. with the credentials of your choice.</p>

<p>If you want a MongoDB instance <strong>without autentication requirements</strong>, simple remove the environment variable and 3 following lines under that. Now new code will look like this:</p>

<pre><code class="language-Ruby">version: '3.8'

volumes:
        mongodb_data:
services:
      mongodb:
        image: mongo
        container_name: mongodb
        restart: always
        volumes:
                - 'mongodb_data:/data/db'
        ports:
          - 27017:27017
</code></pre>

<h2 id="step-3---deploying-mongodb-container">Step 3-  Deploying MongoDB container</h2>

<p>To deploy the container containing the MongoDb instance, use the following command:</p>

<pre><code class="language-Ruby">$ docker-compose up -d --build
Creating network "mongo_default" with the default driver
Creating mongodb ... done

</code></pre>

<p>This will spawn a docker container running MongoDB service which can be accessed by the user.
To verify if the container is up and running user the following command:</p>

<pre><code class="language-Ruby">$ docker ps

</code></pre>

<p>If you see output something like the follwoing then it means the MongoDB container is up and running:</p>

<div class="language-text highlighter-rouge"><div class="highlight"><pre class="highlight"><code>CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                           NAMES
4bb7211b38b1   mongo     "docker-entrypoint.s…"   8 minutes ago   Up 8 minutes   0.0.0.0:27017-&gt;27017/tcp, :::27017-&gt;27017/tcp   mongodb

</code></pre></div></div>
<p>If you don’t see such output then something went wrong while deploying the container. If you see any error then, Well in that case probably google it or ask me, I will be happy to help!</p>

<h2 id="step-4---stoping-the-mongodb-container">Step 4 - Stoping the MongoDB container</h2>

<p>To stop the container use the following command from the same directory where the original docker-compose.yml file is present.</p>

<pre><code class="language-Ruby">$ docker-compose down -v

</code></pre>

<p>Note using the command above will not only stop the MongoDB container but will also remove it from the system so MongoDB specific logs which shows what was going inside the MongoDB container will be inaccessible. <strong>BUT</strong> It will not remove any data generated by MongoDB from your mongodb_data directory.</p>

<h2 id="step-5--debugging-the-mongodb-container">Step 5- Debugging the MongoDB container</h2>

<p>If you want to see what is going inside the container then you can use the <code class="language-plaintext highlighter-rouge">docker logs</code> feature. To use it following command will help:</p>

<pre><code class="language-Ruby"># after logs it is the container id, you will get from the `docker ps` command as mentioned above
$ docker logs 4bb7211b38b1

</code></pre>

<p><code class="language-plaintext highlighter-rouge">docker logs</code>  command can also be used on stopped containers, you just need to get its container id, by using <code class="language-plaintext highlighter-rouge">docker ps -a</code> command if the container was not removed manually or using the docker-compose down command as mentioned in the previous section.</p>

<h2 id="using-your-application">Using your application</h2>

<p>Now you can use any application that require a MongoDB instance running on port 27017 like it was installed on your local machine. The experience will be very smooth and easier to manage.</p>

<p>happy hacking!</p>]]></content><author><name>Abhimanyu Rawat</name><email>work.Abhimanyu@gmail.com</email></author><category term="MongoDB" /><category term="NoSQL" /><category term="Database" /><category term="Setup" /><category term="Docker" /><category term="Container" /><summary type="html"><![CDATA[About MongoDB]]></summary></entry><entry><title type="html">PostgreSQL-Comp-table</title><link href="https://abresting.github.io/posts/2019/PostgreSQL-comp-table/" rel="alternate" type="text/html" title="PostgreSQL-Comp-table" /><published>2019-06-02T00:00:00-07:00</published><updated>2019-06-02T00:00:00-07:00</updated><id>https://abresting.github.io/posts/2019/PostgreSQL-comp-table</id><content type="html" xml:base="https://abresting.github.io/posts/2019/PostgreSQL-comp-table/"><![CDATA[<p>Comparison table of the different PostgreSQL backup tools:<br /></p>

<table>
  <thead>
    <tr>
      <th>Tool Name</th>
      <th>version Number</th>
      <th>Pros</th>
      <th>Cons</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Barman</td>
      <td>2.8 May 2019</td>
      <td>demo Text</td>
      <td>demo Text</td>
    </tr>
    <tr>
      <td>Amanda</td>
      <td> </td>
      <td>demo Text</td>
      <td>demo Text</td>
    </tr>
    <tr>
      <td>Bacula</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Handy Backup</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Iperius Backup</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Backup and Recovery  Manager for PostgreSQL</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>NetVault Backup</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>pgBackRest</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Simpana</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Spectrum Protect</td>
      <td> </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Manuals</td>
      <td> </td>
      <td>demo Text</td>
      <td>demo Text</td>
    </tr>
  </tbody>
</table>]]></content><author><name>Abhimanyu Rawat</name><email>work.Abhimanyu@gmail.com</email></author><category term="PostgreSQL" /><category term="SQL" /><category term="Database" /><category term="Setup" /><summary type="html"><![CDATA[Comparison table of the different PostgreSQL backup tools:]]></summary></entry><entry><title type="html">PostgreSQL-Setup</title><link href="https://abresting.github.io/posts/2019/PostgreSQL-setup/" rel="alternate" type="text/html" title="PostgreSQL-Setup" /><published>2019-06-02T00:00:00-07:00</published><updated>2019-06-02T00:00:00-07:00</updated><id>https://abresting.github.io/posts/2019/PostgreSQL-setup</id><content type="html" xml:base="https://abresting.github.io/posts/2019/PostgreSQL-setup/"><![CDATA[<h1 id="about-postgresql">About PostgreSQL</h1>

<p>In the world of SQL or RDBMS based databases, PostgreSQL offers great services which can be used by an individual user to large scale enterprises. Similar to most of the SQL based databases, setting it up is fairly simple, and it provides users a great experience and flexibility to meet your goals.<br /></p>

<p>To understand the PostgreSQL and how it suffice your use case, setting it up is an essential step. This post demonstrates how to install PostgreSQL on an Ubuntu 16.04 machine along with basic database administration with a running use case(small app).<br /></p>

<h2 id="step-1---install-postgresql">Step 1 - Install PostgreSQL</h2>

<p>The most recommended method is to install it via the <code class="language-plaintext highlighter-rouge">apt</code> packing manager.</p>

<pre><code class="language-Ruby">user@foo:~$ sudo apt update
user@foo:~$ sudo apt install postgresql postgresql-contrib

</code></pre>

<p>After the above installation, automatically a user and a database named as <strong>postgres</strong> have been added to the machine and PostgreSQL service, it is an administration account. Now, you can use this administration user account to access the PostgreSQL related functions and roles. <a href="http://www.postgresqltutorial.com/postgresql-roles/" target="_blank">Roles</a> are nothing but the different types of functions you can perform in the PostgreSQL database.</p>

<p>There are a few ways to use <strong>postgres</strong> account to access PostgreSQL shell.</p>

<h2 id="step-2---enter-the-postgresql-shell">Step 2 - Enter the PostgreSQL shell</h2>

<p>To switch over to the <strong>postgres</strong> account run the following command:</p>

<pre><code class="language-Ruby">user@foo:~$ sudo -i -u postgres
postgres@foo:~$
</code></pre>
<p>Now you have successfully entered in the <strong>postgres</strong> account, to access the PostgreSQL process shell run the following command:</p>

<pre><code class="language-Ruby">postgres@foo:~$ psql
postgres=#

</code></pre>
<h3 id="or-you-can-directly-enter-in-the-postgres-psql-shell-by">Or you can directly enter in the <strong>postgres</strong> psql shell by:</h3>

<pre><code class="language-Ruby">user@foo:~$ sudo -i -u postgres psql
postgres=#

</code></pre>
<p>Now you are free to interact with the database management services.</p>

<p>Exit out of the PostgreSQL shell by:</p>

<pre><code class="language-Ruby">postgres=# \q
user@foo:~$

</code></pre>

<h2 id="step-3---creating-a-role">Step 3 - Creating a role</h2>

<p>In PostgreSQL, a role/user is required so that it can interact with the PostgreSQL database service via as 3rd party standalone apps, PHP web application, Python scripts, etc.</p>

<p>A very simple way of creating a user is from <strong>postgres</strong> account:</p>

<pre><code class="language-Ruby">postgres@foo:~$ createuser -P -s -e demopy
Enter password for new role: 
Enter it again: 
SELECT pg_catalog.set_config('search_path', '', false)
CREATE ROLE demopy PASSWORD 'md516a97ee203992bfc3ab84a77b340de86' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;

</code></pre>

<p>The above script has automatically made the role/user you created with superuser privileges. Select a password and remember, and it will be feed to your application that interacts with the PostgreSQL database on your behalf.</p>

<h2 id="step-4---creating-a-database">Step 4 - Creating a Database</h2>

<p>Now, very quickly, you can create a database to play around with an application. From your <strong>postgres</strong> account type the following command:</p>

<pre><code class="language-Ruby">postgres@foo:~$ createdb demopy

</code></pre>

<h2 id="step-5---accessing-the-database">Step 5 - Accessing the database</h2>

<p>Access of the newly created PostgreSQL database <strong>demopy</strong> can be done in two ways:</p>

<ol>
  <li>Creating a unix user with the same name as role and database(i.e. demopy) so that through terminal prompt you can access the PostgreSQL services the same way user <strong>postgres</strong> does.</li>
</ol>

<pre><code class="language-Ruby">user@foo:~$ sudo adduser demopy

</code></pre>

<p>Note: the password of the unix user can be different of the password of the role, as during the access of the database through a 3rd party app or plugin the role password will be used not the unix user local account password.</p>

<p>Now to access the database using the following command:</p>

<pre><code class="language-Ruby">user@foo:~$ sudo -i -u demopy
demopy@foo:~$ psql
demopy=#

</code></pre>
<p><code class="language-plaintext highlighter-rouge">pydemo=#</code> is the shell through which the database named demopy can be accessed.</p>

<ol>
  <li>Any database can be accessed via the default user <strong>postgres</strong> account, without the need of making a separate unix user local account:</li>
</ol>

<p>Once you enter the PostgreSQL using the <strong>postgres</strong> account, by default it selects the <em>postgres</em> database:</p>

<pre><code class="language-Ruby">postgres@foo:~$ psql
postgres=#

</code></pre>

<p>You can switch to any database present inside the PostgreSQL service using the command <br /> <code class="language-plaintext highlighter-rouge">\c &lt;name_of_the_database&gt;</code></p>

<pre><code class="language-Ruby">postgres=# \c demopy
You are now connected to database "demopy" as user "postgres".
demopy=#

</code></pre>

<p>Database admins mostly advise step 1.</p>

<h2 id="step-6---create-and-deleting-tables">Step 6 - Create and Deleting Tables</h2>

<p>In the database, Tables are of a defined structure according to which the data is stored. After selecting a database to work on creating a table:</p>

<pre><code class="language-Ruby">demopy=# CREATE TABLE test (
            id_number varchar(50) PRIMARY KEY, 
            name varchar(100) NOT NULL, 
            city varchar(50)
    );


</code></pre>

<p>After this fill database entries for demonstration purpose</p>
<pre><code class="language-Ruby">
demopy=# INSERT INTO test (id_number, name, city) VALUES ('1', 'abc', 'JFK');
demopy=# INSERT INTO test (name, id_number) VALUES ('xyz', '2');


</code></pre>

<p>Run a query against the database to check if the data exists inside the database.</p>

<pre><code class="language-Ruby">
demopy=# SELECT * FROM test;

 id_number | name | city 
-----------+------+------
 1         | abc  | JFK
 2         | xyz  | 
(2 rows)

</code></pre>

<p>You can perform many queries/operations over the database, more queries can be found <a href="https://www.tutorialspoint.com/postgresql/postgresql_update_query.htm" target="_blank">here</a></p>

<h2 id="running-an-application">Running an Application</h2>

<p>We can query the database via 3rd party applications, any web plugin or script, etc. In this example, a simple python script will be used to query the database.</p>

<pre><code class="language-Python">#!/usr/bin/python

import pg

conn = pg.DB(host="localhost", user="demopy", passwd="PASSWORD", dbname="demopy")

result = conn.query("SELECT * FROM test")

print result.getresult()

conn.close()


</code></pre>

<p>Output:
<code class="language-plaintext highlighter-rouge">[('1', 'abc', 'JFK'), ('2', 'xyz', None)]</code></p>]]></content><author><name>Abhimanyu Rawat</name><email>work.Abhimanyu@gmail.com</email></author><category term="PostgreSQL" /><category term="SQL" /><category term="Database" /><category term="Setup" /><summary type="html"><![CDATA[About PostgreSQL]]></summary></entry></feed>