c# - 无法连接到远程 MySQL 数据库

标签 c# mysql database

我正在尝试连接到我服务器上的 MySQL 数据库。这是我用来连接的小样本:


using System;
using System.Data;
using MySql.Data.MySqlClient;

namespace MySqlTest
{
    class MainClass
    {
        public static void Main (string[] args)
        {
        string connectionString =
          "Server=12.34.546.213;" +
          "Database=Test;" +
          "UID=root;" +
          "Password=password;";
       IDbConnection dbcon;
       dbcon = new MySqlConnection(connectionString);
       dbcon.Open();

当我尝试执行这段代码时出现以下错误:


Unhandled Exception: MySql.Data.MySqlClient.MySqlException: Access denied for us
er 'root'@'12.34.546.213' (using password: YES)
   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.AuthenticateNew()
   at MySql.Data.MySqlClient.NativeDriver.Authenticate()
   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 MySqlTest.MainClass.Main(String[] args) in c:\Users\dvargo\Documents\Proje
cts\MySqlTest\MySqlTest\Main.cs:line 18
Press any key to continue . . .

知道为什么会这样吗?如何授予 root 权限以便我可以访问它?

最佳答案

创建用户以从任何主机(或仅从您要连接的主机)访问:

CREATE USER 'username'@'%' IDENTIFIED BY  'password';

授予对数据库的权限:

GRANT ALL PRIVILEGES ON  `sometable` . * TO  'username'@'%' WITH GRANT OPTION ;

关于c# - 无法连接到远程 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7667187/

相关文章:

c# - 委托(delegate)参数中的 Co(ntra) 方差

java - 光 : Failed to validate connection because connection is closed

mysql - 如果主从具有不同的数据库以防Mysql复制,如何重新同步Mysql DB?

mysql - MySQL 中 JavaScript 的 encodeURI 组件的等效项是什么?

python - 操作错误:(2003, "Can' t 连接到 'mysql2.000webhost.com' (10060) 上的 MySQL 服务器”)

php - Opencart 问题配置步骤 3 - "Error: Could not connect to the database please make..."

c# - Unity - 奇怪的错误,构建与构建和运行

c# - 错误CS0027 : Keyword 'this' is not available in the current context

c# - 对列表进行排序并根据顺序选择对象

php mysql gallery 数组排序