简体   繁体   English

System.net.Sockets异常

[英]System.net.Sockets exception

So I'm trying to connect to a MySQL database sitting on an Ubuntu VM using C#. 因此,我尝试使用C#连接到位于Ubuntu VM上的MySQL数据库。 However, I keep getting a System.Net.Sockets.SocketException exception and the connection fails every time. 但是,我不断收到System.Net.Sockets.SocketException异常,并且每次连接都会失败。 I can access the server on the VM using it's IP address (on a browser) so I'm not really understanding whats wrong. 我可以使用虚拟机的IP地址(在浏览器上)访问VM上的服务器,因此我不太了解到底出了什么问题。

Any help is much appreciated. 任何帮助深表感谢。

Did you try telnet to the mysql server from where you are trying to connect, telnet localhost 3306 localhost is your mysql server and 3306 is default port. 您是否尝试将telnet从尝试连接的地方连接到mysql服务器, telnet localhost 3306 localhost是您的mysql服务器,3306是默认端口。 If this is successful then it means that mysql is running correctly. 如果成功,则表示mysql运行正常。 Also you can verify if the credentials are passed correctly. 您也可以验证凭据是否正确传递。 Below is sample JNDI configuration for connecting to mysql from liferay portal server 下面是从Liferay门户服务器连接到mysql的示例JNDI配置

<Resource name="jdbc/LiferayPool" auth="Container" type="javax.sql.DataSource"
      maxActive="100" maxIdle="30" maxWait="10000"
     username="root" password="" driverClassName="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/liferay?useUnicode=true&amp;characterEncoding=UTF-8&amp;useFastDateParsing=false"/>

Hope this helps. 希望这可以帮助。

If your server is ok you may try the ADO.Net driver for MySQL ( http://www.mysql.com/downloads/connector/net/ ) 如果您的服务器正常,则可以尝试MySQL的ADO.Net驱动程序( http://www.mysql.com/downloads/connector/net/

After you install the driver, add a reference to MySql.Data.dll in you project. 安装驱动程序后,在项目中添加对MySql.Data.dll的引用。

In your code, you can connect to the server using: 在代码中,您可以使用以下方法连接到服务器:

public static MySqlConnection mysqlCreateConection() {            
        try {
            c = new MySqlConnection("server=" + "localhost" + ";user id=" + "user" +
                                    ";Password=" + "password" + ";persist security info=True;database=" + mysqlDatabase +  "" + ";");
            c.Open();
            Console.WriteLine(c.ToString());
            return c;
        } catch (MySqlException e) {}

With this connector you can use classes and methods already made to make your life easier when dealing with c# + mysql. 使用此连接器,您可以使用已经制作的类和方法来简化c#+ mysql的处理。

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

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