if it is randomly not reachable, it's most likely an issue with the server. Since it's a shared server, maybe the database server is not reachable everytime when it currently on heavy load (by other users?). Start with asking the hoster.
I guess it's not localhost what you have set as database host? if it is a hostname you've entered, maybe try to set it's IP instead,.. that'll skip the DNS check which could also be the problem here, if their DNS server has timeouts.