It usually indicates network connectivity trouble and you should check the condition of your network if this error occurs frequently. It might be because the MySQL server is bound to the loop-back IP (127.0.0.1 / localhost) which effectively cuts you off from connecting from “outside”. If this is the case, you need to upload the script to the webserver (which is probably also running the MySQL server) and keep your server host as ‘localhost’ Another common cause of connect timeouts is the reverse-DNS lookup that is necessary when authenticating clients. It is recommended to run MySQL with the config variable in my.cnf:
Open mysql configuration file named my.cnf and try to find “bind-address”, here replace the setting (127.0.0.1 OR localhost) with your live server ip (the ip you are using in mysql_connect function).
Restart service by command : service httpd restart
GRANT ALL PRIVILEGES ON yourDB.* TO ‘username’@‘YOUR_APPLICATION_IP’ IDENTIFIED BY ‘YPUR_PASSWORD’ WITH GRANT OPTION;