C# .NET (WPF,DIGITALOCEAN) 创建 mysql 连接时现有数据库未知

标签 c# .net ssh digital-ocean mysql-connector

MySql.Data.MySqlClient.MySqlException: 'Authentication to host '127.0.0.1' for user 'root' using method 'mysql_native_password' failed with message: Unknown database 'firefly'

这是错误图像:

error screenshot

当连接到我的 digital ocean 服务器(16.04 上的 ubuntu mysql)上托管的新创建的 mysql 数据库(已填充或未填充)时。

我收到一个异常错误,告诉我调用的数据库不存在。

如果我连接到标准已有的现有数据库,例如“mysql”、“performance_schema”和“sys”,我根本不会收到任何错误并建立成功的连接。

谁有解决办法吗?

phpmyadmin existing databases

这是代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MySql.Data.MySqlClient;
using Renci.SshNet;
using Renci.SshNet.Common;

namespace EPDproject
{
        /// <summary>
        /// Interaction logic for LoginScreen.xaml
        /// </summary>
        public partial class LoginScreen : Window
        {
            public LoginScreen()
            {
                InitializeComponent();
            }

            public void Connection()
            {
                var test = new PrivateKeyFile(@"MYSSHKEY");
                // establishing ssh connection to server where MySql is hosted
                using (var client = new SshClient("MYIP", "root", test)) 

                {
                    client.Connect();
                    if (client.IsConnected)
                    {
                        using (MySqlConnection con = new MySqlConnection("SERVER=127.0.0.1;PORT=3306;UID=MYUSERNAME;PASSWORD=MYPASSWORD;DATABASE=firefly"))
                        {
                            con.Open();

                            using (MySqlCommand com = new MySqlCommand("SELECT * FROM firefly.users", con))
                            {
                                com.CommandType = CommandType.Text;
                                DataSet ds = new DataSet();
                                MySqlDataAdapter da = new MySqlDataAdapter(com);
                                da.Fill(ds);
                                foreach (DataRow drow in ds.Tables[0].Rows)
                                {
                                    Console.WriteLine("From MySql: " + drow[1].ToString());
                                }
                                MessageBox.Show("works");
                            }
                            con.Close();
                        }
                        client.Disconnect();
                    }
                    else
                    {
                    Console.WriteLine("Client cannot be reached...");
                    }
                }
            }
            private void BtnSubmit_OnClick(object sender, RoutedEventArgs e)
            {
                Connection();
            }
        }
    }
}

错误消息详细信息:

MySql.Data.MySqlClient.MySqlException
HResult=0x80004005
Message=Authentication to host '127.0.0.1' for user 'root' using method 'mysql_native_password' failed with message: Unknown database 'firefly'
Source=MySql.Data
StackTrace:
at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticationFailed(Exception ex)
at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()
at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate(Boolean reset)
at MySql.Data.MySqlClient.NativeDriver.Authenticate(String authMethod, Boolean reset)
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at EPDproject.LoginScreen.Connection() in C:\Users\Chakir\Desktop\school\periode3\Project-Firefly-C#\Epd_firefly\EPDproject\LoginScreen.xaml.cs:line 50
at EPDproject.LoginScreen.BtnSubmit_OnClick(Object sender, RoutedEventArgs e) in C:\Users\Chakir\Desktop\school\periode3\Project-Firefly-C#\Epd_firefly\EPDproject\LoginScreen.xaml.cs:line 79
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at EPDproject.App.Main()
Inner Exception 1:
MySqlException: Unknown database 'firefly'

最佳答案

在我之前的代码中,我没有转发我的端口。
转发我的端口后发生错误。

System.Net.Sockets.SocketException:“尝试以访问权限禁止的方式访问套接字”

经过一番挖掘,我发现 MySQL 服务器在端口 3306 上本地运行,从而阻止了端口转发。

我关闭了本地 MySQL 服务器并尝试运行我的代码,从而与我的 digital ocean 在线数据库建立了有效的远程 ssh 连接。

enter image description here

client.Connect();
                if (client.IsConnected)
                {
                    var portForwarded = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306);
                    client.AddForwardedPort(portForwarded);
                    portForwarded.Start();
                    using (MySqlConnection con = new MySqlConnection("SERVER=127.0.0.1;PORT=3306;UID=****;PASSWORD=****;DATABASE=****"))
                    {

关于C# .NET (WPF,DIGITALOCEAN) 创建 mysql 连接时现有数据库未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48726859/

相关文章:

c# - StyleCop 和 FxCop 规则相互排斥

c# - .NET async\await 基础知识

c# - 为什么整数零不等于长零?

node.js - PM2 Node js 服务未访问 SSH key (git)

linux - 如何判断远程 tty 是否正在等待输入?

c# - log4net 日志记录还有哪些其他替代方案?

c# - DataSource 和 DataSourceID 都在 'GridView2' 上定义。删除一个定义

c# - 替换字符串中的 Unicode 转义序列

.net - 4.1.6 版中的 iTextSharp(具有先前的许可条件)

python - 在 python 中通过 ssh 监控控制台输出