Error establishing a database connection when Everything is Right! [WordPress]

Error Establishing a Database ConnectionDatabase name, user and password were right in my wp-config.php file, but still I got ‘Error establishing a database connection’ even though the WordPress blog worked fine before.

This may help you if you were puzzled like me by this error & couldn’t figure out the solution.

For me the problem was in mysql tables. Checking them in phpMyAdmin I found one of them showing ‘In use’ in the ‘Type case’, this can be fix by selecting the table (or tables) & choosing ‘Repair table’.

This may solve the problem for you, but in my case the repair task failed because phpMyAdmin (or Mysql?) couldn’t write in the affected tables. That’s probably the cause of the whole problem. phpMyAdmin tells you what went wrong in the repairing process & shows you path(s) to the file(s) it couldn’t overwrite.

To fix this you need to change the ownership of the affected tables. For that you may need to use SSH to find the table(s) file(s) on your server, change their owner user to ‘mysql’ & their owner group to ‘root’. But I didn’t have to go through SSH & complicated commands, I just used the File editor inside Webmin (an open source software similar to cPanel). In my server all the sql files are located inside ‘/var/lib/mysql’. I selected the affected sql file, clicked the ‘Info’ button, changed user to ‘mysql’ & group to ‘root’.

Once you change ownership using SSH commands or some file editor, go back to phpMyAdmin & repeat the ‘Repair table’ trick, it should succeed this time & your site should live again…