Question

How to Tweak MySQL/MariaDB Configuration for Increased Performance and Stability?

Posted January 2, 2020 6.7k views
Linux BasicsMySQLMariaDB

Hi all,

Do you have any suggestions on how to make sure that MySQL/MariaDB is configured properly for optimal performance and stability?

Thanks!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
2 answers

Hello,

What you could do is use the MySQLTuner script.

The MySQLTuner is a script written in Perl and allows you to quickly test your MySQL configuration and it gives you suggestions for adjustments to increase performance and stability.

According to the official GitHub page, it supports 300 indicators for MySQL/MariaDB/Percona Server in this last version.

To run the script you could do the following:

  • SSH to your Droplet
  • Download the script:
wget http://mysqltuner.pl/ -O mysqltuner.pl
  • Then execute it:
perl mysqltuner.pl

The script would run multiple checks against your MySQL instance, all checks done by MySQLTuner are documented here.

Also as stated in the official documentation, it is still extremely important for you to fully understand each change you make to a MySQL database server. If you don’t understand portions of the script’s output, or if you don’t understand the recommendations, you should consult a knowledgeable DBA or system administrator that you trust.

As a good practice make sure to always test your changes on staging environments before implementing them on your production database.

On the same note, if you want to have a worry-free MySQL hosting and focus on your application, I would recommend trying out the DigitalOcean Managed Databases:

https://www.digitalocean.com/products/managed-databases-mysql/

Hope that this helps!
Regards,
Bobby

Hello, @umpire

I will definatelly recommend you to add a swap file if you haven’t done this yet and also use the MySQL tuner script and see if the MySQL configuration needs an improvement.

You can also create a simple bash script to check if MySQL is running and if not to restart it.

#!/bin/bash

# Check if MySQL is running
sudo service mysql status > /dev/null 2>&1

# Restart the MySQL service if it's not running.
if [ $? != 0 ]; then
    sudo service mysql restart
fi

Run this script every 5 minutes using a cron job like this one:

 */5 * * * * /home/user/scripts/monitor.sh > /dev/null 2>&1

Hope that this helps!
Regards,
Alex

  • Alex!!! Thank you!!! This is exactly what I need!!!

    Could I ask you to create a step-by-step tutorial on what I would need to create this? I’m using digital ocean… without the level of programming skills I should have when using it.

    I am on a fairly new droplet created using the marketplace Wordpress deployment.

    It would be a godsend! I think there are TONS of people on Digital Ocean who need this!

    Sincerely,
    Rai

    • Hello, @raimanau

      You can first access your droplet via ssh and then execute the MySQL tuner script and configure the bash script as well:

      https://docs.digitalocean.com/products/droplets/how-to/connect-with-ssh/

      Once you’re connected you can use any text editor like nano or vim to create the bash script. You can copy/paste the code snippet I’ve already provided in the previous reply and then just save the file.

      You can use a corn job to make to execute the script automatically every 5 or more minutes. You can follow our tutorial bellow:

      https://www.digitalocean.com/community/tutorials/how-to-use-cron-to-automate-tasks-ubuntu-1804

      Regards,
      Alex

      by Mark Drake
      by Shaun Lewis
      Cron is a time-based job scheduling daemon found in Unix-like operating systems, including Linux distributions. This guide provides an overview of how to schedule tasks using cron's special syntax on a machine running Ubuntu 18.04. It also goes over a few shortcuts one can use to make job schedules easier to write and understand.
      • Thanks @alexdo !

        I apologize for the very late response despite you generously providing advice.

        I think I might have been able to do it? Would be very happy if you can help confirm this.

        This is what I did:

        1. Went to xxx.xxx.xx.xxx/usr/local/bin/ and then created a file there named “monitor.sh” and copy pasted the script you mentioned in your comment above. (I found suggestions on other sites to save the script there, since I couldn’t find the /home/user/scripts/ directory.)
        2. Installed cron via ssh
        3. Used the command “crontab -e”, opened nano from options provided, and then added the following script (edited based on where I created the script) to the bottom of the document under all the comments */5 * * * * /usr/local/bin/monitor.sh > /dev/null 2>&1
        4. Exited nano while saving it to the file that directory automatically suggested by the system.

        Does that look right to you?

        Would love for the support until I can get it working!