title: mySQL connection string through SSH tunnel without password
body: <p>I've been given SSH access to a server running mySQL. My access is authenticated through my public key, with no further password required.</p>
  
  <p>Using Putty, I am able to SSH to the server using my private key, log directly into the mySQL database via the command-line tools and run SQL directly. I'm looking to emulate this through a C# service (forwarding port 3306 through the SSH connection), unfortunately all the connection strings listed at <a href="http://www.connectionstrings.com/mysql/" rel="nofollow">http://www.connectionstrings.com/mysql/</a> require a password.</p>
  
  <p>My rough-and-ready code so far, using <code>MySql.Data</code> and <code>SSH.NET</code>:</p>
  
  <pre><code>  PrivateKeyFile file = new PrivateKeyFile(@" [ path to private key ]");
    using (var client = new SshClient(" [ server ] ", "ubuntu", file))
    {
  
        var port = new ForwardedPortLocal(3306, "localhost", 3306);
        client.AddForwardedPort (port);
        client.Connect();
  
        var connection = new MySqlConnection(
             "server=localhost;database=[database];uid=ubuntu;"
        );
        MySqlCommand command = connection.CreateCommand();
        MySqlDataReader Reader;
        command.CommandText = "select * from sample";
        connection.Open();
        Reader = command.ExecuteReader();
        while (Reader.Read())
        {
            string thisrow = "";
            for (int i = 0; i &lt; Reader.FieldCount; i++)
                thisrow += Reader.GetValue(i).ToString() + ",";
            Console.Write(thisrow);
        }
        connection.Close();
        client.Disconnect();
   }
  </code></pre>
  
  <p>Running the code without the <code>SshClient</code> section, talking to a database on the local network (with a known username and password), is working perfectly. With the <code>SshClient</code> section in, and with a breakpoint once the tunnel has been set up, I can telnet to <code>localhost:3306</code> and get a response back from mySQL.</p>
  
  <p>However, the following error is thrown on the <code>connection.Open()</code> line:</p>
  
  <blockquote>
    <p>Authentication to host 'localhost' for user 'ubuntu' using method 'mysql_native_password' failed with message: Access denied for user 'ubuntu'@'localhost' (using password: YES)</p>
  </blockquote>
  
  <p>So ... what connection string do I need? Or have I missed something subtle elsewhere?</p>