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

时间:2018-02-10 23:55:43

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

  

MySql.Data.MySqlClient.MySqlException:'对主机进行身份验证' 127.0.0.1'对于用户' root'使用方法' mysql_native_password'消息失败:未知数据库' firefly'

这是错误图片:

error screenshot

连接到我的数字海洋服务器(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'

1 个答案:

答案 0 :(得分:2)

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

System.Net.Sockets.SocketException:&#39;尝试以其访问权限禁止的方式访问套接字&#39;

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

我关闭了我的本地MySQL服务器并尝试运行我的代码,从而与我的数字海洋在线数据库建立了一个有效的远程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=****"))
                    {