PHP cannot connect to the database?

Kiacc2

New member
Joined
Dec 25, 2024
Messages
3
It is possible to connect normally using mysql -u database -p -h 10.0.0.3 on the server. Then, I wrote a test code called test. php and tested it successfully on other servers, but failed in directadmin Connection refused,Even with MySQL public IP, it cannot run。

```
<?php
$host = '10.0.0.3';
$username = 'testuser';
$password = '123456..';
$dbname = 'test';
$port = 3306;
$conn = new mysqli($host, $username, $password, $dbname, $port);
if ($conn->connect_error) {
die("failed: " . $conn->connect_error);
}
echo "ok";
$conn->close();
?>
```
 
Please make sure IP is whitelisted, or outbound port 3306 is open in CSF firewall.
 
Please make sure IP is whitelisted, or outbound port 3306 is open in CSF firewall.
Hello, may I ask where to check the outbound in CSF firewall ,verified that the MySQL service does not have any firewall or whitelist settings,I am a newly installed directadmin and have not manually set any firewall rules
 
think you very much ,csf -x
That is no solution, because that will completely disable the firewall which is never a good thing.

You can either use the Configserver plugin in your DA to open port 3306 or otherwise login via SSH as root and edit the /etc/csf/csf.conf file and add port 3306 and restart CSF and LFD:
csf -ra

However, opening port 3306 makes your system available for attacks on that port.

Maybe a better solution is to put the ip's of the servers you want to allow to make remote connections in both csf.allow and csf.ignore so that way you don't need to open port 3306, they can bypass the fireall and port 3306 will be kept closed for others.
Kindline (maybe better) options are also possible.
 
Back
Top