从 C# 到 MySQL 实例的 SSH 和 sudo 连接

SSH and sudo connection to MySQL instance from C#

我正在尝试编写一个程序,使我能够与 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 这样的命令时,程序似乎进入了一个循环 - 我猜是因为它被提示输入密码但我不知道如何在提示时提供密码。

我将在下面添加连接代码。我真的只需要能够 运行 select 语句与连接检索数据从 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);
        }
    }
}

谢谢!

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

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


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

  • Connection to MySQL from .NET using SSH.NET Library
  • SSH tunneling a MySQL connection using C#

(除非你的数据库服务器真的允许直接连接)

我只是想用适合我的解决方案更新此线程。我最终使用了上面建议的 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;
            }
        }