Connecting to Remote MySQL Server Using PHP

Connecting to remote MySQL server using PHP

  • firewall of the server must be set-up to enable incomming connections on port 3306
  • you must have a user in MySQL who is allowed to connect from % (any host) (see manual for details)

The current problem is the first one, but right after you resolve it you will likely get the second one.

Remotely connecting to a MySQL database

Use the supplied domain name ukld.db.5510597.hostedresource.com

Prepending the hostname with an IP as you are doing only changes the hostname and that is why it is failing to connect.

The hostname will be converted to an IP address behind the scenes for you. No need to do it yourself. Plus, hardcoding IPs is bad practice as they can change over time.

Remotely connecting to a MySQL database using PHP PDO

You need to remove http from the host and put the port number under port attribute.

Please try with this line :

$dbhost = "mysql:host=10.75.225.171;port=3601;dbname=apt";

PHP - can not connect to remote mysql database

Possible, that your $username do not have permissions to connect to database from server, where the PHP script is executed.

Try to create a user with permission to connect from remote server. Execute on MySQL server:

CREATE USER 'testuser'@'fh24-219.cybersales.cz' IDENTIFIED BY 'mypass';

Change 'fh24-219.cybersales.cz' to PHP server IP. If you will write '%' as server IP it will be accessible from anywhere (it is not recommended).

Updated:

If you do not know what permissions user have, execute SHOW GRANTS; on for e.g. PhpMyAdmin to see if remote IP is mentioned.

PHP not connecting to Remote MySQL

Found this to be an issue with SELinux that is not allowing httpd network connections.

Executing:

setsebool -P httpd_can_network_connect=1

Solved the problem.



Related Topics



Leave a reply



Submit