简体   繁体   中英

c# connecting to mysql on different server

I have ac# web service running on a different webspace to my MySQL database. i want to link them together so have the following c# code.

            server = "www.***.com";
        database = "MyDataBase";
        uid = "admin";
        password = "Password";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

        connection = new MySqlConnection(connectionString);
        try
        {
            connection.Open();
        }
        catch (MySql.Data.MySqlClient.MySqlException ex)
        {
            Console.WriteLine(ex.Message);
        }

on the catch exception im getting the following error

"Access denied for user 'admin'@'cpc13-ches4-2-0-cust195.9-1.cable.virginmedia.com' (using password: YES)"

im only running it locally at the moment so i dont know if that is helping or hindering.

Check your MySQL user settings to make sure the admin account is allowed to log in from machines other than localhost .

You can verify which hosts users are allowed to connect from like this:

select user, host from mysql.user

if the admin user can only connect from localhost, then you'll either want to set up permissions for the new host you are connecting from or for all hosts using % .

You can see what permissions admin has on localhost like this:

show grants for 'admin'@'localhost'

Then you can copy/paste those grants changing the host part to grant the same permissions on another host.

You have to grant access for admin @ cpc.......

Login to mysql and do the following:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%'
->     WITH GRANT OPTION;

Or first check with this command to see the priv first.

SHOW GRANTS FOR 'admin'@'localhost';

And try again...

Make sure that your MySql server allows remote connections. And maybe your connection string is just not the exact one. Check connection strings here: http://connectionstrings.com/mysql

You'll want to go to:

Security > Logins > Click your account > Properties > User Mappings

And ensure you have the correct permissions

  • Download and install "MySQL ODBC 3.51 Driver" at http://dev.mysql.com/downloads/connector/odbc/3.51.html .
  • Using IP addresses instead of www. * .com
  • Require open port 3306 on server

      string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=112.***.**.***;" + "DATABASE=dbname;" + "UID=u_name;" + "PASSWORD=u_pass;port=3306;" + "OPTION=3"; OdbcConnection MyConnection = new OdbcConnection(MyConString); MyConnection.Open(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM