<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Database Archives - ServerStadium</title>
	<atom:link href="https://serverstadium.com/knowledge-base/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>https://serverstadium.com/knowledge-base/category/database/</link>
	<description>Public Cloud Solution by ServerStadium</description>
	<lastBuildDate>Thu, 27 Apr 2023 09:18:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.4</generator>

<image>
	<url>https://serverstadium.com/wp-content/uploads/2023/04/cropped-favicon-1-32x32.png</url>
	<title>Database Archives - ServerStadium</title>
	<link>https://serverstadium.com/knowledge-base/category/database/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Set Up High Availability PostgreSQL Cluster Using Patroni on ServerStadium</title>
		<link>https://serverstadium.com/knowledge-base/set-up-high-availability-postgresql-cluster-using-patroni-on-serverstadium/</link>
		
		<dc:creator><![CDATA[Brian K]]></dc:creator>
		<pubDate>Wed, 06 Jul 2022 07:40:20 +0000</pubDate>
				<guid isPermaLink="false">https://cloudraya.com/knowledge-base/?p=2096</guid>

					<description><![CDATA[<p>PostgreSQL is a powerful OpenSource, an object-relational database management system that extends the SQL language with many features. Meanwhile, Patroni is an OpenSource python package that we can use to manage and automate the deployment of PostgreSQL HA Clusters. Little information, Patroni was developed by Zalando. Prerequisite To follow this tutorial, we need three VM&#8230;</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/set-up-high-availability-postgresql-cluster-using-patroni-on-serverstadium/">Set Up High Availability PostgreSQL Cluster Using Patroni on ServerStadium</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">PostgreSQL is a powerful OpenSource, an object-relational database management system that extends the SQL language with many features.</p>



<p class="wp-block-paragraph">Meanwhile, Patroni is an OpenSource python package that we can use to manage and automate the deployment of PostgreSQL HA Clusters. Little information, Patroni was developed by Zalando.</p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Prerequisite</strong></h2>



<p class="wp-block-paragraph">To follow this tutorial, we need three VM for PostgreSQL and one VM for HAproxy and etcd.<br>Below is four VMs that we will deploy in ServerStadium.</p>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:100%">
<figure class="wp-block-table"><table><tbody><tr><td class="has-text-align-center" data-align="center"><strong>Hostname</strong></td><td class="has-text-align-center" data-align="center"><strong>IP Address</strong></td><td class="has-text-align-left" data-align="left"><strong>Applications</strong></td></tr><tr><td class="has-text-align-center" data-align="center">haetcd</td><td class="has-text-align-center" data-align="center">199.180.130.99</td><td class="has-text-align-left" data-align="left">HAproxy</td></tr><tr><td class="has-text-align-center" data-align="center"></td><td class="has-text-align-center" data-align="center">10.3.3.166</td><td class="has-text-align-left" data-align="left">etcd</td></tr><tr><td class="has-text-align-center" data-align="center">pgsql1</td><td class="has-text-align-center" data-align="center">10.3.3.12</td><td class="has-text-align-left" data-align="left">patroni</td></tr><tr><td class="has-text-align-center" data-align="center">pgsql2</td><td class="has-text-align-center" data-align="center">10.3.3.71</td><td class="has-text-align-left" data-align="left">patroni</td></tr><tr><td class="has-text-align-center" data-align="center">pgsql3</td><td class="has-text-align-center" data-align="center">10.3.3.70</td><td class="has-text-align-left" data-align="left">patroni</td></tr></tbody></table></figure>
</div>
</div>



<p class="wp-block-paragraph">We will use haetcd as jumpbox server since the VM has Public IP Address that we can access from the outside network. In /etc/host, insert the hostname of the servers to make us easy to access the server without remembering the IP Address.</p>



<p class="wp-block-paragraph">/etc/hosts</p>



<figure class="wp-block-image size-full"><img decoding="async" width="284" height="144" src="https://serverstadium.com/wp-content/uploads/2022/05/image.png" alt="" class="wp-image-2098"/></figure>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Install PostgreSQL 14</strong></h2>



<p class="wp-block-paragraph">In this step, we will use PostgreSQL14 for pgsql1, pgsql2, and pgsql3. Please the command below on each server.</p>



<pre class="wp-block-preformatted"><code>sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" &gt; /etc/apt/sources.list.d/pgdg.list'</code>
<code>
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

apt update</code></pre>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1157" height="260" src="https://serverstadium.com/wp-content/uploads/2022/05/image-2.png" alt="" class="wp-image-2100" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-2.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-2-300x67.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-2-1024x230.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-2-768x173.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<pre class="wp-block-preformatted">apt -y install postgresql-14 postgresql-server-dev-14</pre>



<figure class="wp-block-image size-full"><img decoding="async" width="1157" height="131" src="https://serverstadium.com/wp-content/uploads/2022/05/image-3.png" alt="" class="wp-image-2101" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-3.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-3-300x34.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-3-1024x116.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-3-768x87.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph">After the postgresql installation is completed, we need to stop and disable PostgreSQL service. Then we will continue to install patroni.</p>



<p class="wp-block-paragraph"> <code>systemctl stop postgresql &amp;&amp; systemctl disable postgresql</code></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Install Patroni</strong></h2>



<p class="wp-block-paragraph">Patroni uses the utilities of PostgreSQL that we have installed previously which is located at <strong>/usr/lib/postgresql/versin/bin </strong>directory.</p>



<p class="wp-block-paragraph">In this step, we create a symbolic link to PostgreSQL utilities on psql1, psql2, and psql3 </p>



<p class="wp-block-paragraph"><code>ln -s /usr/lib/postgresql/14/bin/* /usr/sbin/ </code></p>



<p class="wp-block-paragraph">Next, we will install patroni on each server</p>



<p class="wp-block-paragraph"><code>apt -y install python3 python3-pip <br>pip install --upgrade setuptools <br>pip install psycopg2 <br>pip install patroni <br>pip install python-etcd</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="340" src="https://serverstadium.com/wp-content/uploads/2022/05/image-4.png" alt="" class="wp-image-2102" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-4.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-4-300x88.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-4-1024x301.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-4-768x226.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph"></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Configure Patroni on Pgsql1, Pgsql2, and Pgsql3</strong></h2>



<p class="wp-block-paragraph">Patroni uses yaml files to store the configuration. We will place the yaml file on /etc directory. </p>



<pre class="wp-block-preformatted">sudo nano /etc/patroni.yml

<strong>pgsql1:</strong>
scope: postgres
namespace: /db/
name: <strong>pgsql1</strong>

restapi:
    listen: <strong>10.3.3.12</strong>:8008
    connect_address: <strong>10.3.3.12</strong>:8008

etcd:
    host:  <strong>10.3.3.166</strong>:2379

bootstrap:
    dcs:
        ttl: 30
        loop_wait: 10
        retry_timeout: 10
        maximum_lag_on_failover: 1048576
        postgresql:
            use_pg_rewind: true

    initdb:
    - encoding: UTF8
    - data-checksums

    pg_hba:
    - host replication replicator 127.0.0.1/32 md5
    - host replication replicator 10.3.3.12/0 md5
    - host replication replicator 10.3.3.71/0 md5
    - host replication replicator 10.3.3.70/0 md5
    - host all all 0.0.0.0/0 md5

    users:
        admin:
            password: admin
            options:
                - createrole
                - createdb

postgresql:
    listen: <strong>10.3.3.12</strong>:5432
    connect_address: <strong>10.3.3.12</strong>:5432
    data_dir: /data/patroni
    pgpass: /tmp/pgpass
    authentication:
        replication:
            username: replicator
            password: replogin321
        superuser:
            username: postgres
            password: secretlogin321
    parameters:
        unix_socket_directories: '.'

tags:
    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false

<strong>pgsql2</strong>
scope: postgres
namespace: /db/
name: <strong>pgsql2</strong>

restapi:
    listen: <strong>10.3.3.71</strong>:8008
    connect_address: <strong>10.3.3.71</strong>:8008

etcd:
    host:  <strong>10.3.3.166</strong>:2379

bootstrap:
    dcs:
        ttl: 30
        loop_wait: 10
        retry_timeout: 10
        maximum_lag_on_failover: 1048576
        postgresql:
            use_pg_rewind: true

    initdb:
    - encoding: UTF8
    - data-checksums

    pg_hba:
    - host replication replicator 127.0.0.1/32 md5
    - host replication replicator 10.3.3.12/0 md5
    - host replication replicator 10.3.3.71/0 md5
    - host replication replicator 10.3.3.70/0 md5
    - host all all 0.0.0.0/0 md5

    users:
        admin:
            password: admin
            options:
                - createrole
                - createdb

postgresql:
    listen: <strong>10.3.3.71</strong>:5432
    connect_address: <strong>10.3.3.71</strong>:5432
    data_dir: /data/patroni
    pgpass: /tmp/pgpass
    authentication:
        replication:
            username: replicator
            password: replogin321
        superuser:
            username: postgres
            password: secretlogin321
    parameters:
        unix_socket_directories: '.'

tags:
    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false

<strong>pgsql3
</strong>scope: postgres
namespace: /db/
name: <strong>pgsql3</strong>

restapi:
    listen: <strong>10.3.3.70</strong>:8008
    connect_address: <strong>10.3.3.70</strong>:8008

etcd:
    host:  10.3.3.166:2379

bootstrap:
    dcs:
        ttl: 30
        loop_wait: 10
        retry_timeout: 10
        maximum_lag_on_failover: 1048576
        postgresql:
            use_pg_rewind: true

    initdb:
    - encoding: UTF8
    - data-checksums

    pg_hba:
    - host replication replicator 127.0.0.1/32 md5
    - host replication replicator 10.3.3.12/0 md5
    - host replication replicator 10.3.3.71/0 md5
    - host replication replicator 10.3.3.70/0 md5
    - host all all 0.0.0.0/0 md5

    users:
        admin:
            password: admin
            options:
                - createrole
                - createdb

postgresql:
    listen: <strong>10.3.3.70</strong>:5432
    connect_address: <strong>10.3.3.70</strong>:5432
    data_dir: /data/patroni
    pgpass: /tmp/pgpass
    authentication:
        replication:
            username: replicator
            password: replogin321
        superuser:
            username: postgres
            password: secretlogin321
    parameters:
        unix_socket_directories: '.'

tags:
    nofailover: false
    noloadbalance: false
    clonefrom: false
    nosync: false
</pre>



<p class="wp-block-paragraph">According to the yml file, the data of PostgreSQL will be stored at /data/patroni. Make sure the ownership of that directory is postgres.</p>



<p class="wp-block-paragraph"><code>mkdir -p /data/patroni</code><br>chown postgres:postgres /data/patroni<br><code>chmod 700 /data/patroni</code></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Create a Service</strong></h2>



<p class="wp-block-paragraph">Next, we will create patroni service that allow us to start and stop the service on each server.</p>



<p class="wp-block-paragraph"><code>sudo nano /etc/systemd/system/patroni.service</code></p>



<p class="wp-block-paragraph">Add the following parameter in the patroni.service<code><br>[Unit]<br>Description=High availability PostgreSQL Cluster<br>After=syslog.target network.target<br>[Service]<br>Type=simple<br>User=postgres<br>Group=postgres<br>ExecStart=/usr/local/bin/patroni /etc/patroni.yml<br>KillMode=process<br>TimeoutSec=30<br>Restart=no<br><br>[Install]<br>WantedBy=multi-user.target</code></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Create and Enable Patroni Service</strong></h2>



<p class="wp-block-paragraph">After the patroni service has been created then we enable it to make it autostart.</p>



<p class="wp-block-paragraph"><code>sudo systemctl daemon-reload<br>sudo systemctl enable patroni</code></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Install Etcd and Haproxy</strong> <strong>on Haetcd VM</strong></h2>



<p class="wp-block-paragraph">etcd is a fault-tolerant, a distributed key value store that can be used to store the state of PostgreSQL cluster.  With etcd, it will keep the PostgreSQL cluster up and running.</p>



<p class="wp-block-paragraph">Run the command below to install etcd</p>



<pre class="wp-block-preformatted">sudo apt -y install etcd</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="156" src="https://serverstadium.com/wp-content/uploads/2022/05/image-5.png" alt="" class="wp-image-2104" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-5.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-5-300x40.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-5-1024x138.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-5-768x104.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph">Next step, we will configure the etcd file. Add the below parameter in the etcd file:</p>



<p class="wp-block-paragraph"><code>sudo nano /etc/default/etcd</code></p>



<p class="wp-block-paragraph"><code>ETCD_LISTEN_PEER_URLS="http://<strong>10.3.3.166</strong>:2380"<br>ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://<strong>10.3.3.166</strong>:2379"<br>ETCD_INITIAL_ADVERTISE_PEER_URLS="http://<strong>10.3.3.166</strong>:2380"<br>ETCD_INITIAL_CLUSTER="default=http://<strong>10.3.3.166</strong>:2380"<br>ETCD_ADVERTISE_CLIENT_URLS="http://<strong>10.3.3.166</strong>:2379"<br>ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"<br>ETCD_INITIAL_CLUSTER_STATE="new"</code><br></p>



<p class="wp-block-paragraph">Restart the etcd service to make a change effect</p>



<p class="wp-block-paragraph"><code>sudo systemctl restart etcd<br></code></p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Start Patroni on Each Pgsql Server</strong></h2>



<p class="wp-block-paragraph">Before we start the patroni service, we need to activate the watchdog support to prevent split-brain on the PostgreSQL<br><br>https://patroni.readthedocs.io/en/latest/watchdog.html<br><br><code>modprobe softdog<br>chown postgres /dev/watchdog</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="705" height="56" src="https://serverstadium.com/wp-content/uploads/2022/05/image-6.png" alt="" class="wp-image-2105" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-6.png 705w, https://serverstadium.com/wp-content/uploads/2022/05/image-6-300x24.png 300w" sizes="(max-width: 705px) 100vw, 705px" /></figure>



<p class="wp-block-paragraph">After the watchdog is activated, now we can start the patroni service again on each server.</p>



<p class="wp-block-paragraph"><code>systemctl start patroni</code><br><code>systemctl status patroni</code></p>



<p class="wp-block-paragraph">pgsql1 as master. <br>We can see, the pgsql1 will act as the PostgreSQL master</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="372" src="https://serverstadium.com/wp-content/uploads/2022/05/image-7.png" alt="" class="wp-image-2106" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-7.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-7-300x96.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-7-1024x329.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-7-768x247.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph">pgsql2 and pgsql3 as slave</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="366" src="https://serverstadium.com/wp-content/uploads/2022/05/image-8.png" alt="" class="wp-image-2107" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-8.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-8-300x95.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-8-1024x324.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-8-768x243.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="353" src="https://serverstadium.com/wp-content/uploads/2022/05/image-9.png" alt="" class="wp-image-2108" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-9.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-9-300x92.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-9-1024x312.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-9-768x234.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Install Haproxy</strong></h2>



<p class="wp-block-paragraph">Our last step install HAproxy service to give us an endpoint to which the application can connect to the database.</p>



<p class="wp-block-paragraph"><code>sudo apt -y install haproxy</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="146" src="https://serverstadium.com/wp-content/uploads/2022/05/image-10.png" alt="" class="wp-image-2109" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-10.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-10-300x38.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-10-1024x129.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-10-768x97.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph" id="block-afeeab82-1a19-4d69-a234-c7d500db5846">Edit the haproxy file configuration as follow</p>



<pre class="wp-block-preformatted"><code>nano /etc/haproxy/haproxy.cfg</code>
global
    maxconn 100

defaults
    log global
    mode tcp
    retries 2
    timeout client 30m
    timeout connect 4s
    timeout server 30m
    timeout check 5s

listen stats
    mode http
    bind *:7000
    stats enable
    stats uri /

listen postgres
    bind *:5000
    option httpchk
    http-check expect status 200
    default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
    server pgsql1 <strong>10.3.3.12</strong>:5432 maxconn 100 check port 8008
    server pgsql2 <strong>10.3.3.71</strong>:5432 maxconn 100 check port 8008
    server pgsql3 <strong>10.3.3.70</strong>:5432 maxconn 100 check port 8008
</pre>



<p class="wp-block-paragraph">Check our haproxy configuration using the following command:</p>



<pre class="wp-block-preformatted">sudo /usr/sbin/haproxy -c -V -f /etc/haproxy/haproxy.cfg&nbsp;</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1000" height="54" src="https://serverstadium.com/wp-content/uploads/2022/05/image-11.png" alt="" class="wp-image-2110" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-11.png 1000w, https://serverstadium.com/wp-content/uploads/2022/05/image-11-300x16.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-11-768x41.png 768w" sizes="(max-width: 1000px) 100vw, 1000px" /></figure>



<p class="wp-block-paragraph">If the configuration is valid, we can restart our haproxy service</p>



<p class="wp-block-paragraph"><code>sudo systemctl restart haproxy</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="307" src="https://serverstadium.com/wp-content/uploads/2022/05/image-12.png" alt="" class="wp-image-2111" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-12.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-12-300x80.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-12-1024x272.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-12-768x204.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">In the final step, we can monitor the PostgreSQL cluster using the browser as shown in the screenshot below:</p>



<p class="wp-block-paragraph"><a href="http://199.180.130.99:7000/">http://199.180.130.99:7000/</a></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="380" src="https://serverstadium.com/wp-content/uploads/2022/05/image-13.png" alt="" class="wp-image-2112" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-13.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-13-300x99.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-13-1024x336.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-13-768x252.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph">As we can see, psql1 is the leader of the PostgreSQL cluster with the green line row.<br>Now we will kill patroni service on psql1, to see which VM will be acting as the leader.</p>



<p class="wp-block-paragraph"><code>systemctl stop patroni</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1157" height="374" src="https://serverstadium.com/wp-content/uploads/2022/05/image-14.png" alt="" class="wp-image-2113" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-14.png 1157w, https://serverstadium.com/wp-content/uploads/2022/05/image-14-300x97.png 300w, https://serverstadium.com/wp-content/uploads/2022/05/image-14-1024x331.png 1024w, https://serverstadium.com/wp-content/uploads/2022/05/image-14-768x248.png 768w" sizes="(max-width: 1157px) 100vw, 1157px" /></figure>



<p class="wp-block-paragraph">After we stop service patroni service in psql1, the psql2 will be elected as the leader node.</p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><br><strong>Test PostgresSQL Cluster</strong></h2>



<p class="wp-block-paragraph">If we want to make a connection to PostgreSQL, we use the IP Address of HAProxy and port 5000.</p>



<p class="wp-block-paragraph"><code>su - postgres<br>psql -U postgres -h 199.180.130.99 -p 5000</code></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="550" height="96" src="https://serverstadium.com/wp-content/uploads/2022/05/image-15.png" alt="" class="wp-image-2120" srcset="https://serverstadium.com/wp-content/uploads/2022/05/image-15.png 550w, https://serverstadium.com/wp-content/uploads/2022/05/image-15-300x52.png 300w" sizes="(max-width: 550px) 100vw, 550px" /></figure>



<p class="wp-block-paragraph"><br>You now have a high availability PostgreSQL Cluster in ServerStadium that is ready to use.<br>Please don&#8217;t hesitate to <a href="https://serverstadium.com/id/contact/" target="_blank" rel="noreferrer noopener">contact ServerStadium support</a> if you need further assistance.</p>



<p class="wp-block-paragraph">Find more tutorials articles about ServerStadium in our Knowledge Base, or if you want to get more general knowledge about technology feel free to visit our <a href="https://serverstadium.com/blog/" target="_blank" rel="noreferrer noopener">Blog.</a> </p>



<p class="wp-block-paragraph">Curious to know more about our products and panel? register <a href="https://cp.serverstadium.com/regist-user" target="_blank" rel="noreferrer noopener">here</a> for free!.</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/set-up-high-availability-postgresql-cluster-using-patroni-on-serverstadium/">Set Up High Availability PostgreSQL Cluster Using Patroni on ServerStadium</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to install and secure MariaDB on Ubuntu 18.04 and 20.04 on ServerStadium</title>
		<link>https://serverstadium.com/knowledge-base/how-to-install-and-secure-mariadb-on-ubuntu-18-04-and-20-04/</link>
		
		<dc:creator><![CDATA[Brian K]]></dc:creator>
		<pubDate>Mon, 22 Mar 2021 05:43:31 +0000</pubDate>
				<guid isPermaLink="false">https://cloudraya.com/knowledge-base/?p=937</guid>

					<description><![CDATA[<p>In this article, we are going to show you how to install and secure MariaDB on Ubuntu 18.04 and 20.04 on ServerStadium, after previously talking about MongoDB. Read also: How to Install and Secure MongoDB on Ubuntu 18.04 and 20.04 But, before that, let&#8217;s define what MariaDB is. In short, MariaDB is an open-source, community-developed,&#8230;</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/how-to-install-and-secure-mariadb-on-ubuntu-18-04-and-20-04/">How to install and secure MariaDB on Ubuntu 18.04 and 20.04 on ServerStadium</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">In this article, we are going to show you how to install and secure MariaDB on Ubuntu 18.04 and 20.04 on ServerStadium, after previously talking about MongoDB.</p>



<p class="wp-block-paragraph">Read also: <a href="https://serverstadium.com/knowledge-base/how-to-install-and-securing-mongodb-on-ubuntu-18-04-and-20-04/" target="_blank" rel="noreferrer noopener">How to Install and Secure MongoDB on Ubuntu 18.04 and 20.04</a></p>



<p class="wp-block-paragraph">But, before that, let&#8217;s define what MariaDB is. In short, MariaDB is an open-source, community-developed, forever free relational database management system (RDBMS) forked from MySQL. However, it is highly compatible with MySQL as a drop-in replacement.</p>



<p class="wp-block-paragraph">So, check the steps below to install and secure <strong>MariaDB 10.5</strong> for <strong>Ubuntu 18.04</strong> and <strong>Ubuntu 20.04</strong> instances on ServerStadium.</p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">TLDR</h2>



<p class="wp-block-paragraph">For those who need quick steps:</p>



<ol class="wp-block-list">
<li>Firstly, Enter the commands below</li>
<li>Secondly, allow remote connections to port 3306/TCP on the Security Profile.</li>
</ol>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<pre class="wp-block-code"><code>&lt;code&gt;sudo apt update &amp;amp;&amp;amp; sudo apt install apt-transport-https -y
curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
sudo apt install mariadb-server -y
sudo mysql_secure_installation
# For remote connection, otherwise ignore these lines
sudo sed -Ei 's/(bind-address *=).*/\1 INTERFACE-IP/' /etc/mysql/mariadb.conf.d/50-server.cnf
sudo systemctl restart mariadb
sudo ufw allow 3306/tcp&lt;/code&gt;</code></pre>
</div></div>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">How to Install MariaDB</h2>



<p class="wp-block-paragraph">MariaDB hasn&#8217;t been shipped to Ubuntu 18.04 / 20.04 official repository yet. So you will have to download it directly from <a href="https://mariadb.com">https://mariadb.com</a>. After that, you can either do the following:</p>



<ol class="wp-block-list">
<li>Download the binary packages, or</li>
<li>Setup the repository via script</li>
</ol>



<p class="wp-block-paragraph">Hence, this tutorial will cover the second method.</p>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Step 1. Install apt-transport-https</h3>



<p class="wp-block-paragraph">Firstly, the installer script needs <code>apt-transport-https</code> package.</p>



<pre class="wp-block-code"><code>sudo apt update
sudo apt install apt-transport-https -y</code></pre>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Step 2. Pull the script and execute</h3>



<p class="wp-block-paragraph">You can download the script on</p>



<pre class="wp-block-preformatted"><code>https://downloads.mariadb.com/MariaDB/mariadb_repo_setup</code></pre>



<p class="wp-block-paragraph">and run it, but you can do it in one-liner like this instead:</p>



<pre class="wp-block-code"><code>curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash</code></pre>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>A brief explanation about the <code>-LsS</code> flags on <code>curl </code>command: <br />&#8211; <code>L</code> Curl will follow any redirects <br />&#8211; <code>s</code> Silent all outputs <br />&#8211; <code>S</code> If used together with <code>s</code>, it will output error messages <em>only</em> on failure</p>
</blockquote>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" class="wp-image-938" src="https://serverstadium.com/wp-content/uploads/2021/01/image-43.png" alt="" width="679" height="195" srcset="https://serverstadium.com/wp-content/uploads/2021/01/image-43.png 2048w, https://serverstadium.com/wp-content/uploads/2021/01/image-43-300x86.png 300w, https://serverstadium.com/wp-content/uploads/2021/01/image-43-1024x294.png 1024w, https://serverstadium.com/wp-content/uploads/2021/01/image-43-768x221.png 768w, https://serverstadium.com/wp-content/uploads/2021/01/image-43-1536x441.png 1536w" sizes="(max-width: 679px) 100vw, 679px" />
<figcaption>MariaDB repo setup script</figcaption>
</figure>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Step 3. Install mariadb-server</h3>



<p class="wp-block-paragraph">You don&#8217;t have to update the repository, as the script has done it for you. So, proceed to install:</p>



<pre class="wp-block-code"><code>sudo apt install mariadb-server -y</code></pre>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Configuration</h2>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Step 4. Secure the installation</h3>



<p class="wp-block-paragraph">To secure the MariaDB installation, run:</p>



<pre class="wp-block-code"><code>sudo mysql_secure_installation</code></pre>



<p class="wp-block-paragraph">Then, answer every prompt with yes.</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" class="wp-image-939" src="https://serverstadium.com/wp-content/uploads/2021/01/image-44.png" alt="" width="782" height="1446" srcset="https://serverstadium.com/wp-content/uploads/2021/01/image-44.png 1564w, https://serverstadium.com/wp-content/uploads/2021/01/image-44-162x300.png 162w, https://serverstadium.com/wp-content/uploads/2021/01/image-44-554x1024.png 554w, https://serverstadium.com/wp-content/uploads/2021/01/image-44-768x1420.png 768w, https://serverstadium.com/wp-content/uploads/2021/01/image-44-831x1536.png 831w, https://serverstadium.com/wp-content/uploads/2021/01/image-44-1108x2048.png 1108w" sizes="(max-width: 782px) 100vw, 782px" />
<figcaption>Securing MariaDB installation</figcaption>
</figure>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Step 5. Enable remote access</h3>



<p class="wp-block-paragraph">The default setting for a fresh MariaDB installation is to allow only local connections. So, you can verify it via the below syntax:</p>



<pre class="wp-block-code"><code>sudo ss -tulpn | grep mariadb</code></pre>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" class="wp-image-940" src="https://serverstadium.com/wp-content/uploads/2021/01/image-45.png" alt="" width="768" height="180" srcset="https://serverstadium.com/wp-content/uploads/2021/01/image-45.png 1024w, https://serverstadium.com/wp-content/uploads/2021/01/image-45-300x70.png 300w, https://serverstadium.com/wp-content/uploads/2021/01/image-45-768x180.png 768w" sizes="(max-width: 768px) 100vw, 768px" />
<figcaption>MariaDB is listening on 127.0.0.1</figcaption>
</figure>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><code>ss -tulpn</code> will list all listening ports/sockets. <br />&#8211; <code>t</code> Display TCP sockets.<br />&#8211; <code>u</code> Display UDP sockets. <br />&#8211; <code>l</code> Display only listening sockets. <br />&#8211; <code>p</code> Show process using socket. <br />&#8211; <code>n</code> Do not try to resolve service names.</p>
</blockquote>



<p class="wp-block-paragraph">There, you will see that it listens to loopback IP (127.0.0.1), which means it will not accept remote connections.</p>



<p class="wp-block-paragraph">If you plan to build an app on the same server, then this is enough, and you can ignore the rest of the steps. But, if this is not the case, you can keep going.</p>



<p class="wp-block-paragraph">Then, Open <code>/etc/mysql/mariadb.conf.d/50-server.conf</code> and find the line <code>bind-address</code>. After that, change the IP address  <code>127.0.0.1</code> to your interface IP.</p>



<pre class="wp-block-code"><code>sudoedit /etc/mysql/mariadb.conf.d/50-server.conf
# find line 'bind-address = 127.0.0.1'
# change it to interface's IP</code></pre>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="900" height="276" class="wp-image-941" src="https://serverstadium.com/wp-content/uploads/2021/01/image-46.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2021/01/image-46.png 900w, https://serverstadium.com/wp-content/uploads/2021/01/image-46-300x92.png 300w, https://serverstadium.com/wp-content/uploads/2021/01/image-46-768x236.png 768w" sizes="(max-width: 900px) 100vw, 900px" />
<figcaption>Change bind-address</figcaption>
</figure>



<p class="wp-block-paragraph">If you are not sure which IP to set, then you can check it by using the below command:</p>



<pre class="wp-block-code"><code>ip -4 -br a s</code></pre>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="801" height="240" class="wp-image-942" src="https://serverstadium.com/wp-content/uploads/2021/01/image-47.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2021/01/image-47.png 801w, https://serverstadium.com/wp-content/uploads/2021/01/image-47-300x90.png 300w, https://serverstadium.com/wp-content/uploads/2021/01/image-47-768x230.png 768w" sizes="(max-width: 801px) 100vw, 801px" />
<figcaption>Display IP addresses</figcaption>
</figure>



<p class="wp-block-paragraph">After you have changed the bind-address, you need to restart the MariaDB service.</p>



<pre class="wp-block-code"><code>sudo systemctl restart mariadb
# check the status
sudo systemctl status mariadb</code></pre>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" class="wp-image-943" src="https://serverstadium.com/wp-content/uploads/2021/01/image-48.png" alt="" width="768" height="461" srcset="https://serverstadium.com/wp-content/uploads/2021/01/image-48.png 1024w, https://serverstadium.com/wp-content/uploads/2021/01/image-48-300x180.png 300w, https://serverstadium.com/wp-content/uploads/2021/01/image-48-768x461.png 768w" sizes="(max-width: 768px) 100vw, 768px" />
<figcaption>MariaDB service status</figcaption>
</figure>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Step 6. Allow port 3306/TCP on ufw</h3>



<p class="wp-block-paragraph">By default, both Ubuntu 18.04 and Ubuntu 20.04 are using <code>ufw</code>. But first, check if it&#8217;s active or not:</p>



<pre class="wp-block-code"><code>sudo ufw status</code></pre>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="822" height="298" class="wp-image-944" src="https://serverstadium.com/wp-content/uploads/2021/01/image-49.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2021/01/image-49.png 822w, https://serverstadium.com/wp-content/uploads/2021/01/image-49-300x109.png 300w, https://serverstadium.com/wp-content/uploads/2021/01/image-49-768x278.png 768w" sizes="(max-width: 822px) 100vw, 822px" />
<figcaption>Display UFW status</figcaption>
</figure>



<p class="wp-block-paragraph">If it&#8217;s inactive, then skip this step. Otherwise, you need to allow mariadb (3306/tcp).</p>



<pre class="wp-block-code"><code>sudo ufw allow 3306/tcp
# or
sudo ufw allow mysql</code></pre>



<p class="wp-block-paragraph">Then, verify by using <code>sudo ufw status</code></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="850" height="378" class="wp-image-945" src="https://serverstadium.com/wp-content/uploads/2021/01/image-50.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2021/01/image-50.png 850w, https://serverstadium.com/wp-content/uploads/2021/01/image-50-300x133.png 300w, https://serverstadium.com/wp-content/uploads/2021/01/image-50-768x342.png 768w" sizes="(max-width: 850px) 100vw, 850px" /></figure>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">Step 7. Add Firewall Rule on the Security Profile on ServerStadium</h3>



<p class="wp-block-paragraph">Add TCP port 3306 (MySQL default port) to the Security Profile to allow remote public connections.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="826" height="346" class="wp-image-946" src="https://serverstadium.com/wp-content/uploads/2021/01/image-51.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2021/01/image-51.png 826w, https://serverstadium.com/wp-content/uploads/2021/01/image-51-300x126.png 300w, https://serverstadium.com/wp-content/uploads/2021/01/image-51-768x322.png 768w" sizes="(max-width: 826px) 100vw, 826px" />
<figcaption>Add Firewall Rule on the Security Profile</figcaption>
</figure>



<p class="wp-block-paragraph">All in all, that is how you install and secure MariaDB on Ubuntu 18.04 and 20.04. If you are eager for more information, you should check our previous article about installing and securing<a href="https://serverstadium.com/knowledge-base/how-to-install-and-securing-mongodb-on-ubuntu-18-04-and-20-04/" target="_blank" rel="noreferrer noopener"> MongoDB on Ubuntu</a>.</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/how-to-install-and-secure-mariadb-on-ubuntu-18-04-and-20-04/">How to install and secure MariaDB on Ubuntu 18.04 and 20.04 on ServerStadium</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to Install and Securing MongoDB on Ubuntu 18.04 and 20.04</title>
		<link>https://serverstadium.com/knowledge-base/how-to-install-and-securing-mongodb-on-ubuntu-18-04-and-20-04/</link>
		
		<dc:creator><![CDATA[Brian K]]></dc:creator>
		<pubDate>Mon, 08 Mar 2021 11:41:29 +0000</pubDate>
				<guid isPermaLink="false">https://cloudraya.com/knowledge-base/?p=926</guid>

					<description><![CDATA[<p>Many wonder how to install and secure MongoDB, especially in Ubuntu 18.04 and 20.04. Before that, let&#8217;s define what MongoDB is. MongoDB is a general-purpose document database that stores its data in JSON-like documents. It also claimed this is the most natural way to think about data. MongoDB has features that traditional databases in Relational&#8230;</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/how-to-install-and-securing-mongodb-on-ubuntu-18-04-and-20-04/">How to Install and Securing MongoDB on Ubuntu 18.04 and 20.04</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Many wonder how to install and secure MongoDB, especially in Ubuntu 18.04 and 20.04. Before that, let&#8217;s define what MongoDB is.</p>



<p class="wp-block-paragraph">MongoDB is a general-purpose document database that stores its data in <a href="https://www.json.org/json-en.html" target="_blank" rel="noreferrer noopener">JSON</a>-like documents. It also claimed this is the most natural way to think about data.</p>



<pre class="wp-block-code"><code>
#JSON file example
{
  "os": "Ubuntu",
  "company": "Canonical Ltd.",
  "founder": {
    "name": "Mark Shuttleworth"
  },
  "age": "16",
  "flavours": [
    "👽︎ Kubuntu",
    "✔ Lubuntu",
    "MATE",
    "Xubuntu"
  ]
}</code></pre>



<p class="wp-block-paragraph">MongoDB has features that traditional databases in Relational Database Management (RDBMS), like MariaDB and PostgreSQL, don&#8217;t have or need additional steps to set up properly, such as:</p>



<ol class="wp-block-list">
<li>Expressive and Flexible means you can create a dynamic data structure, no database schema that you have to define first. Also, MongoDB supports schema-less databases, whereas, in RDBMS, you have to define the schema first and follow that schema.</li>
<li>MongoDB can be easily scaled out by adding a new server as a replica, adding more READ performance.</li>
<li>In High availability terms, the MongoDB cluster can automatically failover to an active node when a Master Node is down.</li>
</ol>



<pre class="wp-block-code"><code>## MySQL way
# initialization 
CREATE TABLE `JSON_in_RDBMS` (
   `os` VARCHAR(150) NOT NULL,
   `company` VARCHAR(150) NOT NULL,
   `founder` VARCHAR(150) NOT NULL,
   `age` INT, 
   `flavours` VARCHAR(150) NOT NULL)
ENGINE =  InnoDB;

# inserting the data
INSERT INTO `JSON_in_RDBMS` (`os`, `company`, `founder`, `age`, `flavours`) VALUES ('Ubuntu1', 'Canonical Ltd.', 'Mark Shuttleworth', '16', 'Kubuntu, Lubuntu, MATE, Xubuntu')</code></pre>



<p class="wp-block-paragraph">&nbsp;</p>



<h2 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>Installing MongoDB on Ubuntu</strong></h2>



<p class="wp-block-paragraph">This tutorial will cover the installation of <em>MongoDB Community Edition</em> on <em>64-bit</em> <em><a href="http://ubuntu.com/">Ubuntu Linux</a> LTS</em> (long-term support) releases using the <em><a href="https://ubuntu.com/server/docs/package-management"><code>apt</code> package manage</a>r</em>, especially on the 18.04 (<a href="http://releases.ubuntu.com/bionic">Bionic</a>) and 20.04 (<a href="http://releases.ubuntu.com/focal">Focal</a>) LTS releases on <a href="https://en.wikipedia.org/wiki/X86-64">x86_64</a> architecture.</p>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">1. Import the public key used by the package management system.</h3>



<pre class="wp-block-code"><code># Make sure you have installed the gnupg package
sudo apt-get install gnupg
</code></pre>



<pre class="wp-block-code"><code># Import the MongoDB public GPG Key
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -</code></pre>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">2. Create a list file for MongoDB repository</h3>



<pre class="wp-block-code"><code># Create the /etc/apt/sources.list.d/mongodb-org-4.4.list file for Ubuntu
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list</code></pre>



<pre id="block-750ee643-0f96-4ed1-bb3f-a01e102240c2" class="wp-block-code"><code># Then make sure the local package database reloaded
sudo apt-get update
</code></pre>



<h3 id="block-bf0deaec-35bd-4eeb-ae11-1c096c0829af" class="has-vivid-cyan-blue-color has-text-color wp-block-heading">3. Install MongoDB </h3>



<pre id="block-750ee643-0f96-4ed1-bb3f-a01e102240c2" class="wp-block-code"><code># Install the latest stable package
sudo apt-get install -y mongodb-org</code></pre>



<h3 id="block-bf0deaec-35bd-4eeb-ae11-1c096c0829af" class="has-vivid-cyan-blue-color has-text-color wp-block-heading">4. Run MongoDB</h3>



<pre class="wp-block-code"><code># Start the process on systemd init 
sudo systemctl start mongod</code></pre>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading">5. MongoDB is up and running !</h3>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="910" height="192" class="wp-image-968" src="https://serverstadium.com/wp-content/uploads/2021/01/2021-01-28-08_45_47-0_1_bash-_galaxy_.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2021/01/2021-01-28-08_45_47-0_1_bash-_galaxy_.png 910w, https://serverstadium.com/wp-content/uploads/2021/01/2021-01-28-08_45_47-0_1_bash-_galaxy_-300x63.png 300w, https://serverstadium.com/wp-content/uploads/2021/01/2021-01-28-08_45_47-0_1_bash-_galaxy_-768x162.png 768w" sizes="(max-width: 910px) 100vw, 910px" /></figure>



<pre class="wp-block-code"><code># Enable Autostart the process 
sudo systemctl enable mongod</code></pre>



<pre class="wp-block-code"><code># Restart the process 
sudo systemctl restart mongod</code></pre>



<pre class="wp-block-code"><code># Stop the process
sudo systemctl stop mongod</code></pre>



<pre class="wp-block-code"><code># Verify the process running or stopped
sudo systemctl status mongod</code></pre>



<h2 class="wp-block-heading"><strong>Securing MongoDB on Ubuntu</strong></h2>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>1. Connect to the MongoDB</strong></h3>



<p class="wp-block-paragraph"># First, Connect to the Mongo instance<br />$ mongo<br />MongoDB shell version v4.4.3<br />connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&amp;gssapiServiceName=mongodb<br />Implicit session: session { &#8220;id&#8221; : UUID(&#8220;0cfcaad9-7c56-40ea-81f8-af273c014d5e&#8221;) }<br />MongoDB server version: 4.4.3<br />Welcome to the MongoDB shell.<br />&gt; </p>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>2. Create the user</strong></h3>



<p class="wp-block-paragraph"># Copy and paste this to Mongodb shell</p>



<p class="wp-block-paragraph">use ubi_db<br />db.createUser(<br />{<br />user: &#8220;ubi_user&#8221;,<br />pwd: &#8220;ub1_p4ss&#8221;,<br />roles: [ { role: &#8220;readWrite&#8221;, db: &#8220;ubi_db&#8221; },<br />{ role: &#8220;read&#8221;, db: &#8220;ubi_db_other&#8221; } ]<br />}<br />)</p>



<p class="wp-block-paragraph"><strong>If successfully created, it will show like this output.</strong></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="518" height="429" class="wp-image-979" src="https://serverstadium.com/wp-content/uploads/2021/02/image.png" alt="" srcset="https://serverstadium.com/wp-content/uploads/2021/02/image.png 518w, https://serverstadium.com/wp-content/uploads/2021/02/image-300x248.png 300w" sizes="(max-width: 518px) 100vw, 518px" /></figure>



<h3 class="has-vivid-cyan-blue-color has-text-color wp-block-heading"><strong>3. Then connect &amp; authenticate as the user</strong></h3>



<p class="wp-block-paragraph"># Inline output on mongo shell command<br />. &gt; use ubi_db<br />switched to db ubi_db<br />. &gt; db.auth(&#8220;ubi_user&#8221;, &#8220;ub1_p4ss&#8221;)<br />db.foo.insert({x:1})<br />1<br />. &gt; db.foo.insert({x:1})<br />WriteResult({ &#8220;nInserted&#8221; : 1 })<br />. &gt; use ubi_db_other<br />switched to db ubi_db_other<br />. &gt; db.foo.find({})</p>



<p class="wp-block-paragraph">&nbsp;</p>
<p>The post <a href="https://serverstadium.com/knowledge-base/how-to-install-and-securing-mongodb-on-ubuntu-18-04-and-20-04/">How to Install and Securing MongoDB on Ubuntu 18.04 and 20.04</a> appeared first on <a href="https://serverstadium.com">ServerStadium</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
