<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Andra's Blog]]></title><description><![CDATA[Andra's Blog]]></description><link>https://andramalmen.com</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Apr 2026 10:44:58 GMT</lastBuildDate><atom:link href="https://andramalmen.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Transfer files to and from your EC2 instance]]></title><description><![CDATA[To transfer files between your local machine and an EC2 instance, you will need scp (secure copy) available on your machine.
The command to transfer files from your machine to your EC2 instance is:
scp -i ./path_to_your_key_pair_cer_files ./path_to_f...]]></description><link>https://andramalmen.com/transfer-files-to-and-from-your-ec2-instance</link><guid isPermaLink="true">https://andramalmen.com/transfer-files-to-and-from-your-ec2-instance</guid><category><![CDATA[AWS]]></category><category><![CDATA[ec2]]></category><dc:creator><![CDATA[Andra Malmén]]></dc:creator><pubDate>Sat, 08 May 2021 18:07:54 GMT</pubDate><content:encoded><![CDATA[<p>To transfer files between your local machine and an EC2 instance, you will need <strong>scp</strong> (secure copy) available on your machine.
The command to transfer files from your machine to your EC2 instance is:</p>
<pre><code><span class="hljs-attribute">scp</span> -i ./path_to_your_key_pair_cer_files ./path_to_file_which_you_want_transfered ec<span class="hljs-number">2</span>-user@public_IPv<span class="hljs-number">4</span>_dns_of_your_ec<span class="hljs-number">2</span>_instance:path_in_EC<span class="hljs-number">2</span>
</code></pre><p>The command to transfer files from your EC2 instance to your local machine is very similar, you need to swap the two paths:</p>
<pre><code><span class="hljs-attribute">scp</span> -i ./path_to_your_key_pair_cer_files ec<span class="hljs-number">2</span>-user@public_IPv<span class="hljs-number">4</span>_dns_of_your_ec<span class="hljs-number">2</span>_instance:path_in_EC<span class="hljs-number">2</span> ./path_to_file_which_you_want_transfered
</code></pre>]]></content:encoded></item><item><title><![CDATA[How to connect to an EC2 instance]]></title><description><![CDATA[Start by creating an EC2 instance in AWS console. This can be done from the EC2 dashboard, then select Instances. In order to create a new instance, click the "Launch instances" button.
First, you will need to select an AMI. You can select any AMI fr...]]></description><link>https://andramalmen.com/how-to-connect-to-an-ec2-instance</link><guid isPermaLink="true">https://andramalmen.com/how-to-connect-to-an-ec2-instance</guid><category><![CDATA[AWS]]></category><category><![CDATA[ec2]]></category><dc:creator><![CDATA[Andra Malmén]]></dc:creator><pubDate>Fri, 07 May 2021 20:24:30 GMT</pubDate><content:encoded><![CDATA[<p>Start by creating an EC2 instance in AWS console. This can be done from the EC2 dashboard, then select Instances. In order to create a new instance, click the "Launch instances" button.</p>
<p>First, you will need to select an AMI. You can select any AMI from the list, but for learning purposes you might want to choose an AMI from the free tier, which will have under its name a tag with "Free tier eligible" label. Once that you have selected the AMI, click on the "Select" button which will take you to the second step, choosing the Instance Type. There are several other steps if you choose to customize your instance, but for the purpose of this tutorial, you can choose "Review and launch" option, which will take you to the last step. If you are happy with the instance configuration, click on the "Launch" button which is at the right bottom of the page. A popup window will appear. If you have created beforehand a key pair and you still have the files saved on your machine, choose your key pair from the list, otherwise create a new one and Download the key pair .cer file. You can launch your instance now, by clicking "Launch instances" button.</p>
<p>In your terminal, navigate to the directory where your key/pair was downloaded. The key pair is publicly viewable, so you will need to modify its permissions, by running the following command:</p>
<p><code>chmod 400 key-pair-filename.cer</code> </p>
<p>In AWS console, visit Instances pages and select the instance you have just created. At the bottom of the page, you can find "Instance summary" information. From here, copy the "Public IPv4 DNS", which will have the following format:</p>
<p>ec2-xxx-xxx-xxx-xxx.aws-region.compute.amazonaws.com</p>
<p>You can now connect to your EC2 instance, by running:</p>
<pre><code><span class="hljs-attribute">ssh</span> -i ./key-pair-filename.ce ec<span class="hljs-number">2</span>-user@the_public_ipv<span class="hljs-number">4</span>_dns_found_above
</code></pre><p>Congratulations, now you are connected and you can interact with your instance.</p>
]]></content:encoded></item><item><title><![CDATA[How to connect your Python application to MySQL Database]]></title><description><![CDATA[Setup your database
If you do not have MySQL or MariaDB on your machine, you should start by downloading and installing one of them. Once they are installed on your computer, make sure the MySQL server is running. Using the terminal of your choice, l...]]></description><link>https://andramalmen.com/how-to-connect-your-python-application-to-mysql-database</link><guid isPermaLink="true">https://andramalmen.com/how-to-connect-your-python-application-to-mysql-database</guid><category><![CDATA[Python]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[MySQL]]></category><category><![CDATA[database]]></category><dc:creator><![CDATA[Andra Malmén]]></dc:creator><pubDate>Fri, 26 Mar 2021 21:17:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1616823380438/sCmzEG4n6.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="setup-your-database">Setup your database</h2>
<p>If you do not have MySQL or MariaDB on your machine, you should start by downloading and installing one of them. Once they are installed on your computer, make sure the MySQL server is running. Using the terminal of your choice, log in as root to create a database and a user for your application:</p>
<pre><code><span class="hljs-attribute">mysql</span> -u root
</code></pre><p>After running the above command you will be asked to enter your root password if you had chosen one when installing the server, otherwise just press <em>Enter</em>. If you have logged in successfully, you will be taken to the MySQL console prompt.</p>
<p>Create the database for your application:</p>
<pre><code><span class="hljs-keyword">create</span> <span class="hljs-keyword">database</span> my_database;
</code></pre><p>Create a user for your application:</p>
<pre><code><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">USER</span> <span class="hljs-string">'my_user'</span>@<span class="hljs-string">'localhost'</span> <span class="hljs-keyword">IDENTIFIED</span> <span class="hljs-keyword">BY</span> <span class="hljs-string">'my_password'</span>;
</code></pre><p>Give user full rights to your application database:</p>
<pre><code><span class="hljs-keyword">GRANT</span> <span class="hljs-keyword">ALL</span> <span class="hljs-keyword">ON</span> my_database.* <span class="hljs-keyword">TO</span> <span class="hljs-string">'my_user'</span>@<span class="hljs-string">'localhost'</span>;
</code></pre><p>After each command, you should see a console message, which will contain the text "Query OK" if everything went well. 
Now that you have created the user and the database for your application, you can quit the MySQL prompt.</p>
<pre><code>quit
</code></pre><h2 id="write-your-python-program">Write your python program</h2>
<p>For this demo, I am using python3 and I am using a virtual environment. If you want to read more about Virtual environments, check the <a target="_blank" href="https://docs.python.org/3/tutorial/venv.html">docs</a>.
I will use pyenv as the name of my virtual environment as it is one of the default names accepted by my favorite editor, <a target="_blank" href="https://code.visualstudio.com/">VS Code</a> as venv folder.</p>
<p>In your terminal, create your virtual environment:</p>
<pre><code>$ python3 -m venv pyenv
</code></pre><p>Activate the virtual environment:</p>
<pre><code><span class="hljs-selector-tag">pyenv</span> \<span class="hljs-selector-tag">Scripts</span>\<span class="hljs-selector-tag">activate</span><span class="hljs-selector-class">.bat</span> <span class="hljs-comment">// for Windows</span>
<span class="hljs-selector-tag">source</span> <span class="hljs-selector-tag">pyenv</span>/<span class="hljs-selector-tag">bin</span>/<span class="hljs-selector-tag">activate</span> <span class="hljs-comment">// Unix or MacOS</span>
</code></pre><p>Now you are using a virtual environment for python3. To confirm, test in your terminal:</p>
<pre><code><span class="hljs-string">$</span> <span class="hljs-string">python</span> <span class="hljs-string">--version</span>
<span class="hljs-string">Python</span> <span class="hljs-number">3.8</span><span class="hljs-number">.2</span>
</code></pre><p>Install the MySQL database driver for Python:</p>
<pre><code>$ pip install mysql-connector-python
</code></pre><p>Open your python file and let's connect to the database we created earlier(finally!)</p>
<pre><code>// <span class="hljs-keyword">import</span> MySQL <span class="hljs-keyword">database</span> driver
<span class="hljs-keyword">import</span> mysql.connector

// <span class="hljs-keyword">create</span> a <span class="hljs-keyword">dictionary</span> variable that contains the information needed <span class="hljs-keyword">to</span> <span class="hljs-keyword">connect</span> <span class="hljs-keyword">to</span> your <span class="hljs-keyword">database</span>.
config = {<span class="hljs-string">'host'</span>: <span class="hljs-string">'127.0.0.1'</span>, <span class="hljs-string">'user'</span>: <span class="hljs-string">'my_user'</span>,
          <span class="hljs-string">'password'</span>: <span class="hljs-string">'my_password'</span>, <span class="hljs-string">'database'</span>: <span class="hljs-string">'my_database'</span>}

// <span class="hljs-keyword">connect</span> <span class="hljs-keyword">to</span> the MySQL <span class="hljs-keyword">server</span> <span class="hljs-keyword">and</span> save the <span class="hljs-keyword">connection</span> reference <span class="hljs-keyword">in</span> a variable named `conn`
conn = mysql.connector.<span class="hljs-keyword">connect</span>(**config)

// <span class="hljs-keyword">create</span> a <span class="hljs-keyword">cursor</span> <span class="hljs-keyword">to</span> communicate <span class="hljs-keyword">with</span> the <span class="hljs-keyword">database</span>
<span class="hljs-keyword">cursor</span> = conn.<span class="hljs-keyword">cursor</span>()

// send the <span class="hljs-keyword">CREATE</span> command <span class="hljs-keyword">to</span> the <span class="hljs-keyword">server</span>. <span class="hljs-keyword">In</span> this <span class="hljs-keyword">case</span>, will <span class="hljs-keyword">create</span> a simple <span class="hljs-keyword">table</span> <span class="hljs-keyword">called</span> demo
<span class="hljs-keyword">cursor</span>.<span class="hljs-keyword">execute</span>("""CREATE table demo (
id int auto_increment primary key,
ts timestamp default current_timestamp,
greeting varchar(128) not null)""")

// send <span class="hljs-keyword">SHOW</span> command <span class="hljs-keyword">to</span> <span class="hljs-keyword">server</span> <span class="hljs-keyword">to</span> display <span class="hljs-keyword">all</span> <span class="hljs-keyword">tables</span> <span class="hljs-keyword">from</span> the <span class="hljs-keyword">database</span>
<span class="hljs-keyword">cursor</span>.<span class="hljs-keyword">execute</span>("""SHOW tables""")

// ask <span class="hljs-keyword">for</span> the result <span class="hljs-keyword">of</span> the above command <span class="hljs-keyword">and</span> print them
print(<span class="hljs-keyword">cursor</span>.fetchall())

// send <span class="hljs-keyword">DROP</span> <span class="hljs-keyword">table</span> command <span class="hljs-keyword">to</span> <span class="hljs-keyword">server</span> <span class="hljs-keyword">to</span> <span class="hljs-keyword">delete</span> `demo` <span class="hljs-keyword">table</span>
<span class="hljs-keyword">cursor</span>.<span class="hljs-keyword">execute</span>("""DROP table demo""")

// clean <span class="hljs-keyword">after</span> you
<span class="hljs-keyword">cursor</span>.<span class="hljs-keyword">close</span>()
conn.<span class="hljs-keyword">close</span>()
</code></pre><p>By running the application from above, your terminal should display a tuple:</p>
<pre><code>[('demo',)]
</code></pre><h2 id="conclusion">Conclusion</h2>
<p>In this short tutorial, you have created a database and a user from MySQL console prompt, you have used a virtual environment for your application and you have learned how to connect your Python application to MySQL (or MariaDB)  database.</p>
]]></content:encoded></item></channel></rss>