My MySQL connect code below is 'Catching' this error: "Unable to connect to any of the specified MySQL hosts."
My DB is on my linux website and I have remote access enabled using % so anyone can access the DB.
My code is .NET C# and I have a winform.
string server = "http://www.mywebite.net";
string database = "mywebsite_app";
string uid = "admin";
string password = "Password";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
MySqlConnection dbConn;
try
{
dbConn = new MySqlConnection(connectionString);
dbConn.Open();
if (dbConn.State.ToString() != "Open")
{
this.Text = "could not open database connection";
}
else
{
this.Text = "database connection opened";
}
}
catch (Exception ex) // catch on general exceptions, not specific
{
this.Text = ex.Message;
}
Thanks for any help...
If you mysql database is on remote hosting, then you usually out of luck try to connect to that database remotely.
The hosting provider usually blocked remote mysql access.
Try using ssh-tunneling.
download putty
http://www.chiark.greenend.org.uk/~sgtatham/putty/
open the command line, and execute putty from the installation directory
putty username#remote_mysqlhost -L 3306:localhost:3306
or you could use the gui, fill the address with your mysql remote, but dont open connection yet.
go to ssh tunnel, add new forwarded port 3306, and the destination as localhost:3306.
as of why the destination is localhost, it is because the destination is what the ssh session see. because you already connected to your remote server in your ssh session, thus, the mysql host is local.
this could also be a set up for dmz.
you can create a dmz gateway using putty to tunnel trough any computer within the dmz network. but the computer behind the gateway, wont publicly expose to the internet.
dont forget to klik the add button to make sure the configuration is added to tunnel list.
open the connection and use your password (ssh user and password, usually the same as webhosting control panel user and password
Now, instead connecting to your remote mysql, you can connect to mysql trough localhost. it would be tunneled by putty to your remote mysql.
try to connect using mysql administration tool first. to test wheter or not the connection is success.
the way ssh tunnel work is almost the same as vpn, from user view.
but without the hassle of setting up vpn server on remote host.
Related
i have SQL server express 2012 installed on windows 10 64 bit, the connection from same computer worked fine, problem when i connect from another computer on same network,
i get error message:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connection
-provider TCP provider error 0 the wait operation timeout
i try the following things but not fix the problem:
Enable and automaticly start SQL Browser
Enbale TCP/IP and set port 1433 for all IP types
Start Named inslance SQLExpress2012 autmaticaly as Build-in : Network service
connect without user and password with IntegratedSecurity=true
The database is using mixed-mode authentication
add an exception in the firewall for port 1433
enable named pipe
allow remote connection
this is my code:
public static bool TestSQLServerConnection(
string ComputerNameOrIPAddress,
string SQLServerInstanceName,
string PortNumber)
{
try
{
SqlConnectionStringBuilder SQLServerConnectionString = new SqlConnectionStringBuilder();
SQLServerConnectionString.DataSource = ComputerNameOrIPAddress+",+"+PortNumber+"\\"+SQLServerInstanceName;
SQLServerConnectionString.InitialCatalog = DatabseName;
SQLServerConnectionString.TrustServerCertificate = true;
SQLServerConnectionString.IntegratedSecurity = true;
//SQLServerConnectionString.UserID = "...";
//SQLServerConnectionString.NetworkLibrary = "DBMSSOCN";
//SQLServerConnectionString.Password = "...";
SqlConnection con = new SqlConnection(SQLServerConnectionString.ConnectionString);
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText= "select 1";
con.Open();
cmd.ExecuteScalar();
con.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
when i ping the ip address or computer name of computer of SQL Server it connected and return response, means the computer is connected to network.
i search for while without any solution, please help me.Thanks
I add
SQL Browser Service UDP Port 1434 on firewall and worked fine.
Use SQL Server Management Studio to connect remotely to an SQL Server Express instance hosted on an Azure Virtual Machine
I downloaded SQL Server 2016 Express and Visual Studio 2015.
I get this message when I try to open SQL Server is my Visual Studio program.
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: Named
Pipes Provider, error: 40 - Could not open a connection to SQL Server)
My code is
// _ConnectionString = "Server = ARKY/SQLExpress; Database = Tracker; User Id = Track;Password =Track;Trusted_Connection=True;";
_ConnectionString = "Server = ARKY/SQLExpress; Database = Tracker;Trusted_Connection=True;Connect Timeout=10";
SqlConnection myConnection = new SqlConnection(_ConnectionString);
try
{
myConnection.Open();
return "";
}
catch (Exception e)
{
return e.Message + " " + _ConnectionString;
}
I tried my different connection strings. This is one example.
ARKY is the name of my computer and SQLEXPRESS is my named instance.
See the screen shot of my SQL Server database. I will appreciate any suggestions.
Try this:
_ConnectionString = #"Data Source=ARKY\SQLExpress;InitialCatalog=Tracker;Trusted_Connection=True;Connection Timeout=10";
You need a backslash rather than a front slash in your server name, and several parameters are misnamed.
In general, you can open "Server Explorer" in VS, add a data connection to your server there, then click on the data connection and look for the connection string in the properties window. You can cut and paste from there.
Try to use this one
_ConnectionString = "Data Source=ARKY/SQLExpress;Initial Catalog=Tracker;Trusted_Connection=True;Connect Timeout=10";
_ConnectionString = "Server = ARKY\\SQLExpress; Database = Tracker;Trusted_Connection=True;Connect Timeout=10";
Check your connection string as well
From FIX : ERROR : Could not open a connection to SQL Server:
Check if your SQL server services is up and running properly:
Go to All Programs > Microsoft SQL Server 2016 > Configuration Tools > SQL Server Configuration Manager > SQL Server Services
Check to make sure SQL Server service status is Running.
In addition, ensure that your remote server is in the same network. Run sqlcmd -L to ascertain if your server is included in your network list.
Enable TCP/IP in SQL Server Configuration
When two or more SQL Servers are connected across network they do all communication using TCP/IP. The default port of SQL Server installation is 1433. This port can be changed through SQL Server Configuration Manager. TCP/IP should be enabled for SQL Server to be connected.
Go to All Programs >> Microsoft SQL Server 2016 >> Configuration Tools >> SQL Server Configuration Manager >> Select TCP/IP
Right Click on TCP/IP >> Click on Enable
You must restart SQL Server Services for all the changes to take effect. Right click and go to menu properties to select location where default port of SQL Server can be changed.
I'm building a winform application with C# lang and MySQL database. There's 3 computer to work with.
1st computer connected with LAN network (Static ip address), it creates hotspot with connect*fy app, and MySQL database server
2nd computer connect to 1st computer with WirelessLAN, connect to its connect*fy app
3rd computer connect to 1st computer with LAN cable (static IP address)
I can only connect 1st and 2nd computer. When I try with 3rd computer it showing message "Unable to connect any specified MySQL host"
Connection Strings that I used
string user = "koas";
string password = "poipoi";
string database = "bke_nota_db";
string host = "10.20.10.129";
MySqlConnection conn = new MySqlConnection("server=" + host +
";database=" + database +
";username=" + user +
";password=" + password +
";port = 3306" +
";");
Server's IP address that I used is 1st computer static IP adress that connect to LAN.
What should I do?
I'm developing an app with C# and I need to access MySql database in my university server. They provided me one login and password. With they, I can access the MySql via PhpMyAdmin, but when I try to connect with my app, the following error occurs:
"Unable to connect to any of the specified MySQL hosts."
I thought that could be the privileges of the user. But I'm not sure. So, what could be? With the possible solutions I can ask them to correct.
My connection code:
server = "server";
database = "database";
uid = "id";
password = "pass";
connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
conn = new MySqlConnection(connectionString);
conn.Open();
Thanks a lot! I'm sorry for my bad English. =(
Mysql server's default port is 3306. You need to add a connectionstring in web.config and use them all over in your web-app.
As Meda written in her answer you need to use localhost or 127.0.0.1 it must be the ip of your server where you actually host your mysql database. remember that remote access must be enabled (on your university server) if you provide the IP to access the mysql database.
Tried these thing will surely fix your issue.
I use MySQL with my C# projects and I define the host value as:
localhost or 127.0.0.1
an IP address (xxx.xxx.xxx.xxx)
a host url (my.dbserver.com)
here is a working MySQL connection string:
<add name="connectionString"
connectionString="server=xx.xx.xx.xx;database=yyyyy;
User Id=zzzzzz;Password=**********;"
providerName="MySql.Data.MySqlClient" />
I am going to make a smaill application with c# .net 2.0 here I have to connect the Mysql server i am trying many tutorial but fail
I am using
string constr = "Server=11.22.33.123;Port=3306;Database=bfcerin_ad;" +
"Uid=<user id here>;Pwd=<password here>;pooling=false;";
MySqlConnection con = new MySqlConnection(constr);
con.Open();
I have tried twice my user name and password is fine but getting access denied from Server
I am able to connect to the localhost mysql with this code but not remote mysql server
Login your cpanel and click on Remote MYSQL and then add Remote Database Access Hosts put % for giving access to all host.