I am trying to connect mysql database using php. But it is running fine on local machine but not when hosted online and error appeared as mysqli_connect(): (HY000/2002): Co
I think you have to white list your hosting server's ip address to your DB server.
So your DB server will accept requests from your hosting server.