2

i have a web site and in it's phpmyadmin create a database and i want to connect to it from c#.

i use MySQL connector for .NET in visual studio 2010.

my String Connection is :

 server=158.58.185.197;database=porbarco_base;uid=****;pwd=****;CharSet=utf8;

but i can't connect to database and give an error : Unable to connect to any of the specified MySQL hosts.

How i can connect to this database.

i see many threads in stackoverflow but don't solve my problem.

2 Answers2

2

The likelyhood is that the problem is less your C# and more remote access to MySQL

You need to check the following.

  1. Is MySQL listening a network address. Check your mysql configuration file. Look for an entry for bind address. if its set to 127.0.0.1 then you are just listening on localhost, change it to 0.0.0.0 and remember to restart the service

  2. Is your linux firewall blocking remote access to the mysql port.

  3. Does your mysql allow remote access for the specified user from a remote host. see This link for an example of how to set this up

Bob Vale
  • 18,094
  • 1
  • 42
  • 49
  • 1
    i don't access to `mysql` configuration files and `linux firewall`. i have an account in the site navidiranian.ir and use `direct admin` to administrate database and domain and so on. i found my problem and Explain it in below answer. – MohammadReza Vahedi Jul 04 '13 at 20:11
  • 1
    @MohammadRezaVahedi Your comment doesn't make sense. Name resolution will have nothing to do with it, you are using the ip address directly. It is not that. The likelihood is that your host provider doesn't support remote access to the MySQL database, which is not surprising as this is a big security hole. A quick look with nmap shows that port 3306 is not open on that server. – Bob Vale Jul 04 '13 at 22:06
0

If you are trying to access it directly using the IP address then the port 3306 needs to be open. Is the port open? If the application is hosted on the same server as the database then I would recommend using localhost to access it instead of the IP address.

rgrano
  • 351
  • 1
  • 6
  • port 3306 is open. my application is C# and server is linux base. i want to remotely access to database. – MohammadReza Vahedi Jul 04 '13 at 16:37
  • Try specifying the port right in the connection string as follows: Server=158.58.185.197;Port=3306; – rgrano Jul 04 '13 at 16:41
  • Is it case sensitive? Try this version: Server=158.58.185.197;Port=3306;Database=porbarco_base;Uid=myUsername;Pwd=myPassword;CharSet=utf8; – rgrano Jul 04 '13 at 16:57
  • I do this and no change in Result. – MohammadReza Vahedi Jul 04 '13 at 17:02
  • Is your issue at all related to this posting: http://stackoverflow.com/questions/5915534/accessing-a-mysql-database-from-external-host-ip-ie-mysql-workbench – rgrano Jul 04 '13 at 17:09
  • i found my problem!!! but i can't solve it. when i give my sites IP (www.porbarco.com) get me 158.58.185.197 but when i give recursively Name of IP get me ir1.navidiranian.com and navidiranian.com is my Host provider. – MohammadReza Vahedi Jul 04 '13 at 17:23
  • @MohammadRezaVahedi That is not going to be the case, all the reverse lookup tells you is that the number to name lookup records specify that `158.58.185.197` is `ir1.navidiranian.com`. This wouldn't affect you access by IP. The actual problem is that remote access to the MYSQL server is not enabled – Bob Vale Jul 04 '13 at 22:08