Connecting remotely to Azure Database for MySQL flexible server

Dario Ferreira 20 Reputation points
2024-12-05T04:26:24.94+00:00

Hello,

I need some help connecting to my Azure Database for MySQL flexible server.

I watched the video https://www.youtube.com/watch?v=MwxqYHbQxqY and read countless articles online about it, but I can't connect to the DB from a website running PHP.

In Azure portal,
I checked "Allow public access to this resource through the internet using a public IP address"
I set up the IP addresses (my local and website) in Networking > Firewall rules

////////////////////////////////
/////// local computer /////////
////////////////////////////////

I downloaded DigiCertGlobalRootCA.crt.pem and added to c:/1/ in my computer
I can connect from my computer using xampp with the following PHP code:

PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_SSL_CA => $path . 'DigiCertGlobalRootCA.crt.pem'
];

 // Establish the connection

$db = new PDO($dsn, $username, $password, $options);	    

// If connection is successful  
echo "Connection successful!";

} catch (PDOException $e) {

// If there is an error in the connection  
echo "Connection failed: " . $e->getMessage();

}

?>

////////////////////////////////
/////// website server /////////
////////////////////////////////

The problem is, I can't connect from a website using a similar PHP script.

I have the server IP in the firewall rules.

the code below does not work, I get a SQLSTATE[HY000] [2002] Connection refused

I think it is related to the path to DigiCertGlobalRootCA.crt.pem, but I'm not sure

I uploaded the .pem file to the root of the site and below the root, none work.

What am I doing wrong?

PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_SSL_CA => $path . 'DigiCertGlobalRootCA.crt.pem'
];

$pdo = new PDO($dsn, $username, $password, $options);

// If connection is successful  
echo "Connection successful!";

} catch (PDOException $e) {

// If there is an error in the connection  
echo "Connection failed: " . $e->getMessage();

}

?>

Btw, I can connect to the DB with MySQL Workbench also.

I also tried the IP range from 0.0.0.0 - 255.255.255.255, still no luck.

Thanks for your help in advance.
Dario

Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
874 questions
{count} votes

Accepted answer
  1. ShaktiSingh-MSFT 16,086 Reputation points
    2024-12-06T06:27:11.79+00:00

    Hi Dario Ferreira •,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Solution in cx verbatim:

    ***"*According to my hosting company, SQL port (3600) was not open for outgoing traffic."

    If you have any other questions or are still running into more issues, please let me know. Thank you again for your time and patience throughout this issue.

    Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.

    Thanks

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. ShaktiSingh-MSFT 16,086 Reputation points
    2024-12-06T03:29:58.1933333+00:00

    Hi Dario Ferreira •,

    We are sorry about your issue.

    Please refer to a similar Microsoft Q&A post:

    https://zcusa.951200.xyz/en-us/answers/questions/1515941/php-app-website-wont-connect-to-database-but-mysql

    See if any of the solutions provided here could help in your case.

    Since you have checked all possible reasons, please check SSL file if empty as suggested in the above post the customer.

    If not, we recommend you to raise support case for deeper investigation.

    Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.