【问题集】Unity 3D 连接 MySQL 报 The given key ‘utf8mb4‘ was not present in the dictionary. 错误!!!

在使用 Unity 3D(version = 2021.3.13f1c1 LTS) 连接 MYSQL 数据库时报了如下两个错误

问题一:Unable to connect to any of the specified MySQL hosts.

 问题二:The given key 'utf8mb4' was not present in the dictionary.

数据库连接接口,仅供参考

public void connectMYSQL(string ipAddr,int port,string dbName,string userID,string password)
{
    string connectionString = string.Format("Server={0};Port={1};Database={2};UserID={3};Password={4};charset=utf8",ipAddr,port, dbName, userID, password);
    try{
        dbConnection = new MySqlConnection(connectionString);
        dbConnection.Open();
        Debug.Log("数据库连接成功");
    }
    catch(Exception e)
    {
        throw new Exception("服务器连接失败:"+e.Message.ToString());
    }
}

问题一解决方法:C# 连接数据库时报该错误原因是数据库的服务器IP地址填写有错误,所以传给 ipAddr 参数的数据库服务器IP地址不要传 localhost,而是传 真实IP地址 或者 127.0.0.1

问题二解决方法1:数据库与 Unity 3D 之间的连接需要的驱动包 mysql.data.dll 有问题,与MySQL的版本不匹配,点击链接 Mysql.Data.dll 重新下载驱动包,下载第三个然后解压进行替换即可。

问题二解决方法2:在连接字符串后面加 "charset=utf8" 定义字符串的编码格式,如上面接口里的的 connectionString 变量所示。

猜你喜欢

转载自blog.csdn.net/weixin_43729127/article/details/128319784