【问题标题】:SSH and sudo connection to MySQL instance from C#从 C# 到 MySQL 实例的 SSH 和 sudo 连接
【发布时间】:2020-04-09 05:50:29
【问题描述】:

我正在尝试编写一个程序,允许我与 Ubuntu 虚拟机中本地服务器上的远程数据库进行交互。以下是我可以在 windows 终端中运行以连接数据库的命令,这些命令与我想在程序中执行以与数据库通信的命令基本相同。

>ssh user@192.168.0.60
user@192.168.0.60's password: *********

>sudo mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 101
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

我可以轻松建立ssh连接,我可以执行ls之类的命令并查看当前目录的内容。但是当我尝试执行sudo mysql 之类的命令时,程序似乎进入了一个循环——我猜是因为它会提示输入密码,但我不知道在提示时如何提供密码。

我将在下面添加连接代码。我真的只需要能够通过连接运行选择语句以从 MySQL 数据库中检索数据。

using System;
using Renci.SshNet;


namespace DBconnector
{
    class Program
    {
        private static string username = "user";
        private static string password = "password";
        private static string host = "192.168.0.60";

        static void Main(string[] args)
        {
            SshClient client = new SshClient(host, 22, username, password);
            client.Connect();
            if (!client.IsConnected)
            {
                Console.WriteLine("Connection Error: Failed");
                System.Environment.Exit(1);
            }

            SshCommand dir = client.RunCommand("...some command..."); // This is where I want to run 'sudo mysql'

            // and here I need to be able to change DB and select from tables

            Console.WriteLine(dir.Result);
        }
    }
}

谢谢!

【问题讨论】:

标签: c# mysql .net database ssh.net


【解决方案1】:

对于您的字面问题,请参阅:

不过,以这种方式自动化 sudo 是一种不好的做法:


我相信您实际上会希望通过 SSH 隧道连接 MySQL:

(除非您的数据库服务器实际上允许直接连接)

【讨论】:

    【解决方案2】:

    我只是想用对我有用的解决方案来更新这个线程。我最终使用了上面建议的 mysql 连接器。

    using MySql.Data;
    using MySql.Data.MySqlClient;
    using System;
    using System.Collections.Generic;
    using System.Net.NetworkInformation;
    
    namespace DBconnector
    {
        class DBConnect
        {
            public MySqlConnection connection;
            private string server;
            private string database;
            private string uid;
            private string password;
    
            //Constructor
            public DBConnect()
            {
                Initialize();
            }
    
            private void Initialize()
            {
                Console.WriteLine("Initializing Connection...");
                server = "192.168.0.60";
                database = "devDB";
                uid = "user";
                password = "password";
                string connectionString;
                connectionString = "SERVER="+server+";" + "UID="+uid+";" + "DATABASE="+database+";" + "PASSWORD="+password+";";
    
                connection = new MySqlConnection(connectionString);
            }
    
    
            /* Sample select statement from a table in the DB */
            public List<string> SelectFBOEntries(String statement)
            {
                string query = statement;
                List<string> response = new List<string>();
                if (this.OpenConnection())
                { 
                    MySqlCommand cmd = new MySqlCommand(query, connection);
                    MySqlDataReader dataReader = cmd.ExecuteReader();
    
                    //Read the data and store them in the list
                    while (dataReader.Read())
                    {
                        response.Add(dataReader["ID"] + "");
                        response.Add(dataReader["Name"] + "");
                        response.Add(dataReader["Age"] + "");
                        response.Add(dataReader["DOB"] + "");
                        response.Add(dataReader["ZIP"] + "");
                    }
                    dataReader.Close();
                    this.CloseConnection();
                    return response;
                }
                else
                {
                    return response;
                }
            }
    
            public bool CloseConnection()
            {
                try
                {
                    connection.Close();
                    return true;
                }
                catch (MySqlException ex)
                {
                    Console.WriteLine("Error Closing Connection");
                    return false;
                }
            }
    
    
    

    【讨论】:

      猜你喜欢
      • 2016-10-30
      • 2012-06-04
      • 2017-11-24
      • 2018-04-17
      • 2020-03-13
      • 2015-08-28
      • 1970-01-01
      • 2020-01-23
      • 2014-02-18
      相关资源
      最近更新 更多