繁体   English   中英

将 Visual Studio 连接到 SSH 隧道 MySQL 服务器

[英]Connecting Visual Studio to a SSH Tunneled MySQL Server

我正在寻找一种配置数据库连接的解决方法。
我看到打开 3306 端口是危险的,我们应该使用 SSH 隧道来连接数据库。

我使用 docker 配置了我的 MySQL 服务器,并使用 MySQL Workbench 成功连接了它在此处输入图像描述

现在我必须配置它并将其连接到 Visual Studio 2022 才能查询数据库。

Visual Studio 2022 仅受 MySQL Data through NuGet 包的支持,这些包没有 gui 连接设置。

在此处输入图像描述

我安装了 MySQL 数据库正式支持的 Visual Studio 2019,可以通过数据源进行配置。

如果配置了 SSH 隧道,我如何设置 MySQL 数据库连接到我的 Visual Studio。
添加连接 window 只显示连接的基本信息。 我不确定如何通过 SSH 隧道进行配置。

在此处输入图像描述

先感谢您。

出于安全考虑,有时数据库服务器只能通过SSH访问。比如MySql服务安装在服务器A上,机器A只能被机器B访问,部署环境可能在C机器上。在这个case, C 服务器通过B服务器连接到A服务器。 此时需要SSH连接,需要SSH.NET class库:代码如下:

using MySql.Data.MySqlClient;
using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace SSHMySql
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                SSHConnectMySql();
            }

            public void SSHConnectMySql()
            {
                string SSHHost = "*.*.*.*";        // SSH address
                int SSHPort = ;              // SSH port
                string SSHUser = "user";           // SSH username
                string SSHPassword = "pwd";           // SSH password

                string sqlIPA = "127.0.0.1";// Map addresses  In fact, it is possible to write other MySql on Linux My.cnf bind-address can be set to 0.0.0.0 or not
                string sqlHost = "192.168.1.20"; // The IP address of the machine installed by mysql can also be an intranet IP, for example: 192.168.1.20
                uint sqlport = ;        // Database port and mapping port
                string sqlConn = "Database=mysql;Data Source=" + sqlIPA + ";Port=" + sqlport + ";User Id=user;Password=pwd;CharSet=utf8";
                string sqlSELECT = "select * from user";

                PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo(SSHHost, SSHPort, SSHUser, SSHPassword);
                connectionInfo.Timeout = TimeSpan.FromSeconds();
                using (var client = new SshClient(connectionInfo))
                {
                    try
                    {
                        client.Connect();
                        if (!client.IsConnected)
                        {
                            MessageBox.Show("SSH connect failed");
                        }

                        var portFwdL = new ForwardedPortLocal(sqlIPA, sqlport, sqlHost, sqlport); // map to local port
                        client.AddForwardedPort(portFwdL);
                        portFwdL.Start();
                        if (!client.IsConnected)
                        {
                            MessageBox.Show("port forwarding failed");
                        }

                        MySqlConnection conn = new MySqlConnection(sqlConn);
                        MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
                        myDataAdapter.SelectCommand = new MySqlCommand(sqlSELECT, conn);

                        try
                        {
                            conn.Open();
                            DataSet ds = new DataSet();
                            myDataAdapter.Fill(ds);
                            dataGridView1.DataSource = ds.Tables[];
                        }
                        catch (Exception ee)
                        {
                            MessageBox.Show(ee.Message);
                        }
                        finally
                        {
                            conn.Close();
                        }

                        client.Disconnect();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
    }

注意:如果报错,可以在本地(开发机)停止MySql服务。

所需 dll:SSHDLL.rar

您可以填写以下信息来配置与MySql数据库的连接。

在此处输入图像描述

服务器名称:输入MySQL的IP地址,在你的SSL连接信息中看到就是127.0.0.1。

用户名:输入Mysql的用户名

密码:输入Mysql的密码

数据库名称:输入一个测试数据库

希望对你有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM