-2

OK I have created an azure virtual machine with a web app. I also created an azure server, and a database to go with the server. All are connected and working. I have even ran queries on the database and linked a github repository.

First I have configured everything with phpmyadmin on a local xampp server. On the xampp server everything with the website is working fine, however when I change the value of the server host, user, pass, and database I receive an sql error when attempting to connect to the database.

Bellow is the code I use to connect to the database.

Every time I run this code I receive the error Failed to connect to MySQL:

MySQL server has gone away

$db = mysqli_init();
mysqli_real_connect($db, $sname, $dbUser, $dbpass, $dbName, 1433);
if (mysqli_connect_errno($db)) {
die('Failed to connect to MySQL: '.mysqli_connect_error());
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
xander3
  • 3
  • 2
  • 2
    Possible duplicate of [MySQL error 2006: mysql server has gone away](https://stackoverflow.com/questions/7942154/mysql-error-2006-mysql-server-has-gone-away) – Wael Assaf Jul 06 '19 at 01:01
  • @WaelAssaf - I don't see that as a duplicate. That (ancient) question from 2011 is about connections eventually timing out. – David Makogon Jul 06 '19 at 01:02
  • 1
    It's unclear what you're asking here. Are you using MySql in a VM, or MySql Database service instance? Did the instance ever work (and now doesn't)? Please edit this question to be specific. – David Makogon Jul 06 '19 at 01:03
  • Ok I edited the post explaining my problem. Since I wrote all my code before interacting with azure I used mysqli_ for running queries on the website such as logging in and connecting to the database. Azure gives me a connection string using sqlsrv_ which does not return an error and connects to the database. My goal is to use mysqli_ with azure so I don't have to rewrite all my php code. Thank you for the help. Also the port in the azure connection string is 1433 so that is the port I used for mysqli_real_connect(). – xander3 Jul 06 '19 at 14:51
  • @xander3 It sounds like you were using a SQL Azure instance(MS SQL Server on Azure), not MySQL on Azure, because the port of the connection string for MySQL on Azure should be `3306`, please see [the figure](https://i.stack.imgur.com/jMjJC.png). Please inspect what type of your Database on Azure carefully. – Peter Pan Jul 09 '19 at 10:16

1 Answers1

0

According to your description and comments, I have good reason to believe that the MySQL server on Azure as you said is actually an Azure SQL database instance (MS SQL Server on Azure cloud).

If you see your connection string on Azure portal as the figure below, it's actually Azure SQL database, not Azure Database for MySQL server.

enter image description here

So as you said, you see Azure gives a connection string using sqlsrv_connect function as above.

The real connection string for Azure Database for MySQL should be as below.

enter image description here

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • Thanks for the feedback. Under connection servers I only get one php connection string that uses sqlsrv_, do you know why this is? – xander3 Jul 10 '19 at 15:10
  • @xander3 You can update your post for the screenshot of your Azure service and connection string to let us know what happended. – Peter Pan Jul 11 '19 at 01:07
  • Thanks for the help. Due to time constraints I just re wrote the source files using sqlsrv_ which works fine. – xander3 Jul 12 '19 at 14:45