cPanel & WHM Version 82 has been released, and brings a slew of great updates. Take a look at what is included, and then upgrade today!
 

cPanel and Remote MySQL Server should be able to use private IPs

Jeff Nolan shared this idea 5 years ago
Open Discussion

Typically the cPanel server is public-facing which means it's hostname resolves to a public IP address. There is a strong use case to want to connect a remote MySQL server using a private network interface. However, the MySQL server can only resolve the cPanel server's hostname to it's public IP address which means the DB user accounts created on the MySQL server are given grants to the wrong IP address.


The work-around is to grant privileges on the private IP address manually. But in a shared hosting environment where accounts can be provisioned automatically this would pose a problem. It would useful for cPanel to support remote connections to MySQL using private IPs.

Best Answer
photo

This functionality can be achieved without the need to manually add new grants each time a new account/MySQL user is provisioned.


The deployment would be to modify /etc/hosts on the MySQL Remote Server such that the resolution is forced for the MySQL Remote Server to resolve the cPanel & WHM server's hostname to the private IP desired. This would solve the issue such that the /etc/hosts file change is the only change necessary. No further modifications are required.


Example:


cPanel & WHM server with hostname 'server.hosting.com', public IP 123.123.123.123, and private ip 192.168.0.1

MySQL Remote Server with private ip 192.168.0.2


On the MySQL Remote Server, /etc/hosts would be modified to include:


192.168.0.1 server.hosting.com


This does not resolve the fact that the public IP will still receive a grant, but the hostname based grant will be applicable.


In other words, this is not a solution for this feature request -- but a potential temporary workaround in the interim.

Comments (2)

photo
1

This functionality can be achieved without the need to manually add new grants each time a new account/MySQL user is provisioned.


The deployment would be to modify /etc/hosts on the MySQL Remote Server such that the resolution is forced for the MySQL Remote Server to resolve the cPanel & WHM server's hostname to the private IP desired. This would solve the issue such that the /etc/hosts file change is the only change necessary. No further modifications are required.


Example:


cPanel & WHM server with hostname 'server.hosting.com', public IP 123.123.123.123, and private ip 192.168.0.1

MySQL Remote Server with private ip 192.168.0.2


On the MySQL Remote Server, /etc/hosts would be modified to include:


192.168.0.1 server.hosting.com


This does not resolve the fact that the public IP will still receive a grant, but the hostname based grant will be applicable.


In other words, this is not a solution for this feature request -- but a potential temporary workaround in the interim.

photo
1

up vote10down voteaccepted

Just query a host that returns your IP address:

$externalContent = file_get_contents('http://checkip.dyndns.com/');

preg_match('/Current IP Address: \[?([:.0-9a-fA-F]+)\]?/', $externalContent, $m);

$externalIp = $m[1];or, set up a service that simply echoes just the IP, and use it like this:

$externalIp = file_get_contents('http://phihag.de/ip/');Note that in an IP network with one or more NATs, you may have multiple external IP addresses. This will give you just one of them.

Also, this solution of course depends on the remote host being available. However, since there is no widely implemented standard (no ISP and only some home routers implement UPnP), there is no other way to get your external IP address. Even if you could talk to your local NAT, you couldn't be sure that there isn't another NAT behind it.


ref: http://stackoverflow.com/questions/23110995/how-to-get-public-ip-address-using-php