简体   繁体   English

无法从.NET连接器连接到Azure MySQL数据库

[英]Cannot connect to Azure MySQL database from .NET Connector

I tried to connect to the Azure MySQL database using MySQL Workbench and MySQL Shell and it works fine. 我尝试使用MySQL Workbench和MySQL Shell连接到Azure MySQL数据库,并且工作正常。 Now I am trying to connect using following C# code: 现在,我尝试使用以下C#代码进行连接:

var connStringBuilder = new MySqlConnectionStringBuilder
{
    Server = "creatur-db.mysql.database.azure.com",
    Database = "test",
    UserID = "creatur_db_main@creatur-db",
    Password = "{my_password}",
    SslMode = MySqlSslMode.Preferred,
};

using (MySqlConnection connection = new MySqlConnection(connStringBuilder.ToString()))
{
    connection.Open();

    connection.Close();
}

Here I replaced {my_password} with password to the database and it gives me an exception inside Open method: 在这里,我用数据库的密码替换了{my_password} ,这给我打开方法里面的异常:

MySql.Data.MySqlClient.MySqlException: 'Authentication to host 'creatur-db.mysql.database.azure.com' for user 'creatur_db_main@creatur-db' using method 'mysql_native_password' failed with message: The connection string may not be right. MySql.Data.MySqlClient.MySqlException:使用方法“ mysql_native_password”对用户“ creatur_db_main @ creatur-db”的主机“ creatur-db.mysql.database.azure.com”进行身份验证失败,并显示以下消息:连接字符串可能不正确。 Please visit portal for references. 请访问门户网站以获取参考。

I also tried different connection strings: 我还尝试了不同的连接字符串:

Server=creatur-db.mysql.database.azure.com; Port=3306; Database=test; Uid=creatur_db_main@creatur-db; Pwd={my_password}; SslMode=Preferred

and

Database=test; Data Source=creatur-db.mysql.database.azure.com; User Id=creatur_db_main@creatur-db; Password={my_password}

But none of them worked. 但是他们都不起作用。

The same exception occurs when I create new connection using Server Explorer in Visual Studio 2013. It seems the error has something to do with .NET Connector. 当我在Visual Studio 2013中使用服务器资源管理器创建新连接时,会发生相同的异常。似乎该错误与.NET连接器有关。 I tried to use different versions of MySQL.Data.dll, .NET Framework and Visual Studio but no luck. 我尝试使用不同版本的MySQL.Data.dll,.NET Framework和Visual Studio,但没有运气。

I just created a console application using VS2017 I used Nuget package MySql.Data and .Net Framework 4.6.1 我刚刚使用VS2017创建了一个控制台应用程序,我使用了Nuget包MySql.Data和.Net Framework 4.6.1

It works perfectly here What I did. 在这里,我所做的一切都很完美。

After Creating the MySQL server I used the CloudShell to connect to the SERVER (not a database). 创建MySQL服务器之后,我使用CloudShell连接到SERVER(不是数据库)。

在此处输入图片说明

Using this code: 使用此代码:

mysql --host franktest.mysql.database.azure.com --user frank@franktest -p

I got an error 我有一个错误

ERROR 9000 (HY000): Client with IP address '40.76.202.47' is not allowed to connect to this MySQL server.

So I add that IP and at the same time my IP from where I'm connected. 因此,我添加了该IP,同时添加了我所连接的IP。

So once the IP were saved. 因此,一旦IP被保存。 I executed the previous command, and this time it worked perfectly. 我执行了前面的命令,这次它运行良好。 I created a database named: frankdemo using this command: 我使用以下命令创建了一个名为:frankdemo的数据库:

CREATE DATABASE frankdemo;

Then, back in VisualStudio used this code as my Main method, copied from the documentation. 然后,回到VisualStudio,将此代码用作我的Main方法,从文档中复制。

static void Main(string[] args)
    {
        var builder = new MySqlConnectionStringBuilder
        {
            Server = "franktest.mysql.database.azure.com",
            Database = "frankdemo",
            UserID = "frank@franktest",
            Password = "gr3enRay14!",
            SslMode = MySqlSslMode.Required,
        };

        using (var conn = new MySqlConnection(builder.ConnectionString))
        {
            Console.WriteLine("Opening connection");
            conn.Open();

            using (var command = conn.CreateCommand())
            {
                command.CommandText = "DROP TABLE IF EXISTS inventory;";
                command.ExecuteNonQuery();
                Console.WriteLine("Finished dropping table (if existed)");

                command.CommandText = "CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);";
                command.ExecuteNonQuery();
                Console.WriteLine("Finished creating table");

                command.CommandText = @"INSERT INTO inventory (name, quantity) VALUES (@name1, @quantity1),
                    (@name2, @quantity2), (@name3, @quantity3);";
                command.Parameters.AddWithValue("@name1", "banana");
                command.Parameters.AddWithValue("@quantity1", 150);
                command.Parameters.AddWithValue("@name2", "orange");
                command.Parameters.AddWithValue("@quantity2", 154);
                command.Parameters.AddWithValue("@name3", "apple");
                command.Parameters.AddWithValue("@quantity3", 100);

                int rowCount = command.ExecuteNonQuery();
                Console.WriteLine(String.Format("Number of rows inserted={0}", rowCount));
            }

            // connection will be closed by the 'using' block
            Console.WriteLine("Closing connection");
        }

        Console.WriteLine("Press RETURN to exit");
        Console.ReadLine();
    }

Runed it and it works 运行它,它的工作原理

在此处输入图片说明

The documentation I'm referring to is: 我要参考的文档是:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM