Search code examples
c#mysqlvisual-studio-2010connection-stringmysql-connector

C# with MySQL connector - access denied


I have a remote hosted website with a MySQL database.

I am trying to access that database from a small C# program.

What I've tried:

  • Referencing the MySQL DLL in the project
  • Using MySQL connection strings
  • Disabling my firewall
  • Adding my IP address to the Remote MySQL Databases in the site's cPanel

What I'm getting:

  • Access denied for user blabla@mycomputerip in the Visual Studio console

What boggles my mind:

  • How can I find out if my webhosting service allows remote connections to the DB?
  • Do I really need to connect with PuTTy to the server and tweak the configs?
  • ^ if so, do I even have access to do that?

LATER EDIT:

  • Interesting enough. If I do

    GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' WITH GRANT OPTION;
    

    it throws me this:

    #1045 - Access denied for user 'blabla'@'localhost' (using password: YES)
    

    Common sense should tell you that my username is not actually 'blabla'.

LATER LATER EDIT:

I don't have privileges to CREATE USER, nor do I have SSH access via PuTTy or so.
Does this basically mean that it's my hosting's fault that I can't connect from a remote location to the DB?


Solution

  • Have a look at the users in your mysql database. A couple things to check:

    1. With cPanel on shared hosting, if you create a user named blah the actual name of the user in mysql is often unixusername_blah. This is done automatically to prevent MySQL user name conflicts between different cPanel accounts sharing the same MySQL server.

    2. With MySQL you can define which hosts a user is allowed to connect from. Is the new user you defined allowed to connect from your IP?

    ETA:

    Might want to have a look at the permissions for that user account and make sure appropriate permissions are granted. You can do this by running the following in phpMyAdmin or at the console:

    SHOW GRANTS FOR 'something_root'@'localhost'
    

    And compare the grants you see there to what you get here:

    SHOW GRANTS FOR 'something_root'@'%'
    

    Finally, depending on your hosting environment you may not have permissions to create new users or expand their privileges enough to be able to do this. This would be particularly likely if you have a shared hosting account.