Solution 1 :

Can you explain me somethings?

What linux distribution/version?

Do you have two or three servers? (MySQL server, Tests server and Production server?)

Have you check the ufw/ip-tables or another “firewall” software that may you have installed?

Have you check the connection from terminal? (something like nc -zv or anything else)

I’m new on this… I just try to help…

Excuse me for my english

Solution 2 :

Thanks so much for all the assistance. It seems that the host I was using for my database for almost 4 years has decided to lock down the 3306 access to workbench and remote access. No notification of this.

They have decided that the attack rate on the MySQL is too high. Now only local host and same host websites work there. This is strange though, because all my other projects and workbench still connect. I have argued this with them, but they insist.

I will have to move the database elsewhere.

I did a test this morning on a different host. All worked perfectly.

Thanks again for all the assistance.

Problem :

My DataBase host and my website host are not the same.

I have created a few web apps on .NET with a web.config or app.config file where I could easily set a machinekey for the mysql connection on a mysql database for the web-farm.

This was very simple on .NET or Visual studio web app.

   <system.web>
 <machineKey validationKey="C250127840E44F50A34824E348FC089A0DE6C60C3D0CF61A31AB01BCAB6AE3940C82648D2F085B1DECFA5204AD393A5810403DABED73002608AEF7231F29E6CB" decryptionKey="8A86EB63181EDD9439D664ACA990D07302F3280E6F6919B3" validation="SHA1" decryption="AES"/>
</system.web>

However

I am now creating a website for a Linux host. I am using the same Database and again, on testing, the connection is fine. However, when published, the mysql connection is refused.

I have a suspision that it is again because of the web-farm, but there is no evidence of it.

I have allowed the host IP on remote-mysql.

I have checked that the Host IP is not blocked on the Mysql host.

I have checked the ports for the mysql connection.(it works before publish).

I have checked the user privileges on the mysql username.

I have also mailed the support guys on my mysql host to see if there is anything on their side.

I have been sitting with this for a while, and its frustrating.

Is there a way i can set the Machine key on this app/is it needed?

If anyone suggest anything else I could check, or point me to a setting I may be missing, that would be MUCH appreciated.

Below is my connection, which DOES work when I run the application.

Publish to host, Connection refused.

<?php

    $host = "server IP";
    $port = 3306;
    $user = "username";
    $password = "**********";
    $dbname = "mysql.table";

    $conn = new mysqli($host, $user, $password, $dbname, $port);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

?>



<?php

 $host = 'DB_IP';
    $dbname = 'DB_Name';
    $dsn = 'mysql:host=DB_IP;dbname=DB_Name';
    $username = 'Username';
    $password = 'Password';


     try {
         $conn = new PDO($dsn, $username, $password);
         echo "Connected to $dbname at $host successfully.";
     } catch (PDOException $pe) {
         die("Could not connect to the database $dbname :" . $pe->getMessage());
     }    

  ?>

This is when i test the code ::

Connected to mydb at IPADDRESS successfully.

This is after publish to the host ::

Could not connect to the database mydb :SQLSTATE[HY000] [2002] Connection refused

I am getting the same result with PDO and MySqli

If anyone can please assist. I have been looking for answers for days now.

Comments

Comment posted by Nico Haase

If the given code works sometimes, and sometimes it doesn’t, what have you tried to debug the problem? Is there any error message given?

Comment posted by Gareth Swemmer

Hi there. Thanks so much Nico. I have done that, I have also checked error logs. I get one error code — (HY000/2002): Connection refused. However, its not very specific. — The code does connect. When I compile… it returns the correct data from the remote mysql. The moment i publish it. No longer connects.

Comment posted by Nico Haase

And what

Comment posted by Gareth Swemmer

When I F5 the VS Code, everything works fine. It returns the data, displays it on the browser, everything is great. When I place the files inside the public_html, then the connections does’t work. If you read the above, you will see that I have set the permissions on the database already. I have checked the ports, and the IP allow, etc. I have also tried different connection methods, besides mysqli.

Comment posted by Nico Haase

And “(HY000/2002): Connection refused” is the whole error message printed after initializing the MySQLi class? Have you tried logging in on your production server and connect to the MySQL server through a shell client like

Comment posted by Gareth Swemmer

Hi there. much appreciated. I have this database connected on various other applications and web apps in .NET and mysql workbench. and a desktop application and a phone application. I’m using VS Code with WAMP… I have allowed the host IP on remote-mysql. I have checked that the Host IP is not blocked on the Mysql host. I have checked the ports for the mysql connection.(it works before publish). I have checked the user privileges on the mysql username. I have also mailed the support guys on my mysql host to see if there is anything on their side.

Comment posted by dev.mysql.com/doc/refman/8.0/en/can-not-connect-to-server.html

I don’t think that the trouble is on MySQL server; I usually use roughted solutions, but you have made that things (from production server to mysql server ping, traceroute, netcat… etc). Have you check this?

Comment posted by Gareth Swemmer

I’m gonna check it out right now. Thanks very much.

Comment posted by Gareth Swemmer

I doubt its MySQL. Is there maybe something i need to add as a reference to the site for the RemoteMysql host, or on the .htaccess? I have allowed everything on the website host and the remotemysql.

Comment posted by Capitex

I really wanna help you, but I really need to know if your website host can connect to your DataBase host… I’m not talking about the mysql connection; I suspicious that the trouble is there.

By