If you are trying to connect to a mysql database remotely, you need to make sure the remote IP address is allowed access to the database. To do this, run the following command.
GRANT ALL ON `$db`.* TO ‘$user’@’$host’ IDENTIFIED BY ‘$pass’;
Replace $db with the database name
Replace $user with the database username
Replace $host with the IP address you are going to connect from
Replace $pass with the password to the database
After the previous command, run flush privileges;
You may also need to allow the IP address through port 3306 on your firewall (port number can be changed, but 3306 is the default MySQL port).