'Php not able to connect to MySQL workbench when using IP as host, but works fine when using localhost

This is the error I get when using php code

"mysql_connect($host,$user,$password)", where $host="IP_of_my_system" :

Warning: mysql_connect(): Host 'xxx' is not allowed to connect to this MySQL server in C:\xampp\htdocs\mysqltest\insert.php on line 4

But when using localhost instead of IP, it works fine.

Why is this behavior?



Solution 1:[1]

Please Mention the port no like 192.168.0.1:3306 ( by default mysql port ) in $hostname.

For server port no contact your server administratior

Solution 2:[2]

Two possible reasons for your problem:

1) Your firewall is blocking IP_of_my_system:3306

2) you haven't configured proper access in MySql.

For the item #1, check your system.

For the #2, in Workbench, enter as administrator of your database. Then, click on "Users and Privileges". In this page, you must grant to your user (MySQL user, not Windows user!), the permissions to login from an address or a network. This permission is "per host" and "per schema". In the following image, you can see that the user "testmvc" may login form any host (%) and from localhost: MySQL Workbench Users and Pribileges - Server Access Management

In the next image you can see the "Schema Privilege" tab: MySQL Workbench Users and Pribileges - Schema Privileges where you can see that the user "testmvc", from any host (%) may access the "testmvc" schema with the specified privileges.

The combination of access and privileges grants your user to access the MySQL server and operate on it.

Solution 3:[3]

I had a similar problem and only worked solution is creating a new user with same specification as old user. Strange, but hope it helps someone else with the same problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jayesh babel
Solution 2 AndrewQ
Solution 3 Ruwan Liyanage