mirror of
https://github.com/SrIzan10/mainwebsite.git
synced 2026-06-06 00:56:58 +00:00
198 lines
10 KiB
XML
198 lines
10 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<feed xmlns="http://www.w3.org/2005/Atom">
|
|
<id>https://srizan.dev/blog</id>
|
|
<title>Sr Izan's Blog</title>
|
|
<updated>2023-11-22T20:55:41.512Z</updated>
|
|
<generator>https://github.com/jpmonette/feed</generator>
|
|
<author>
|
|
<name>Sr Izan</name>
|
|
<email>izan@srizan.dev</email>
|
|
<uri>https://srizan.dev</uri>
|
|
</author>
|
|
<link rel="alternate" href="https://srizan.dev/blog"/>
|
|
<link rel="self" href="https://srizan.dev/blog/atom.xml"/>
|
|
<subtitle>My little donowall place on the net</subtitle>
|
|
<logo>https://srizan.dev/pfp.png</logo>
|
|
<icon>https://srizan.dev/pfp.png</icon>
|
|
<rights>Copyleft 2023, Sr Izan</rights>
|
|
<entry>
|
|
<title type="html"><![CDATA[My tales of MongoDB migration]]></title>
|
|
<id>https://srizan.dev/blog/2</id>
|
|
<link href="https://srizan.dev/blog/2"/>
|
|
<updated>2023-11-11T23:00:00.000Z</updated>
|
|
<summary type="html"><![CDATA[Here I ramble about the last service migration I did, MongoDB, and all the difficulties that came with it.]]></summary>
|
|
<content type="html"><![CDATA[<h2>Introduction</h2>
|
|
<p>So, the last few months I've been migrating services from my good old Raspberry Pi into my new HP server and the last service I migrated was MongoDB.</p>
|
|
<p>I've been using MongoDB for a while now and I've been using it for a few things, like my discord bots, <a href="https://github.com/SrIzan10/webhooks-ui">webhooks-ui</a> and probably other projects I don't remember right now.</p>
|
|
<p>So, let's get started!</p>
|
|
<h2>Testing the plan</h2>
|
|
<p>My database instance is on Docker with a replica set of 1 node (itself) so <a href="https://www.prisma.io/">Prisma</a> works.</p>
|
|
<p>My idea is to add the HP server as a secondary replica and then promote it to be the primary one, but I don't know if that will work, so we need to test some stuff.</p>
|
|
<p>I first created 2 docker containers on my <a href="https://gist.github.com/SrIzan10/50bc2ba689a4cc43bcbac2799cc733c9">main Ryzen machine</a>'s WSL Ubuntu instance.</p>
|
|
<p>I created a <code>docker-compose.yml</code> file with the following content:</p>
|
|
<pre><code class="language-yml">version: "3.8"
|
|
services:
|
|
mongo1:
|
|
image: mongo:4.4.17-rc0-focal
|
|
container_name: mongo1
|
|
restart: always
|
|
ports:
|
|
- 27017:27017
|
|
volumes:
|
|
- ./mongo1:/data/db
|
|
command: mongod --replSet mongoset
|
|
networks:
|
|
- mongo
|
|
mongo2:
|
|
image: mongo:4.4.17-rc0-focal
|
|
container_name: mongo2
|
|
restart: always
|
|
ports:
|
|
- 27018:27017
|
|
volumes:
|
|
- ./mongo2:/data/db
|
|
command: mongod --replSet mongoset
|
|
networks:
|
|
- mongo
|
|
networks:
|
|
mongo:
|
|
</code></pre>
|
|
<p>and ran it with <code>docker compose up -d</code>.</p>
|
|
<p>I went to connect with MongoDB Compass and it didn't work for some reason. I asked GPT and nothing. It looks like it accepted the connection but it won't connect, so I installed <code>mongosh</code> and tried to connect with that.</p>
|
|
<pre><code class="language-bash">$ mongosh mongodb://localhost:27017
|
|
</code></pre>
|
|
<p>...and it worked! That didn't make any sense, but okay, we can work with it.</p>
|
|
<p>I then connected to the <code>mongo1</code> instance and ran the following commands:</p>
|
|
<pre><code class="language-bash">> rs.initiate()
|
|
</code></pre>
|
|
<p>and it worked, but only that same database connected. Before adding the second database to the replica, I went ahead and pinged it from the first container (just to check if the network configuration worked):</p>
|
|
<pre><code class="language-bash">docker exec mongo1 sh -c "rm /bin/ping;apt update;apt install inetutils-ping -y;ping mongo2"
|
|
</code></pre>
|
|
<p>I removed /bin/ping because I tried to transfer the binary from WSL to the container but it still needed some libraries and I didn't want to bother, so I just installed the package.</p>
|
|
<p>It worked, so I went ahead and added the second database to the replica set:</p>
|
|
<pre><code class="language-bash">> rs.add("mongo2")
|
|
</code></pre>
|
|
<p>After waiting for it, the second database connected and everything was working fine. Let's create a collection and some documents on the primary replica (mongo1):</p>
|
|
<pre><code class="language-bash">> use test
|
|
> db.createCollection("test")
|
|
> db.test.insertOne({ name: "test" })
|
|
</code></pre>
|
|
<p>and then, let's check if it's on the second replica (mongo2):</p>
|
|
<pre><code class="language-bash">$ mongosh mongodb://localhost:27017
|
|
</code></pre>
|
|
<pre><code class="language-bash">> use test
|
|
> db.getMongo().setReadPref("secondaryPreferred")
|
|
> db.test.find()
|
|
</code></pre>
|
|
<p>and, yeah, that worked.</p>
|
|
<p>I don't really know if ORMs will read when connecting to the second replica, but for now it's fine as the main plan is on track.<br>So, to promote I connected to the primary replica (mongo1) and ran the following command:</p>
|
|
<pre><code class="language-bash">> rs.stepDown()
|
|
</code></pre>
|
|
<p>And that worked! Woo! The second replica is now the primary one. We can now start <em>drum rolls please</em>:</p>
|
|
<h2>The migration</h2>
|
|
<p>This is it. We're doing it.</p>
|
|
<p>I went ahead and created a new docker-compose file on my server with the following content:</p>
|
|
<pre><code class="language-yml">version: "3.8"
|
|
services:
|
|
mongo:
|
|
image: mongo:4.4.17-rc0-focal
|
|
container_name: mongodb
|
|
restart: unless-stopped
|
|
ports:
|
|
- 27017:27017
|
|
volumes:
|
|
- ./mongo:/data/db
|
|
command: mongod --replSet rs0
|
|
</code></pre>
|
|
<p>After deploying the stack, I connected using mongosh to the primary db and ran the following command:</p>
|
|
<pre><code class="language-bash">> rs.add("ip")
|
|
</code></pre>
|
|
<p>and after waiting for a while it looked like it worked. I then connected to the new database and ran the following command to check if the replica cloned fine:</p>
|
|
<pre><code class="language-bash">> db.getMongo().setReadPref("secondaryPreferred")
|
|
</code></pre>
|
|
<p>and let's just let the results speak for themselves:</p>
|
|
<pre><code class="language-bash">rs0 [direct: secondary] test> show dbs
|
|
# author's note: some dbs are redacted for privacy reasons
|
|
admin 80.00 KiB
|
|
api 80.00 KiB
|
|
ava 40.00 KiB
|
|
bask 168.00 KiB
|
|
config 144.00 KiB
|
|
local 348.00 KiB
|
|
vinci 428.00 KiB
|
|
rs0 [direct: secondary] test> use vinci
|
|
switched to db vinci
|
|
rs0 [direct: secondary] vinci> show tables
|
|
afk
|
|
birthdays
|
|
chatgpt
|
|
giveaways-enters
|
|
giveaways-message
|
|
padyama
|
|
suggestions
|
|
twitter
|
|
warns
|
|
youtube
|
|
rs0 [direct: secondary] vinci> db.afk.find()
|
|
[
|
|
{
|
|
_id: ObjectId("sadfsad fsadfsdf"),
|
|
id: 'redacted',
|
|
reason: 'redacted',
|
|
__v: 0
|
|
},
|
|
{
|
|
_id: ObjectId("asdfsadfadf"),
|
|
id: 'redacted',
|
|
reason: 'readacted',
|
|
__v: 0
|
|
}
|
|
]
|
|
rs0 [direct: secondary] vinci>
|
|
</code></pre>
|
|
<p>Nice. let's now try to write something to the database from Vinci:<br><img src="https://img.srizan.dev/Discord_a2iXkWYxwn.png" alt=""><br>That just worked and we can see it on the secondary replica:</p>
|
|
<pre><code class="language-bash">rs0 [direct: secondary] vinci> db.afk.find({ id: '703974042700611634' })
|
|
[
|
|
{
|
|
_id: ObjectId("6550eccc6154a8c9030fe76a"),
|
|
id: '703974042700611634',
|
|
reason: 'test',
|
|
__v: 0
|
|
}
|
|
]
|
|
</code></pre>
|
|
<p>Let's now edit all .envs and change the database url to the new secondary one. For this I checked all dbs that I have and then go from top to bottom editing the secrets.</p>
|
|
<p>After that was done I needed to deploy all changes. I went ahead and created too many tabs on my terminal and ran the all deployment commands on each tab. At the same time.<br>I really hope that doesn't make my server run out of ram, because I'm really short on that.</p>
|
|
<p>After executing all the commands I <code>rs.stepDown()</code>'ed the primary Raspberry Pi replica and, as expected, the HP Server took over.</p>
|
|
<p>The last command of the day:</p>
|
|
<pre><code class="language-bash">> rs.remove("ip")
|
|
</code></pre>
|
|
<p>...SIKE! I needed to check the logs of the containers to see if everything was working fine. The <code>api</code> and <code>vinci</code> to be exact.<br>This is because <code>api</code> runs Prisma and <code>vinci</code> runs the now defunct in my stack, <a href="https://mongoosejs.com/">mongoose</a>.</p>
|
|
<p>Luckily enough, both were fine, so I was free. Yay!</p>
|
|
<h2>Conclusion</h2>
|
|
<p>Welp, that was a lot of work. I'm glad it's over. I got my HP server on July and it's now November and I just finished migrating.<br>Could I have done it in less time? Yes.<br>Was I lazy? Also yes.</p>
|
|
<p>So that answers all your questions.</p>
|
|
<p>I hope you enjoyed this my first blog post, and thankfully it was a big one.<br>This took 3 hours in total, but at the end of the day, it was worth it.</p>
|
|
<p>I'll see you in the next one!</p>
|
|
]]></content>
|
|
<author>
|
|
<name>Sr Izan</name>
|
|
<uri>https://srizan.dev</uri>
|
|
</author>
|
|
</entry>
|
|
<entry>
|
|
<title type="html"><![CDATA[Welcome to my new blog!]]></title>
|
|
<id>https://srizan.dev/blog/1</id>
|
|
<link href="https://srizan.dev/blog/1"/>
|
|
<updated>2023-08-19T22:00:00.000Z</updated>
|
|
<summary type="html"><![CDATA[This post welcomes you to my new blog]]></summary>
|
|
<content type="html"><![CDATA[<h1>Hey!</h1>
|
|
<p>This is probably the last time I'm going to make a blog. I've made a few in the past, but I've never really stuck to them. I'm hoping that this time will be different.<br>This one was made entirely from scratch using React and Markdown, initially trying to use MDX, but it was a pain to set up, and it didn't end up working in the end.<br>I'm hoping to post about my projects, and maybe some other stuff too. I'm not sure yet, but I'll figure it out as I go along.<br>Anyways, thank you for reading. I hope you enjoyed my UX/UI for this one!</p>
|
|
<p>PD: I need some help for making the blog text look good and readable, so hit me up on my Discord if you have any ideas.</p>
|
|
]]></content>
|
|
<author>
|
|
<name>Sr Izan</name>
|
|
<uri>https://srizan.dev</uri>
|
|
</author>
|
|
</entry>
|
|
</feed> |