Home  >  Article  >  Database  >  How to Connect to a Remote MySQL Database via SSH.NET in .NET?

How to Connect to a Remote MySQL Database via SSH.NET in .NET?

Barbara Streisand
Barbara StreisandOriginal
2024-10-27 12:27:02388browse

How to Connect to a Remote MySQL Database via SSH.NET in .NET?

Connecting to MySQL from .NET via SSH.NET

Problem:

Users encounter Schwierigkeiten when attempting to retrieve data from a MySQL database situated on a remote server over SSH using a combination of the MySQL Connector/NET and SSH.NET libraries. Despite having established an SSH connection, the code fails to produce results from database queries.

Solution:

To address this issue, it is crucial to configure the forwarded port correctly. The code below offers a refined approach:

<code class="csharp">using(var client = new SshClient("ssh server id", "sshuser", "sshpassword")) // establish SSH connection to server hosting MySQL
{
    client.Connect();
    if (client.IsConnected)
    {
        var portForwarded = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306);
        client.AddForwardedPort(portForwarded);
        portForwarded.Start();
        using (MySqlConnection con = new MySqlConnection("SERVER=127.0.0.1;PORT=3306;UID=someuser;PASSWORD=somepass;DATABASE=Dbname"))
        {
            using (MySqlCommand com = new MySqlCommand("SELECT * FROM cities", con))
            {
                com.CommandType = CommandType.CommandText;
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(com);
                da.Fill(ds);
                foreach (DataRow drow in ds.Tables[0].Rows)
                {
                    Console.WriteLine("From MySql: " + drow[1].ToString());
                }
            }
        }
        client.Disconnect();
    }
    else
    {
        Console.WriteLine("Client cannot be reached...");
    }
}</code>

In this updated code:

  • The SSH connection establishment remains unchanged.
  • The forwarded port is configured to forward the local port 3306 to the remote port 3306 on localhost. This ensures that database queries are routed through the SSH tunnel.
  • Subsequently, a MySqlConnection is established to the local port 3306, allowing the application to connect to the remote MySQL database.
  • Database query execution, result processing, and data display are handled in a standard manner.

This revised code effectively connects to the database, retrieves data, and displays it, providing a reliable mechanism for accessing a remote MySQL database through SSH in .NET applications.

The above is the detailed content of How to Connect to a Remote MySQL Database via SSH.NET in .NET?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn