0
votes

Im trying to connect to MySQL database on Azure through C#. I am using MySQLConnector package for my program. I have admin account so I can easily access to the database through Sever Explorer in Visual Studio in SSH Sever Authentication . However I can not do the same with C#. I even tried to copy the exact connection string given by Sever Explorer but I still cant connect to my database through C# . I always get the error "1042: Unable to connect to any of the specified MySQL hosts." . The connection String from Sever Explorer provide necessary param : Data Source, Inital Catalog, Persist Security Info, User ID and Password.

As I have admin account. I have also tried to copy the connection String provided from Azure Portal for ADO.net. The connection String look a bit different . For example it provide extra parameter like TrustServerCertificate, Encrypt, MultipleActiveResultSets. However if I used this connection string, the program does not compile. Visual Studio tell me the these parameters are not supported . For example :'Option 'MultipleActiveResultSets' not supported.' etc. If I deleted those paratemters, I have the same 1042 error. Please help me, I am new. Thanks

The Connection String from Sever Explorer in Visual Studio look like this:

Data Source= ******.database.windows.net;Initial Catalog=******;Persist Security Info=True;User ID=******;Password=******

The Connection String from Azure Portal look like this:

Server=tcp:******.database.windows.net,1433;Initial Catalog=******;Persist Security Info=False;User ID=******;Password=******;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
1
Also, If anyone wonder I have checked Firewall. I have added my IP address as Client IP address in Azure Portal, so I think it should be fine. - J Nguyen
This connection string looks like a Azure SQL, not a MySQL ? If you are using Azure SQL, then use the System.Data.SqlClient, give a look: docs.microsoft.com/en-us/dotnet/api/overview/azure/… - Tony
Thank @Tony . You are right . I follow your instruction and it work - J Nguyen

1 Answers

1
votes

The Connection String from Azure Portal look like this:

Server=tcp:******.database.windows.net,1433;Initial Catalog=******;Persist Security Info=False;User ID=******;Password=******;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

This is a SQL Server connection string. You cannot use MySqlConnector with this connection string. Are you sure you're using Azure Database for MySQL and copying the connection string from the right database?