Cannot open port 1433 for Azure SQL database in Mac - java

I'm trying to access a Azure-hosted SQL database on my java application. I checked the port 1433 using nmap and it shows that it's closed:
Starting Nmap 7.12 ( https://nmap.org ) at 2016-09-02 09:44 PHT
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00014s latency).
Other addresses for localhost (not scanned): ::1
PORT STATE SERVICE
1433/tcp closed ms-sql-s
I have edited the /etc/pf.conf and restarted my mac but the port is still closed. Here is my pf.conf:
scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"
pass in proto tcp from any to any port 1433
Also, my firewall is set to off.
My java app is throwing this error:
Error starting database: The TCP/IP connection to the host flowengine7.database.windows.net, port 1433 has failed. Error: "Connection timed out: no further information. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
Java code:
String connectionString =
"jdbc:sqlserver://dbName.database.windows.net:1433;database=dbName;user=user##user;password=passwordhere;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
Connection connection = null;
try {
connection = DriverManager.getConnection(connectionString);
println 'connected';
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) try {
connection.close();
} catch (Exception e) {
}
}

You're overlooking the Azure SQL server-level Firewall:
https://azure.microsoft.com/en-us/documentation/articles/sql-database-configure-firewall-settings/
You need to whilelist your Mac's Public IP address in there to be able to connect from local. By default only Azure services can reach 1433/TCP on your Azure SQL instance (permitted by Allow access to Azure Services setting in the Azure SQL Firewall).
If you're getting a new Public IP address every time you reboot your DSL/Cable/Fiber modem you'll need to define a whole range of addresses not just the one (i.e. 174.73.0.0 - 174.73.255.255 vs. listing 174.73.16.180). Hopefully you'll always grab an IP address in the same range.

Related

Why am I getting SQL State: 28000 FATAL: Ident authentication failed for user error message when running a simple Java program?

I have a Postgres 11 database installed on a VirtualBox virtual machine running Centos7 (guest machine), and my host machine is a Mac OS Catalina. Both the guest (Centos) and host (Catalina) have JDK 8 installed.
I have the following simple Java code that uses a single dependency postgresql-42.2.5:
public class JavaApplication5 {
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5332/mydb", "mydbuser", "mydbpassword")) {
if (conn != null) {
System.out.println("Connected to the database!");
} else {
System.out.println("Failed to make connection!");
}
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
I compile and run this on my host (Catalina) :
java -jar JavaApplication5.jar
and the output I get is:
Connected to the database!
So clearly this means I can connect to the Postgres database from my host to my guest. Next, I test whether I can connect directly from my guest. So I copy "JavaApplication5.jar" into guest Virtual Machine running Centos 7 and re-run the same command. However, this time I am getting the following message from the same executable jar file:
SQL State: 28000
FATAL: Ident authentication failed for user "mydbuser"
Does anybody know why I am getting this error on the guest virtual machine but not on the host?
Here is the relevant portion of my my pg_hba.conf file:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
host all all 0.0.0.0/0 md5
You must have port forwarding set up in your VirtualBox to capture connection attempts on the host to that port and route them to the VB. So while you specify 127.0.0.1, they are coming from someplace else as far as PostgreSQL is concerned. That matches the 'md5' line of your pg_hba.conf. When done from the guest, the connections really are coming from 127.0.0.1, and so match the earlier 'ident' line.
The 'ident' authentication is failing, for reasons surely mentioned in the log file. If you don't want to use 'ident', remove those lines.
You're using hardcoded 127.0.0.1 on both machines. 127.0.0.1 should swapped out to be the IP address of the database host or it's FQDN. 5332 instead of 5432 is unusual.

java.net.ConnectException when trying to connect in MySQL [duplicate]

This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 5 years ago.
I try to connect with my DB (MySQL) from a servlet but I get Connection refused error. I read similar questions like this. However:
You have not started your server.
In my local server (Ubuntu server 16.04), with IP 192.168.0.4 is installed MySQL server. MySQL Server is running as the command service mysql status give this output:
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2017-03-21 11:50:56 EET; 1 weeks 1 days ago
Main PID: 1209 (mysqld)
You are trying to connect to the wrong IP/port.
MySQL is listening to port 3306, as the command netstat -an | grep 3306.
gives the following output
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
A firewall is blocking your connection before it reaches your server.
The server's firewall is disabled, as ufw status
Status: inactive
I disabled my PC's firewall too but it didn't solve the connection error.
Your server has too many pending connections waiting to be accepted.
My server is fresh installed and there is no way to have any pending connections.
Your server is not listening for connections.
I run the commnand nmap -sT -O localhost and I got:
Not shown: 993 closed ports
PORT STATE SERVICE
21/tcp open ftp
22/tcp open ssh
25/tcp open smtp
80/tcp open http
110/tcp open pop3
143/tcp open imap
3306/tcp open mysql
NETBEANS
Me and Server we are in the same network. My Java class which handles the communication with the DB contains:
private final String WEBAPP_DB = "jdbc:mysql://192.168.0.4:3306/myDataBase";
private final String JDBC_DRIVER="com.mysql.jdbc.Driver";
private final String USERNAME = "myUserName";
private final String PASSWORD = "myPassWord";
Connection c = null;
PreparedStatement stm = null;
sq = "SELECT * FROM Machines";
try {
Class.forName(JDBC_DRIVER);
c = DriverManager.getConnection(WEBAPP_DB, USERNAME, PASSWORD);
stm = c.prepareStatement(sq);
ResultSet rs = stm.executeQuery();
while(rs.next()){
System.out.println("Machine = " + rs.getInt("id"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stm != null) {
stm.close();
}
if (c != null) {
c.close();
}
}
and some of the errors I get is
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
...
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
...
Caused by: java.net.ConnectException: Connection refused: connect
Any hint what may be the problem?
If you're trying to connect yo your MySql server remotely, the problem is in the MySql server network configuration, it's listening on localhost (127.0.0.1):
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
It should be:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
Or at least, instead of 0.0.0.0, your server ip address (192.168.0.4).
I suggest to read this How to Bind to Static IP address in MySQL
On the other hand, if you're connecting locally (your code is executed on MySql server, which is the same machine where NetBeans runs) you should change your connection string into jdbc:mysql://localhost:3306/myDataBase
P.S. So at resuming your questionnaire :)
You are trying to connect to the wrong IP.

JDBC connection failed, error: TCP/IP connection to host failed

I want to connect Java class file with SQL server 2012. I have logged in with SQL server authentication, but I am receiving an error when connecting.
Error:
The TCP/IP connection to the host 127.0.0.1, port 1433 has failed.
Error: "Connection refused: connect. Verify the connection properties.
Make sure that an instance of SQL Server is running on the host and
accepting TCP/IP connections at the port. Make sure that TCP
connections to the port are not blocked by a firewall.".
My code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //1. Register your driver
//2. get the connection object
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=aysha","sa","admin");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;databaseName=aysha","user=sa","password=admin");
//"jdbc:sqlserver://127.0.0.1:1433; Instance=SQL2008;" + "databaseName=MB;user=sa;password=123;";
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=aysha","sa" , "password");
//3. Prepare a statement
Statement stmt = con.createStatement();
//4. Write the query`
String sql = "Select * from employee";
//5. Execute the statement and
ResultSet rs = stmt.executeQuery(sql);
//6. Process the result set
while (rs.next())
{
System.out.println(rs.getInt(1));
}
Open SQL Server Configuration Manager, and then expand SQL Server 2012 Network Configuration.
Click Protocols for InstanceName, and then make sure TCP/IP is enabled in the right panel and double-click TCP/IP.
On the Protocol tab, notice the value of the Listen All item.
Click the IP Addresses tab:
If the value of Listen All is yes, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item under IPAll.
If the value of Listen All is no, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item for a specific IP address.
Make sure the TCP Port is 1433.
Click OK.
Easy Solution
Got to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager ->Expand SQL Server Network Configuration-> Protocol ->Enable TCP/IP Right box
Double Click on TCP/IP and go to IP Adresses Tap and Put port 1433 under TCP port.
The error is self explanatory:
Check if your SQL server is actually up and running
Check SQL server hostname, username and password is correct
Check there's no firewall rule blocking TCP connection to port 1433
Check the host is actually reachable
A good check I often use is to use telnet, eg on a windows command prompt run:
telnet 127.0.0.1 1433
If you get a blank screen it indicates network connection established successfully, and it's not a network problem. If you get 'Could not open connection to the host' then this is network problem
Go to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool -> Click SQL Server Configuration Manager.
If you see that SQL Server/ SQL Server Browser State is 'stopped'.Right click on SQL Server/SQL Server Browser and click start.
In some cases above state can stop though TCP connection to port 1433 is assigned.
Open %windir%\System32 folder and find SQLServerManagerXX.msc
For example:
C:\Windows\System32\SQLServerManager14.msc
Go to protocols settings then enable TCP/IP port is 1433 by default
As the error says, you need to make sure that your sql server is running and listening on port 1433. If server is running then you need to check whether there is some firewall rule rejecting the connection on port 1433.
Here are the commands that can be useful to troubleshoot:
Use netstat -a to check whether sql server is listening on the desired port
As gerrytan mentioned in answer, you can try to do the telnet on the host and port
important:
after any changes or new settings you must restart SQLSERVER service. run services.msc on Windows
If you are using a named instance, the port you using likely is 1434, instead of 1433, so please check that out using telnet or netstat aforementioned too.
Open Firewall GUI,Open TCP 1433 rule,go to scope tab and add your IP address under Remote IP Address and the problem is solved.

java to sql server connectivity exception

I have this error/exception-
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
and my code is-
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433";
Connection con = DriverManager.getConnection(connectionUrl,"","");
System.out.println("ok");
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
how can i sovle it,please help me.
This could mean almost anything - basically, it was impossible to connect to port 1433 on localhost. Could be a network problem, could be that the database is not started, could be that it is running but not bound to port 1433.
In a pinch, you can always open a shell and run
telnet localhost 1433
to see if it is possible to make a connection at all.
I had this error.
I fixed it by going into the SQL Server Configuration Manager.
Under SQL Server 2005 Configuration -> Protocols for SQLEXPRESS. Look at the IP Address tab of the Properties of the TCP/IP Protocol.
At the very bottom of the protocol list (under IPALL), the TCP Port field was blank. I put 1433 there, saved, restarted the SQL service and it worked great :)
It's telling you it can't connect.
Check that sql server is available on your local machine on that port.
Check that sql server is accepting connections with the blank name and password you're providing in your getConnection() invocation.
Check that your jdbc url is in the correct form, they're sometimes manufacturer/driver specific.
The error just means that java was not able to connect to your database. Either database is not running or your firewall is blocking connections to it. If you are sure your DB server is up on your localhost, try after disabling your windows firewall.
Lets look at some of the possibilities:
The database server is not listening on localhost:1433 : you probably would get a "connection refused" rather than a "connection timed out"
The database server is catatonic (i.e. in a state where it is not responding to requests) : you could get a "connection timed out".
The database server is massively overloaded : you could get a "connection timed out", but you would probably notice that the machine was very slow.
The local software firewall is configured to block all requests to port 1433 : you could get either "connection refused" or "connection timed out" ... or some other response. (It depends exactly what the firewall does with unwanted traffic.)
The loopback interface (for IP 127.0.0.x) is not configured : you are more likely to get "no route to host" or "no route to network".
The DNS entry for localhost is misconfigured (i.e. localhost doesn't resolve to 127.0.0.x) - lots of things could happen ... depending on how it is misconfigured.
Here are some things you could do to diagnose the problem ... depending on your system:
Look at the configured networks using ipconfig or ifconfig.
Look at "/etc/hosts" to check the entry for localhost
Try to connect to the service using telnet -p 1433
Use "ps" or the task manager to see what processes are running.
Check the local firewall configs to ensure that 1433 is open for (at least) TCP from the configured localhost IP address (probably 127.0.0.1).
I had a problem similar to this, and solved it by adding the line:
System.setProperty("java.net.preferIPv4Stack", "true");
The problem was it used IPv6 instead of IPv4 to access the db host (I used it for a DB on a remote host, not localhost).
Hope it helps.

Communication failure during handshake. Is there a MySQL server running on localhost:3306

i am using below code
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root","root"); // cust is the DSN Name
} catch (Exception e) {
System.out.println("Exception "+e);
}
got this exception during run time...
tried netstat -a in command prompt to check if mysql is running on port 3306 below is the trace...
Proto Local Address Foreign Address State
TCP user:epmap user:0 LISTENING
TCP user:microsoft-ds user:0 LISTENING
TCP user:3306 user:0 LISTENING
Well, googling your problem i have come to this solution:
Try reseting your user password executing the next statement at your mysql command line client:
SET PASSWORD FOR 'root'#'localhost' = OLD_PASSWORD('root');
As it seems it kind of changes the authentication method at the mysql server.
Let me know if this have worked.
Other option is to check the compatibility between the version of your MySQL database and the MySQL JDBC driver you are using.

Categories

Resources