Reduce RAM usage of MySQL in Ubuntu


Recently I came across a unique challenge to optimize RAM usage of MySQL in Ubuntu server.

I had a web application server running on a t2-micro instance on Google Cloud Platform. If you are familiar with t2-micro instances in GCP or AWS cloud, you would be aware that these are very low memory compute instances with just 1GB RAM. I was required to use a MySQL server in this instance having Ubuntu 24.0 Operating system.

In the past I had MySQL server running on Ubuntu 16 and 18 without much of an issue. However in Ubuntu 24, I started experiencing some performance degradation. I solved this issue by reducing the RAM usage of MySQL server.

If you want to optimize the RAM usage of MySQL server, you can follow these steps.





Check current RAM Usage

Start by checking the RAM usage using the below command in terminal:

$ top

This will give the list of processes with high memory usage. Here, I found MySQL service to be using almost 40% of the RAM, i.e. it was using close to 400MB in my case.

In a small instance of 1GB RAM, system itself will consume a lot of RAM. Hence, 400MB usage by MySQL seems to be relatively high considering the available resources. Hence we need to optimize this by configuring MySQL server.



Configure MySQL server

Type the below command:

$ cd /etc/mysql
$ ls

You will be able to view the list of files in this folder. There will be a file called “my.cnf”. Type below command to edit this configuration file.

$ sudo nano my.cnf

Once the file opens up, add these lines listed below:

[mysqld]
performance_schema = 0


Restart MySQL server

Now click on Ctrl+X to save and close the file.

Now, we need to start the MySQL server so that the configuration changes start reflecting. Type below command to restart the MySQL server:

$ sudo systemctl restart mysql.service

If you see any error during restart, you may check the details by typing below command to view the status.

$ sudo systemctl status mysql.service


Verify the configuration

Once the MySQL server is restarted, you need to check whether these changes are reflecting or not.

For this visit MySQL console by using following command:

$ mysql –u root –p

Enter password in the next prompt. Now, once the MySQL console is loaded, type the below SQL query:

>> SHOW VARIABLES LIKE '%perf%';

In the results returned, you should see the first row as below:

>> performance_schema | OFF

Note that, earlier it was ON and now it is switched OFF.

Now exit from MySQL console and go back to Ubuntu terminal.

>> exit

Now check the RAM usage again.

$ top

You should see a reduction in the RAM being used by MySQL service.

In my case, it was using 400MB RAM and after disabling the performance schema, it reduced the RAM usage to 190 MB. Though some people also claim that it reduces even further upto 130MB.



What did we do?

If you are wondering what is the “performance_schema” used for, then read on…

Performance schema is basically used for performance monitoring of the MySQL server. It may be useful when you want to diagnose performance issues and resource usage. Though it is a good practice to use this, however if you are running low memory servers for learning & personal projects, then it is perfectly fine to run the database server without this. It will not affect the regular performance of your web application using the database.

Hope this post was helpful, do let me know your thoughts…

Follow Me on Social Media

Advertisement
FREE Sales CRM Software

Fully customizable CRM Software for Freelancers and Small Businesses

Signup for Free

Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount