Azure MySql异常:连接字符串可能不正确。请访问门户以获得参考

时间:2019-03-07 16:39:38

标签: c# mysql .net azure

我从.net框架应用程序连接到Azure MySql服务器的MySql存在问题。

错误是

MySql.Data.MySqlClient.MySqlException
  HResult=0x80004005
  Message=Authentication to host 'myserver-mysql.mysql.database.azure.com' for user 'username@myserver-mysql' using method 'mysql_native_password' failed with message: The connection string may not be right. Please visit portal for references.
  Source=MySql.Data
  StackTrace:
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticationFailed(Exception ex)
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
   at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate(Boolean reset)
   at MySql.Data.MySqlClient.NativeDriver.Authenticate(String authMethod, Boolean reset)
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.Driver.Open()
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at EfTest.Program.Main(String[] args) in ...

Inner Exception 1:
MySqlException: The connection string may not be right. Please visit portal for references.

用于重现问题的控制台应用程序

    static void Main(string[] args)
    {
        var server = "myserver-mysql.mysql.database.azure.com";
        var database = "e10bf91fc2a8e281d06fecf752e2a592";
        var user = "username@myserver-mysql";
        var password = "mypassword";
        var port = "3306";
        var sslM = "preferred";

        var connectionString = String.Format("server={0};port={1};user id={2}; password={3}; database={4}; SslMode={5}", server, port, user, password, database, sslM);

        using (var connection = new MySqlConnection(connectionString))
        {
            connection.Open();

            connection.Ping();
        }

        Console.ReadKey();
    }

包装

<packages>
  <package id="Google.Protobuf" version="3.5.1" targetFramework="net472" />
  <package id="MySql.Data" version="8.0.15" targetFramework="net472" />
</packages>

我还尝试了与Azure门户完全相同的连接字符串 $"Server=myserver-mysq.mysql.database.azure.com; Port=3306; Database={database}; Uid=username@myserver-mysq; Pwd={password}; SslMode=Preferred;";,但没有机会。

有人可以指出我想念的是什么吗?

1 个答案:

答案 0 :(得分:0)

尝试以下连接字符串格式:

var connectionString = String.Format("Server={0};Port={1};Uid={2};Pwd={3};Database={4};SslMode={5}", server, port, user, password, database, sslM);