A database error can occur for many reasons. It's usually the result of an incorrect database address, username, or password. It's likely not a timeout failure, since WordPress has a separate error for that. Here are two common issues:
Since you have intermittent failures, this likely points to an issue with the database server itself. If you are on a shared host, it's possible they have overloaded their servers. Your best option is to contact your host's technical support and have them verify the mySQL load and move you to a different server if needed.
Without know more details about your server configuration it's hard to diagnose further.
It might also be an issue with a caching plugin, do you have one?
You could have cache miss. This occurs when something is looked up in the cache and is not found. In this scenario, WordPress then has to generate the content from the database and save it to cache. When configured correctly, a cache miss should not produce an error.
Without know more details about your plugins it's hard to diagnose further.
It's possible that your site has been compromised. You should compare your wp-config.php file to wp-config-sample.php. Only the values of the existing named constants should be different. There should be no additional code or defined values.