Error establishing a database connection wordpress. Is there any proper solution? I have tried many things but after 2 to 4 days same error occurs.
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.
Hello, all
This crash is most likely due to your system running out of memory. I’d suggest that you add a swap file to give yourself a bit more of a buffer. Check out this tutorial:
How To Add Swap on Ubuntu
You will most definitely need to upgrade your droplet as you’re running out of memory and your application/website needs more resources in order to continue to operate.
What you can also do is to 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:
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/
This was mini tutorial was posted from bobbyiliev in this question in our community: https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability
You can also create a simple bash script to check if MySQL is running and if not to restart it.
Run this script every 5 minutes using a cron job like this one:
Hope that this helps! Regards, Alex
Hello, @avdi
Have you checked the MySQL error_log in order to see if the server is running out of memory or if the issue is with to many MySQL connections? As per the tutorial you can run this command in order to check if the server have recently run out of memory:
zgrep will search through log files, including older log files that have been archived as compressed .tar.gz files. We’re searching for lines that contain allocate memory, in any error.log* file in the
/var/log/mysql/
directory.You can always login and check the MySQL
error_log
when the issue occurs using tail to either check the last 100 entries in the log or monitor the log in real time:and monitor it in real time:
Hope that this helps! Regards, Alex
You can use this script to restart your server whenever there is database connection error. https://github.com/Gurnoor30196/Launch-Mysql
Thank you. I have already 1gb/30gb server. This error never occurs in shared hosting.
@jgojariya @newbie
Although swap is generally recommended for systems utilizing traditional spinning hard drives, using swap with SSDs can cause issues with hardware degradation over time. Due to this consideration, we do not recommend enabling swap on DigitalOcean or any other provider that utilizes SSD storage. Doing so can impact the reliability of the underlying hardware for you and your neighbors. This guide is provided as reference for users who may have spinning disk systems elsewhere.
…
If you’re on a 512MB Droplet and you’re seeing MySQL crash, I would recommend upgrading to a 1GB or 2GB Droplet. Running Apache/NGINX, MySQL/MariaDB, and PHP on a 512MB Droplet requires a great deal of optimization and is really only meant for very low traffic scenarios.
@jgojariya most probably your out of resources and your mysql is crashing for that. switch to a higher plan if you are on 5$ one or add some swap space https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04