Do you get the error “Error establishing a database connection” message often?
If you do then there the possible reason could be MySQL service crash on your DigitalOcean Droplet.
Basically, the reasons for MySQL crash are different and may be due to lack of resources on the server to bad website scripts.
Let’s check the common reasons that cause MySQL crashing and tips to fix them.
Reasons for MySQL crashing in DigitalOcean Droplet
A MySQL server crash is really critical because all your websites will be affected by this. Therefore, it needs a quick solution.
To resolve this issue, it is important to find out the reason for MySQL failure.
Check below the different reasons lead to the crashing of MySQL service on the Droplet.
1. Buggy scripts
As per our experts that manage DO Droplets, there are often buggy php scripts seen overloading the server. These scripts comprise of bad mysql queries that might consume server resources. In addition, they simply keep on running on the server without any result.
Same is the case with WordPress websites, often certain plugins lead to MySQL failure. This further gives rise to database errors on the website.
2. Lack of server resources
The next reason for MySQL crash could be lack of server resources.
When Digital Ocean provides minimal configuration droplets that are stuffed with too many websites, it might lead to MySQL crash. These droplets comprise of a total 512 MB memory. This low memory won’t be able to handle high traffic websites, manage server processes, etc. This results in ‘Out of Memory’ issues in droplet.
In case, the physical memory (RAM) is full, PHP scripts use a buffer memory called “Swap” to process the request.
After we plan to use this buffer space, we need to assign this Swapspace when the initial set up of Droplet is done.
If there isn’t an additional space created in the Droplet, there can be lack of memory leading to MySQL crash.
3. Malicious attacks
It rarely happens that there high resource usage by MySQL is because of valid website access. Attacks involving xmlrpc.php file of WordPress can also be one of the reasons behind the MySQL crash. In this attack, hacker takes the efforts to break into the admin panel of WordPress by using too many hits to the website.
Due to this, the server fails to handle the large number of requests and can crash.
4. Broken databases
Broken databases can also lead to database errors in the website. The reason behind this to happen is the database files corruption. Mostly, disk full issues, permission problems, etc. can also lead to MySQL crash.
Ideas to Fix MySQL Crashing in DigitalOcean Droplets
The solution to fix the crash varies for different reasons. Let’s see these fixes in detail.
1. Assign Adequate Server Resources
In this solution, you are allocated with adequate amount of resources on the droplet. This mainly depends on the server’s functionality. The Droplet should comprise of enough processing power, memory, disk space, swap space, etc. on the server.
The DigitalOcean droplets don’t have swap space created by default. Here, our technical support team will check for the resources on the server and preserve adequate percentage of memory for swap. For example, if a droplet comes with 4GB memory, based on the type of websites, a swap space of 1GB would be perfect.
But, if constant use of swap space on the server is detected, it is recommended to permanently upgrade the memory of Droplet.
2. Service Optimization
Even though the server has enough resources, if the MySQL configuration is wrong, it can lead to crash. Therefore, if there is a frequent MySQL crash on a droplet, slow queries are enabled in MySQL by our experts. You will find those logged to the mysql log file available at /var/log/mysql/log-slow-queries.log. Due to this, it becomes easy to identify the specific sites that cause the problem.
If there is a problem with the website script, our team helps to fix it. This is also applicable to the trouble cause by the plugins on WordPress websites.
Moreover, the MySQL service variables like max_connections, query_cache_limit, table_cache, etc. are also optimized by our team.
To get the best results, this optimization is extended to the web server too. It includes changes such as restricting values such as Max_clients, StartServers in Apache configuration, selecting the correct processing module, etc.
3. Prevent Malicious Attacks
When an attack is observed on a droplet, proper firewall is implemented to protect these malicious attacks. For instance, in xmlrpc attack, we disable xmlrpc method on the server as a whole, install plugins to stop attack, etc.
Also, our experts recommend the customers to proactively set proper defending mechanisms in the droplets.
4. Check the Database Periodically
Always do a periodic check of the databases running on the server. With this you can track and repair corrupt tables, if any. Also, we do a scheduled database check using the cron jobs. This comprises of checking the server disk space usage, MySQL log file size, etc. as well. This will help to omit the MySQL crash due to disk full issues.
Lastly, it can be said that MySQL crashing in DigitalOcean droplets happens mainly due to bad MySQL configuration, scarcity of resources, buggy scripts, etc. If still you face any issue while resolving the MySQL crash, you can always contact our support team.
Also Read: MySQL Database (cPanel)