Running MariaDB/MySQL on small VPS

I had my databases down twice in a month with the following error message:

[ERROR] mysqld: Out of memory

Compared to shared hosting services that we used to have, the tiny and cheap cloud VPSs are amazing. I am a user and big fan of the service as I don’t have any personal projects that require AWS by now.

These servers are convenient and comfortable as you can customize, install and configure as you wish. When everything is to your liking you can take snapshots as backup or to create duplicates of your server.

The only problem is that some of the softwares needed to run a fully web server comes with predefined configuration files. They run fine on their own but you need to take in mind that your VPS have a limited RAM to work with. MySQL and MariaDB by default will allocate around 400MB, which is reasonable to a normal server, but not to a limited virtual instance running 24/7.

I will comment on the measures it took to get around this problem. As usual the following steps are for Ubuntu 14.04, your steps may vary depending on your distro and version. You may find a my.cnf over the internet that fits your server’s specifications.

Disable Performance Schema

In my opinion, performance schema only has a use if you or a DBA wants to monitor the database activity. To disable it edit the default MySQL configuration file, in this case /etc/mysql/my.cnf, and edit the following line under the [mysqld] block (if it doesn’t exist, create the line).

performance_schema = off

MyISAM tables

InnoDB tables have a huge impact in performance and memory usage, if you don’t use its resources you may consider to use MyISAM. To do so you need edit the following variables under the [mysqld] block of the /etc/mysql/my.cnf file:

default-storage-engine=MyISAM
default-tmp-storage-engine=MyISAM

If you don’t need InnoDB resources at all you can disable it adding the following line to your my.cnf:

skip-innodb

You may need to change your existing tables to MyISAM. The following ShellScript can do that for you.

#!/bin/bash

MYSQLCMD=mysql

for db in `echo show databases | $MYSQLCMD | grep -v Database`; do
        for table in `echo show tables | $MYSQLCMD $db | grep -v Tables_in_`; do
                TABLE_TYPE=`echo show create table $table | $MYSQLCMD $db | sed -e's/.*ENGINE=\([[:alnum:]\]\+\)[[:space:]].*/\1/'|grep -v 'Create Table'`
                if [ $TABLE_TYPE = "InnoDB" ] ; then
                        mysqldump $db $table > $db.$table.sql
                        echo "ALTER TABLE $table ENGINE = MyISAM" | $MYSQLCMD $db
                fi
        done
done

Swap

Swap isn’t recommended, at least by DigitalOcean that uses SSDs, but I use it as a fail-safe measure. The following commands will create a swapfile and use it as swap space in your machine, run as root:

fallocate -l 256M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

To keep your changes permanent don’t forget to add the following line to your /etc/fstab file:

/swapfile   none    swap    sw    0   0

Conclusion

Cloud VPSs are amazing considering their cost, reliability and convenience, keep in mind that you will need to tweak one thing or other. If you need a more robust solution I recommend Amazon’s RDS or upgrading your instance plan.

Don’t forget to restart your MySQL/MariaDB service after any changes to the my.cnf file.

service mysql restart

Reviving dead Ni-MH batteries

I’m using the Microsoft Desktop 800 for almost 4 years. In an effort to reduce the E-waste I’m using rechargeable batteries. Unfortunately two of them just died a couple of weeks ago, it occurred to me to find a way to recover them. After some time reading I found a way to bring them back to life and I will tell a little about that.

img_20161023_132738910First try charging your batteries two or three times, if they still don’t charge up, you can check with your multimeter in the dc voltage setting. You should get a value like 0 volts, but I read somewhere that some chargers start charging after some threshold, so you may read something below 0.8 volts.

Battery charger and batteries in parallelAfter you make sure that your batteries are dead, you can try the procedure. Do this the same way you do with a car battery, connect the negative terminal of one working and fully charged battery to the negative of your dead one and do the same with the positive terminals. Keep them connected for 20 to 30 seconds. I used my (unplugged) charger and a coin as the negative are a common ground.

Charged batteries multimeter readingWith a full charge I got a 1.38 volts reading and my two batteries working again.

Getting shit done

There is nothing more frustrating than having nice ideas and halt your work because you simply, at some point, lack motivation to move forward.

I deal with this kind of situation since I was only 10 years old and started some projects with programming languages. Never finished any of those projects, but learned a lot though.
Not that I am a perfectionist or something like that, but I never had pleasure releasing something that is half-assed. From my point of view, everything can be improved at some point.

The problem with that kind of attitude is that you end up having nothing to show and no feedback from others. Right now I am trying to work on my side projects (even this blog) and release what I can in some way. If there is a window to improve I will do it later, because right now I think it worth more to have something out there (used by someone, to get feedback or for any other purpose) than trying to make something flawless on the first try.

When developing a project you will end up improving your skills (programming, drawing, composing, painting) over time and something done like 12 months ago will look like shit because you got better. My point is: release. If someone criticise you try to look what he did, his projects, his skills. Is he that good? In my opinion, people who are really good don’t smother who is beginning.

So… that is how I am trying to deal with all the things now. I am trying to get shit done. That is how I am dealing with my motivation.

Other than that I try to work with a schedule, goals, tasks and deadlines. That helps to keep track of my things and it’s satisfying to see how it improve.