Hi all,

I am a student trying to access a database that is on a different server than where my web pages are stored. My webpages running PHP scripts are here ( through ) and my database is here ()
Here is the code that I implemented:

$con = mysql_connect(146.186.83.60, username, password) or die ('Error connecting to mysql');
mysql_select_db(databasename, $con);

The IP is where my database is located, but I get an error message:

Warning: mysql_pconnect() [function.mysql-pconnect]: Can't connect to MySQL server on '' (4) in /pass/home/14/t/tmv105/www/Connections/diehl.php on line 9

Fatal error: Can't connect to MySQL server on '' (4) in /pass/home/14/t/tmv105/www/Connections/diehl.php on line 9

Might I be being denied access due to a firewall, and if so, is there PHP code to work around this?

Dani AI

Generated

, is right about network allowlisting and @spthorn is right that there is no PHP-only workaround. To reach MySQL at 146.186.83.60 from your web servers (128.118.142.34-46), three things must all be true: (1) mysqld is listening on a non-loopback address, (2) a MySQL account is permitted from your web server IP(s), and (3) TCP 3306 is reachable end-to-end. If any one is missing, you will see the "Can’t connect to MySQL server" error even with correct code.

Practical checklist for the DB admin:

  • On the DB host, ensure mysqld binds externally (my.cnf: bind-address set to the server’s IP or 0.0.0.0; then restart). Safer is to bind to the specific interface rather than 0.0.0.0.
  • Create a DB user limited to your web server range and least privileges:
CREATE USER 'appuser'@'128.118.142.%' IDENTIFIED BY 'strongpass';
GRANT SELECT, INSERT, UPDATE, DELETE ON databasename.* TO 'appuser'@'128.118.142.%';
  • From a web server, test reachability before touching PHP:
    mysql -h 146.186.83.60 -u appuser -p -e "SELECT 1"
    (or) nc -vz 146.186.83.60 3306

If you do not control the firewall/config, you will need the DB admin to allow those IPs. One workable interim option, if you have SSH access to the DB host, is a tunnel (get approval first). Forward an unused local port on the web server to the DB’s 3306, then connect locally:

ssh -f -N -L 3307:127.0.0.1:3306 dbuser@146.186.83.60
$pdo = new PDO(
  'mysql:host=127.0.0.1;port=3307;dbname=databasename;charset=utf8mb4',
  'appuser','strongpass',
  [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

That keeps 3306 closed externally while still letting your PHP code talk to MySQL.

Recommended Answers

All 3 Replies

PHP code is correct but it seems that you are facing the problem due to firewall settings. Add your web server IP addresses (where you PHP script is stored) in firewall allowed list and also make sure that port 3306 is not blocked in the server firewall.

Kailash

I thank you for this information, but I do not have access to the server firewall connections where the database is stored. Is there possibly a work around without access?

(I think I may be unclear also as to why I would add my webserver addresses to the server where my PHP script is stored? My PHP scripts are on the webserver and I need to access my database on another server with my PHP scripts from my webserver. Does this make sense or I am not describing this clearly? I appreciate any help that you can provide!)

Thanks!

Tara :)

Member Avatar for Member #203197

No workaround is available via PHP. That wouldn't make sense; allowing PHP to somehow override firewall settings would be somewhat dangerous. :-)

If you can't access the db server, the firewall is blocking it, so add the db server IP to the firewall allowed list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.