通过SSH进行MySQL访问(无隧道)

时间:2017-11-09 07:47:24

标签: c# mysql ssh

Hy先, 我尝试通过SSH连接到本地网络中的MySQL数据库但是他说他想在我的计算机上打开该数据库。

  

使用方法验证用户'root'的主机'mysqldatabase'   'mysql_native_password'因消息失败:用户拒绝访问   'root'@'mymachine'(使用密码:YES)--->   MySql.Data.MySqlClient.MySqlException(0x80004005):拒绝访问   用户'root'@'mymachine'(使用密码:YES)

问题是我不知道他为什么不使用Port Forwarding。

private void SSHConnect()
        {
            PasswordConnectionInfo coninfo = new PasswordConnectionInfo(Settings.ReadSettings("sshhost"), Int32.Parse(Settings.ReadSettings("sshport")), Settings.ReadSettings("sshuid"), Settings.ReadSettings("sshpass"));
            coninfo.Timeout = TimeSpan.FromSeconds(30);
            client = new SshClient(coninfo);
            client.Connect();
            if (client.IsConnected)
            {
                Console.WriteLine("SSH is open");
            }
            else
            {
                Console.WriteLine("Error with SSH Connection");
            }
            var portFwdL = new ForwardedPortLocal(Settings.ReadSettings("ipa"), UInt32.Parse(Settings.ReadSettings("port")), Settings.ReadSettings("ipa"), UInt32.Parse(Settings.ReadSettings("port")));
            client.AddForwardedPort(portFwdL);
            portFwdL.Exception += delegate (object sender, ExceptionEventArgs e)
            {
                Console.WriteLine(e.Exception.ToString());
            };
            portFwdL.Start();
            if (portFwdL.IsStarted)
            {
                Console.WriteLine("Port Forwarding starting");
            }
            else
            {
                Console.WriteLine("Port Forwarding failed");
            }

        }
private void MySQLConnect()
        {
            string connectstring = "Server=" + Settings.ReadSettings("mysqlserver") + "; " + "Port=" + Settings.ReadSettings("port") + "; " + "Database=" + Settings.ReadSettings("mysqldatabase") + "; " + "Uid=" + Settings.ReadSettings("mysqluid") + "; " + "Pwd=" + Settings.ReadSettings("mysqlpass") + ";";
            mysqlcon = new MySqlConnection(connectstring);
            dic_result = new Dictionary<string, string>();
            dic_run = new Dictionary<int, Dictionary<string, string>>();
            try
            {
                mysqlcon.Open();
                Console.WriteLine("SQL Connection is open");
                MySqlCommand cmd = new MySqlCommand(Settings.ReadSettings("mysqlselect"), mysqlcon);
                int u = 0;
                int i = 0;
                MySqlDataReader data_Reader = cmd.ExecuteReader();
                String resul;
                String action;
                String res;
                String title;
                while (data_Reader.Read())
                {
                    resul = Convert.ToString(data_Reader["result"]);
                    action = Convert.ToString(data_Reader["action"]);
                    res = Convert.ToString(data_Reader["res"]);
                    title = Convert.ToString(data_Reader["title"]);

                }
            }
            catch (MySqlException e)
            {
                Console.WriteLine(e);
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                Console.WriteLine("SQL Connection is closed");
                mysqlcon.Close();
            }
        }

0 个答案:

没有答案