Error establishing a database connection

The other day, my mam informed me that my website wasn’t working. She was right: it was showing an error message: Error establishing a database connection. Oh noes.

I googled the message; one result told me to check whether the error occurred just on the front end or both the back end and the front end. It was both.

I rummaged around the internet further and discovered I could start the database up again by typing

sudo /etc/init.d/mysql start

at the command line while sshing to the server. This fixed the whole problem — or so it seemed. A few hours later, when I looked again, the error message was back. I left it for a while.

Just now, I had another look at it. I restarted the database, as above, and then tried the repair database method, while the database was actually up and running. The results were that the database was all fine. I thought it would be: I had already used Sequel Pro to check it for myself. I should have maybe tried to connect to it before I restarted it, just to see. Oh well — I’m sure there’ll be another opportunity in the future.

The advice given on a Digital Ocean community page is to check the log file. Unfortunately, it doesn’t say how to find the log file.

I found the answer on a superuser page: do some complicated-looking greppy thing:

sudo ls -l `pgrep mysqld | sed 's@.*@/proc/&/fd/@'` | grep log

Note I had to use sudo, whereas the web page didn’t mention it.

Then, once I’d found it, I had to view it. I made up a command to do this, which surprisingly enough, worked:

sudo view /var/log/mysql/error.log

Sadly, this opened Vim, which I dislike as much as I dislike Emacs. I copied and pasted the text into a proper text editor, then tried pressing Ctrl+C to get rid of Vim. It told me to type :quit followed by Enter. That seemed to work.

The most useful lines in the error log are perhaps these:

InnoDB: The log sequence number in ibdata files does not match
InnoDB: the log sequence number in the ib_logfiles!
160613 9:08:00 InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.

However, I don’t know what what that really means or what caused the database to shut down abnormally. Le sigh.

I think I’ll just not worry about it unless it happens again. Hopefully, it won’t.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.