问题
I have the following php code to connect to my mysql DB.
mysql_connect( "myserver.com" , "root", "redhat","datastore") or die(mysql_error());
when I ran this code I got the error saying:
Warning: mysql_connect(): Host '10.21.21.10' is not allowed to connect to this MySQL server in C:\xampp\htdocs\inventory\net.php on line 20
Host '10.21.21.10' is not allowed to connect to this MySQL server
But the IP of myserver.com when I did ping myserver.com in command prompt it gave 10.25.15.95
,so I Modified the code to:
mysql_connect( "10.21.21.10" , "root", "redhat","datastore") or die(mysql_error());
the same error repeated.
Then i changed my code to:
mysql_connect( "10.25.15.95" , "root", "redhat","datastore") or die(mysql_error());
the error was
Warning: mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\inventory\net.php on line 20
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Please help me in solving this..
Thanks in advance...
回答1:
At your mysql server:
mysql> use mysql;
mysql> CREATE USER 'root'@'client_ipaddress' IDENTIFIED BY 'redhat';
mysql> GRANT ALL PRIVILEGES ON . TO 'root'@'client_ipaddress' WITH GRANT OPTION;
回答2:
You are running your PHP scripts locally but your database server is somewhere external (not on you local pc).
Most database servers are configured to not allow external connections (this is good).
Then depending on the configuration of your local machine, the outgoing request could also be blocked by a firewall, ...
That is why you can't connect. But you shouldn't be doing that in the first place.
NEVER EVER LINK YOUR PRODUCTION DATABASE TO YOUR DEVELOPMENT SCRIPT.
just don't!
What if you by accident write a wrong DELETE ALL query on you local machine and you accidently fire it. oops...
You are using xampp, this has a mysql server built in, then connect to it using localhost as your host.
Test on you local machine, the migrate everything to your myserver.com host
来源:https://stackoverflow.com/questions/23733626/php-mysql-connectivity-error