0

当我尝试使用以下代码对 Azure MySQL 版本 8.0.15 服务器执行存储过程时,我收到错误消息“表 'mysql.proc' 不存在”。旧版本不会发生此错误。一些搜索说要使用 MySQL_upgrade,但我不认为它适用于 azure MySQL。我该如何克服这个错误?当我连接到与 azure 托管相同的本地 MySQL 版本时,一切正常。

示例代码

static async Task Main(string[] args)
    {
        var builder = new MySqlConnectionStringBuilder
        {
            Server = "myserver.mysql.database.azure.com",
            Database = "mydb",
            UserID = "myserver@myserver",
            Password = "password",
            SslMode = MySqlSslMode.Prefered,
        };

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

            using (var command = conn.CreateCommand())
            {
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "prc_myprocedure";

                var reader = await command.ExecuteReaderAsync(CommandBehavior.Default);
                while (reader.Read())
                {
                    Console.WriteLine(reader.GetValue(0));
                }
            }

            Console.WriteLine("Closing connection");
        }

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

完全错误:

Unhandled exception. MySql.Data.MySqlClient.MySqlException (0x80004005): Table 'mysql.proc' doesn't exist
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
   at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.SchemaProvider.GetProcedures(String[] restrictions)
   at MySql.Data.MySqlClient.ISSchemaProvider.GetProcedures(String[] restrictions)
   at MySql.Data.MySqlClient.ISSchemaProvider.GetSchemaInternal(String collection, String[] restrictions)
   at MySql.Data.MySqlClient.SchemaProvider.GetSchema(String collection, String[] restrictions)
   at MySql.Data.MySqlClient.MySqlConnection.GetSchemaCollection(String collectionName, String[] restrictionValues)
   at MySql.Data.MySqlClient.ProcedureCache.GetProcData(MySqlConnection connection, String spName)
   at MySql.Data.MySqlClient.ProcedureCache.AddNew(MySqlConnection connection, String spName)
   at MySql.Data.MySqlClient.ProcedureCache.GetProcedure(MySqlConnection conn, String spName, String cacheKey)
   at MySql.Data.MySqlClient.StoredProcedure.GetParameters(String procName)
   at MySql.Data.MySqlClient.StoredProcedure.CheckParameters(String spName)
   at MySql.Data.MySqlClient.StoredProcedure.Resolve(Boolean preparing)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at AzureMySqlExample.MySqlUpdate.Main(String[] args) in C:\Users\sammo\source\repos\ConsoleApp1\Program.cs:line 31
   at AzureMySqlExample.MySqlUpdate.<Main>(String[] args)
4

2 回答 2

0

ISSUE:您收到以下错误,因为当您开始连接到 Azure MySQL 时,Azure MySQL 的网关将首先发送回一个带有默认服务器版本“5.6.42.0 MySQL Community Server (GPL)”的问候包。这使得 MySQL.Data 驱动程序将服务器版本视为 5.6。但实际上它是 8.0,在 MySQL 8.0 中,表 mysql.proc 不再存在。

解决方法:您可以使用 ProxySQL 修复版本问题:

  1. 为 Azure MySQL 设置 ProxySQL https://techcommunity.microsoft.com/t5/Azure-Database-for-MySQL/Load-balance-read-replicas-using-ProxySQL-in-Azure-Database-for/ba-p/ 880042

  2. 更新 ProxySQL 中的服务器版本 使用管理员帐户连接到 ProxySQL,运行以下命令。

一个。mysql –u 管理员 –padmin -h 127.0.0.1 -P 6032

湾。UPDATE global_variables SET variable_value='' WHERE variable_name='mysql-server_version';

C。将mysql变量加载到运行时;

d。将mysql变量保存到磁盘;

于 2020-05-01T01:41:29.027 回答
0

为了补充Mike Ubezzi的回答(感谢这篇文章为我指明了正确的方向),解决方案是简单地将数据库连接字符串中的端口从 3306 更改为 3309。更多信息在这里

就我而言,我正在运行一个链接到 MySQL 数据库版本 8.0 的 ASP.NET 应用程序。在网络应用程序的配置设置中编辑连接字符串并重新启动网络应用程序后,我的存储过程按预期执行。

于 2021-09-17T00:45:13.567 回答