尝试从gitpod IDE(ubuntu服务器)连接到MySql数据库时出现套接字异常 [英] Socket Exceptions when trying to connect to MySql database from gitpod IDE (ubuntu server)

查看:100
本文介绍了尝试从gitpod IDE(ubuntu服务器)连接到MySql数据库时出现套接字异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gitpod在线IDE(相对于浏览器中带有ubuntu linux服务器的代码).我正在努力在另一台Linux(CentOS)服务器上创建到我的mysql数据库的连接.我已将数据库服务器上的远程MySql主机设置为'%'=>'allallowed'.

我正在使用MySql连接器软件包:

我不确定如何继续或现在尝试什么.

解决方案

在调用堆栈中,您似乎正在使用Blazor将代码编译为wasm.此代码在浏览器中运行,并且受浏览器沙箱的所有限制.更具体地说,浏览器无法打开TCP套接字进行网络通信.

更多背景信息是此处:

Blazor正在浏览器沙箱中运行,并且只能执行javascript网络明智的操作.因此从技术上讲这是不可能的.

此处:

TCP网络API在浏览器中永远无法使用,因为JavaScript不会公开原始的TCP网络功能.这自然会导致堆栈中某个地方的运行时失败.

这就是为什么您得到 PlatformNotSupportedException 的原因.

要解决此问题,您需要重新整理代码,以便从Web服务器上运行的代码建立MySQL连接,并且Blazor代码发出HTTP请求(向Web API)以调用服务器端代码,执行数据库操作.

I'm working with the gitpod online IDE ( vs code in the browser with an ubuntu linux server behind it). I'm struggling with creating a Connection to my mysql database on an other linux (CentOS) server. I've set the Remote MySql hosts on the db server to '%' => 'all allowed'.

I'm using the MySql Connector package: https://www.nuget.org/packages/MySqlConnector to connect to it. That's what my code looks like:

public async Task<List<User>> GetUsersAsync()
{
      List<User> readUsers = new List<User>();
      string connection = "server=myserverip;database=db;user=user;password=pwd";
      using (MySqlConnection con = new MySqlConnection(connection))
      {
           MySqlCommand cmd = new MySqlCommand("SELECT * FROM admin", con);
           cmd.CommandType = CommandType.Text;
           con.Open();

           MySqlDataReader rdr = cmd.ExecuteReader();
           while(rdr.Read())
           {
               User user = new User();
               user.ID = Convert.ToInt32(rdr["ID"]);
               user.Username = rdr["Username"].ToString();
               user.Password = rdr["Password"].ToString();
               readUsers.Add(user);
           }
      }
      return readUsers;
}  

Of course the connection string has other values than displayed above (because of security reasons). I'm getting following errors and I just don't know how to solve them or what exactly the problem is:

I'm not sure how to continue or what to try now.

解决方案

From the call stack, it looks like you're compiling your code to wasm using Blazor. This code is running in the browser and is subject to all the limitations of the browser sandbox. More specifically, there is no ability in a browser to open a TCP socket for network communication.

Some more background info is here:

Blazor is running in the browser sandbox and cannot do more than javascript can network wise. So technically this is not possible.

And here:

TCP networking APIs that can't ever work in the browser, since JavaScript doesn't expose raw TCP networking capabilities. This naturally leads to a runtime failure from somewhere low in the stack.

That is why you are getting a PlatformNotSupportedException.

To fix this, you would need to rearchitect your code so that the MySQL connection is made from code running on a web server, and the Blazor code makes a HTTP request (to a web API) to invoke server-side code that performs the database operation.

这篇关于尝试从gitpod IDE(ubuntu服务器)连接到MySql数据库时出现套接字异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆