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.
Start by checking the RAM usage using the below command in terminal:
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.
Type the below command:
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.
Once the file opens up, add these lines listed below:
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:
If you see any error during restart, you may check the details by typing below command to view the status.
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:
Enter password in the next prompt. Now, once the MySQL console is loaded, type the below SQL query:
In the results returned, you should see the first row as below:
Note that, earlier it was ON and now it is switched OFF.
Now exit from MySQL console and go back to Ubuntu terminal.
Now check the RAM usage again.
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.
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…
Fully customizable CRM Software for Freelancers and Small Businesses