gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:05:34 27 4
gpt4 key购买 nike

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com