user2323620
user2323620

Reputation: 133

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?

Upvotes: 0

Views: 1786

Answers (4)

Ruwan Liyanage
Ruwan Liyanage

Reputation: 375

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.

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94672

Its quite simple if you understand how Userid's work in MySQL.

Each userid has a location associated with it from where that userid is allowed to logon from. This can be from anywhere % but normally its an ip address or set of ip addresses.

So the userid you are using is probably setup to be allowed access from localhost and 127.0.0.1 and maybe ::1 all of which are ways of addressing the PC that the MySQL Server is running on.

These should suffice so use localhost or 127.0.0.1 or change the userid definition in MySQL to be allowed to acess from 192.168.0.99 or whatever you ip address is, but using one of the alias's for this PC should be all you need. Using the alias's also helps when you move your code to another machine and/or MySQL server. I fyou use a specific IP it probably wont.

Upvotes: 1

AndrewQ
AndrewQ

Reputation: 420

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.

Upvotes: 0

jayesh babel
jayesh babel

Reputation: 21

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

Upvotes: 0

Related Questions