MySQL Error 1040 – Too Many Connections and No Space Left on Device

disk-spacemax-connectionsMySQLmysql-8.0

My app crashes because there are too many connections open and mysql does not make me log in as root showing the same problem.
After restarting mysql, it does not restart because of no space on disk

Best Answer

here there is a full guide on how I solved the issue:

I tried to log in as root with the command:

sudo mysql -u root -p 

But it did not work for me, mysql kept giving the error:

ERROR 1040 (HY000): Too many connections

After some searches I tried

service mysqld restart --max-connections=500

This way stopped mysqld, which wasn't able to restart because at the same time my disk was full and receiving:

 Error: 28 (No space left on device)

Then, in order to inspect on ubuntu where my space was occupied I used the command:

du -h --max-depth=1 | sort -hr

That let me know all the space was occupied by binlog files of mysql. I couldn't remove files by hand, it is not recommended procedure. But I did not have anything to remove and I was not able to start mysql to purge the binlog files. Then I run the following command which gave me enough space to restart mysql:

apt-get clean
service mysql restart || systemctl restart mysqld

At this point I was able to login to mysql and purge the binlog files with:

PURGE BINARY LOGS BEFORE '2021-03-03 22:46:26';

But this is not a long term solution. To avoid the problem in the future I did:

SET GLOBAL binlog_expire_logs_seconds = 432000; // 5 days of binlog files

I am going to improve this answer when I solve the maximum connection issue for a long term solution.