Not sure this is the place to ask this, but I am trying to connect to a server which I have full access to. I can login to the phpMyAdmin from the web, but when I try to connect to it from C#, it doesn't work. It shows the following error in particular:
Unable to connect to any of the specified mysql hosts
Here's the connection string I'm using:
string mycon = "server=someIP;port=3306;uid=user;pwd=pwd;database=mydb;Convert Zero Datetime=True";
I think this is because the server requires SSH. Is there any way I can disable SSH requirement for the connection?
If port 3306 is closed, you won't be able to connect from a remote host. PHPmyadmin works because it is running on the same machine as the mysql server (I assume) so it is not connecting from a remote host. You need to either open port 3306 and configure mysql to listen to port 3306, or you mention that you have ssh, which opens up another option.
If you aren't able to open 3306 to the general public, you could create an ssh tunnel to the server that forwards 3306 on the mysql server to some port on your local machine (like 3307), then configure C# to connect to that local port.
string mycon = "server=localhost;port=3307;uid=user;pwd=pwd;database=mydb;Convert Zero Datetime=True";
Here's an example of how to forward a port with putty.
http://www.cs.uu.nl/technical/services/ssh/putty/puttyfw.html
Also, the easiest way to check for open ports on an ip is a port scanner like nmap. I'm assuming you're on windows since you're using C#.
There is windows gui client here: http://nmap.org/zenmap/
I finally solved the problem by changing the mysql configuration in my.cnf
It was previously:
bind-address = 127.0.0.1
and I changed it to
bind-address = 0.0.0.0
Restart mysql service, and everything's fine :)
Related
I have a database hosted with XAMPP on a PC with IP: 10.35.250.195
I want to access this database from a laptop in the same LAN where my C#-Application is running on.
This is my connection-string:
"SERVER=10.35.250.195;DATABASE=testdb;USERID=root;PASSWORD=;"
But I'm being unable to open a connection from a remote pc/laptop.
I can access the database with the above connection-string from the PC where the database is hosted. I can also access the phpmyadmin in the browser from the remote laptop with "10.35.250.195/phpmyadmin". So the connection works from the laptop to the PC with the database, but I am not able to open this remote connection in C#.
Within phpmyadmin I allowed user "root" to access host 10.35.250.195 with all rights and I even allowed that every user can access host 10.35.250.195 with all rights to get sure (I know I should not do this because of security reasons...).
How can I access the database in c# with mysql from a remote PC via Lan? Can somebody help me to find the fault?
Maybe your port of the DB is blocked by a firewall?
MySql has Port: 3306
and you could also try a PC based tool to test it like
https://www.heidisql.com/
I have an executable in C# that needs to remotely connect to a Linux server in order to connect to an Amazon RDS database.
The executable lives on a Windows Server. Only the Linux server is able to connect to the RDS database due to permissions. Since the C# executable needs information from the database, I have found myself needing to first connect to the Linux server, and then from there connect to the database.
// include files
using MySql.Data;
using MySql.Data.MySqlClient;
using Renci.SshNet;
//...
using (var client = new SshClient("server.amazonaws.com", "serverUser", privateKeyFile))
{
client.Connect();
MySqlConnection databaseConnection = null;
string connString = "Server=dbServer.rds.amazonaws.com; database=database " +
"UID=databaseUser password=databasePassword";
databaseConnection = new MySqlConnection(connString);
databaseConnection.Open();
string queryString = "SELECT * FROM tbl_table;"; // errors here
// ..... }
I get an error when assigning the querystring that says "MySql.Data.MySqlClient.MySqlException: 'Unable to connect any of the specified MySQL hosts.'"
I'm using SSH.Net to form the connection to the Linux server. I'm using MySQL Connector to connect and retrieve information from the database.
If there's something wrong with my code, I'd greatly appreciate it being pointed out, however I'm fairly certain I'm missing some necessary step to properly connect to the database. I noticed other questions asking something similar, but they either haven't been using an RDS DB or didn't have to SSH in through C#.
EDIT: Since posting this, the server I am executing this code on has been given access to the RDS database. I currently do not have time to attempt the solutions posted. What I can say is that after being given access, things are working smoothly only using MySql Connector.
Skip the SSH. On your Linux box, configure IPtables to port-forward to AWS. Pick any arbitrary port number, say 12345.
Any incoming TCP connections to port 12345 will forward traffic to a given host name and port on AWS.
Subsequently, your MySQL connection string will connect to port 12345 on your linux box, but the forward will send the traffic to AWS.
Here are some sample articles:
Port Redirection Example
How can I port forward with IPTables?
How To Forward Ports through a Linux Gateway with Iptables
As IPtables can be a pain in the rear to configure manually, consider installing Webmin first as an easy-to-use web interface to configure iptables with.
You're almost there. I'm not familiar with SSH.NET, but when using Putty (an SSH terminal), you would configure SSH "port tunneling" or "port forwarding" which will setup a listening port on your local computer. Any connections to your local port are forwarded along the SSH connection to the remote computer, then forwarded on to the final destination.
https://github.com/sshnet/SSH.NET does mention that it supports "port forwarding". Take a look at using that.
Connect to your Linux box using SSH.NET
Configure a port forward from localhost:12345 to dbServer.rds.amazonaws.com:3306
Use localhost:12345 in your connection string as the target server rather than dbServer.rds.amazonaws.com:3306 for MySqlConnection.
Connecting through SSH is not enoguh. What you want to do can't be done. The problem with your code is that the AWS service allows connections to the database only from the Linux machine. You are trying to initiate a connection from the Windows machine. The only way to do this is to create some kind of API between the Linux machine and the Windows machine or else you need to log in to your AWS account and grant database access to your Windows machine
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.
The likelyhood is that the problem is less your C# and more remote access to MySQL
You need to check the following.
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
Is your linux firewall blocking remote access to the mysql port.
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
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.
I have done a small C# Win Forms application, that connects to mySQL database using mySQLconnector. This works perfectly ok with my localhost.
String ConnectionString = "server=xx.xxx.xx.xxx;uid=myuserid;pwd=mypassword;database=dbname";
But I do get following error when I try to connect to remote server:
Unable to connect to any of the specified MySQL hosts.
{"No connection could be made because the target machine actively refused it xx.xxx.xx.xxx:3306"}
I have pinged the server from command prompt and I get response.
I also verified mySQL server settings in remote server database (correct name/ip, portno:3306, skip networking:OFF)
The remote server is accessible without any problem from web server that runs php script and uses same settings as the connection sting I listed above.
The mySQL server is hosted on external shared hosting company 123reg.co.uk with me logging to phpmyadmin using same uid/passwd as like in connection string.
I can't understand what is different between webserver accessing OK, but my C# app having trouble with it??
Can any one please help...
Thanks in Advance
Sam
I've had issues with servers setup so they won't accept remote connections. This is for security reasons mainly. Some places will let you enable it in the CP, but if you have direct access to the server, you should be able to follow these steps.
http://www.rackspace.com/knowledge_center/article/mysql-connect-to-your-database-remotely
Your ip address will be different from the web server.
If your ip address is static you can grant access to your ip address within your mysql database ( https://www.google.com.au/search?q=mysql+grant+access+to+user+from+host ).
I'd recommend using the server to access the database and make your c# application talk to your webserver using ajax/json calls ( https://www.google.com.au/search?q=C%23+Win+Forms+ajax+json ) - that way you're not tied to an ip address for access.
Alternatively you can look at setting up ssh tunnels to your webserver if remote shell access is available so you can get the same access as your webserver. ( https://www.google.com.au/search?q=ssh+tunnels+example+mysql That's what I do myself)
I'm having a bit of trouble connecting to my SQL database through my C# code. I can connect to it fine using Putty and the server I am connecting to is based in Germany, I am here in the UK.
When I use Putty I use these credentials:
Host Name:
sample-intra.net
Port:
2223
And then to login using the window that opens I type
login as: my_username
password: my_password
then once access has been granted I type:
mysql -h localhost -u root -p
and then when asked for the password I enter
my_password
once again.
That works fine and I can access my database (called SampleDB) fine, and access all the tables, but I am having trouble turning this in to a C# connection string I can use with the SqlConnection object. If someone can provide me with the connection string based on the above credentials I would appreciate it greatly.
Many thanks
Because you're first connecting with PuTTY to SSH into your server, and then running mysql commands while connected, it's as if you're physically sitting at the console of the server. In other words, you connected to your mysql instance using the address "localhost" because you are indeed already on that machine.
There's two things you could do here to work around your problem:
Make an SSH tunnel in PuTTY, and connect to your MySQL
instance through it.
Enable remote access in your MySQL server
for connections from your desktop machine's IP address.
Option one is more secure.