2
votes

I am trying to connect to a MySQL database on my server. Here is my small sample that I am using to connect:


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();

I get the following error when I try to execute this code:

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 . . .

Any ideas why this might be? How can I give permissions to root so I can access it?

3
Did you allow remote access using the root account on the MYSQL instance?Thomas Li
In the my.cnf file, I changed the #bind-address = 123.34.567.89 which is the ip of my server. Is there more I need to do than that?user489041

3 Answers

7
votes

Create user to access from any host (or only from host from which you will be connecting):

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

Grant permissions on the db:

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

Check mysql configuration, by default the root user is allowed to connect only from localhost.

http://dev.mysql.com/doc/refman/5.1/en/connection-access.html

0
votes

Did you edit the config file to allow remote access?

If not, that's likely your problem. I don't know the rules about posting links here, but searching google for remote mysql access will give some good advice.