Try to connect with php-mysqli to external mysql database (non-cpanel)

Operating System & Version
AlmaLinux v8.5.0
cPanel & WHM Version
v100.0.5

doppie

Registered
Jan 16, 2022
1
0
1
NL
cPanel Access Level
Root Administrator
Hi,

I'am trying to connect with a php script to connect to an external mysql database.
But i think cpanel/whm is blocking somewhere external connections.
Things that i already have done:

if i run in ssh:
mysql -u sql_username -h sql_ipadres -P 7999 -p i have successfully connection to external database. (so firewall accept outgoing connection port 7999)

If i run on php file:

PHP:
$conn = new mysqli("$ip_externhost","$user","$password","$DB","$port");
I get an error

Code:
Failed to connect to MySQL: Access denied for user '$user'@'localipadres' (using password: YES)
I'am 100% that the credentials are right so that is not the problem.

So something change the external ip-adres to local-ipadres. So i think i have to enable something, but i don't know where?
Is it possible that mysqli can't connect to a external DB?

The script is running on PHP 7.2
 

cPanelAnthony

Administrator
Staff member
Oct 18, 2021
1,045
112
118
Houston, TX
cPanel Access Level
Root Administrator
Hello! In order for remote MySQL to work with cPanel, some additional work needs to be done. Any of the following articles might help. Also, you will want to ensure that port 3306 is open inbound on the server being connected to, and outbound on the server you're connecting from.



 

Travis-NS

Registered
Mar 5, 2022
2
0
1
Virginia
cPanel Access Level
DataCenter Provider
Hello, if you're trying to connect externally from a cPanel to server to another server running a MySQL, make sure the server firewall allows external connections. For example, if you're using CSF, add port 3309 to TCP_OUT. If you don't have access to CSF, ask your host or whoever manages your server to check and make that change for you.