MySQL CommunicationsException: Communications link failure [duplicate] - java

This question already has answers here:
java.net.UnknownHostException: Invalid hostname for server: local
(14 answers)
Kafka Consumer Error - xxxx nodename nor servname provided, or not known
(1 answer)
Closed 1 year ago.
I've seen many topics with that issue but unfortunately I couldn't solve my problem. I've installed MySQL on my server (CentOS 7), I'm able to connect with my DB via shell or MySQL Workbench but when I'm trying to run my Java app, then I'm getting errors like these:
com.mysql.cj.jdbc.exceptions.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.UnknownHostException: (my server's ip): nodename nor servname provided, or not known
Firewall or proxy can't block connection since I can connect with DB via Workbench or shell..
What's more, I've followed this topic: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure and:
ip address in application.properties is correct
not sure why it could be not recognized
port number is configured to 3306
my DB is running
server accept tcp/ip connections
can't run out of connections since I can connect in same time with Workbench and shell
firewall and proxy is configured to don't block these connections
any ideas what can be wrong with that?
EDIT:
My application.properties configuartion
#Database - deployment
spring.datasource.url=jdbc:mysql://xxx.xx.xxx.xxx/mydbname?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Warsaw
spring.datasource.username=mylogin
spring.datasource.password=mypassword
#Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=create
spring.jpa.database=mysql
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Related

Why is my MySQL connection disconnecting although autoReconnect is true?

Sometimes my MySQL connecting disconnects after 8 hours (standard wait_timeout value). And everytime I get the following error:
[Server thread/WARN]: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 432,896,866 milliseconds ago. The last packet sent successfully to the server was 432,897,054 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
It says that i have to set autoReconnect to true, but this is how I connect to MySQL:
final Properties prop=new Properties();
prop.setProperty("user",user);
prop.setProperty("password",password);
prop.setProperty("useSSL","true");
prop.setProperty("autoReconnect","true");
con=DriverManager.getConnection("jdbc:mysql://"+host+"/"+database,prop);
As you can see: autoReconnect is enabled, so why is my connection disconnecting and how can i fix that?

Can we configure spring.datasource.url for external mysql server

For my Springboot project I can't connect to a mysql that I had hosted on a site. With a local mysql (workbench) it works well but when I replace the localhost url with that of the host it does not work and I have the following error message:
com.mysql.cj.jdbc.exceptions.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.
here is my code in application properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/word?
spring.datasource.username=root
spring.datasource.password=root
I replace the following line of code with my Database Host:
spring.datasource.url=jdbc:mysql://db.example.net:3306/word?
spring.datasource.url=jdbc:mysql://db.example.net:3306/word?
It could be many things
IPTables/firewalld based firewall on either machines
Firewall on the network
Or simply IP is not reachable
If all the above 3 is done right then it could be a config on MySQL server side to not accept remote connections. Flip that flag (and possibly restart the MySQL service)
I would start from pinging the DB IP. If that's accessible you can rule out the line number 3. Then next I would look for the aforementioned config on mysql server side.
I just received a message from the support, they don't allow connection from any IP address. Thanks for answers!

AWS RDS connection database error : com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

I am trying to deploy my springboot application on an AWS linux instance however i keep getting the following error :
2018-04-24 18:15:27.777 INFO 24020 --- [ost-startStop-1]
o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-04-24 18:15:28.771 ERROR 24020 --- [main] o.a.tomcat.jdbc.pool.ConnectionPool: Unable to create initial connections of pool.
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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_161]
...
2018-04-24 16:58:57.522 WARN 23780 --- [main] o.s.b.a.orm.jpa.DatabaseLookup : Unable to determine jdbc url from datasource
org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException:
Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
This is how i created my applications.properties file :
#database config
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://****-west-1.rds.amazonaws.com:3306/DevManagementDB
spring.datasource.username=root
spring.datasource.password=mypassword
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
The strange part is that i can connect to my RDS database from this springboot application when i am running the application on my local machine. As soon as i try to deploy the application to the linux instance on AWS it fails and i get the above errors.
If your DB is an RDS instance, make sure the public IP address of the device accessing the DB is included in the RDS instance's Security Group. Also make sure the public IP address is included in Inbound channel of the appropriate VPC security group.
I have also faced similar problem.
I have to update my mysql extra parameters from /etc/mysql/mysql.conf.d/mysqld.cnf
Stop mysql server sudo systemctl stop mysql.service
Need to update
bind-address = 0.0.0.0 // replace 0.0.0.0 with required ip, 0.0.0.0 allows all ip
Start mysql server sudo systemctl start mysql.service
also need to create a user who has access with that ip, add permission in mysql
mysql > CREATE USER 'user_name'#'%' IDENTIFIED BY 'PASSWORD'; // replace % with appropriate IP
mysql > CREATE USER 'user_name'#'%' IDENTIFIED BY 'PASSWORD'; // replace % with appropriate IP
mysql > GRANT ALL PRIVILEGES ON *.* TO 'user_name'#'%' WITH GRANT OPTION; // replace % with appropriate IP
mysql > FLUSH PRIVILEGES;
Hope this will help someone

Netbeans Connection to SQLSERVER still not wroking

I am still facing this problem for about 2 days already. It seems I did some researched on Google and as well stack overflow. I followed step by step but still not running.
-> Enable SQL BROWSER
-> Enable TCP/IP PORT and change to 1433
I tried both but still not working. It still giving me this error.
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host ., named instance sqlexpress has failed. Error: "java.net.UnknownHostException: .". Verify the server and instance names, check that no firewall is blocking UDP traffic to port 1434, and for SQL Server 2005 or later verify that the SQL Server Browser Service is running on the host.
This is my connection code:
Connection conn = DriverManager.getConnection("jdbc:sqlserver://.\\sqlexpress;user=sa;password=;database=pharmacy_posic");
My server name: .\sqlexpress
My Username : sa
My password : (blank)
Am I making any mistake on the connection?
Check this it is explained very well:
https://www.youtube.com/watch?v=Z46OHxdEEEI

Java connection to a SQL Server Database: Login failed for user 'sa' [duplicate]

This question already has answers here:
SqlServer: Login failed for user
(14 answers)
Closed 4 years ago.
I am trying to connect to a database that I created using SQL Server 2012, but I keep getting an error. This is the code for the connection:
Driver d = (Driver)Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
String DB_URL = "jdbc:sqlserver://localhost:1433;databaseName=Tema6;user=sa;password=123456";
java.sql.Connection con = DriverManager.getConnection(DB_URL);
And this is the error that I am getting:
Login failed for user 'sa'. ClientConnectionId:e6335e64-ca68-4d72-8939-5b7ded951424
I have enabled TCP/IP protocol from SQL Server Config, I am sure that the 'sa' account is enabled and that the password is correct. Can anyone help me, please?
EDIT: This is the entire stacktrace.
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:e6335e64-ca68-4d72-8939-5b7ded951424
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Connection.main(Connection.java:12)
EDIT2: After replacing the driver with jTDS:
java.sql.SQLException: Login failed for user 'sa'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2893)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2335)
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:609)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:369)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:183)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Connection.main(Connection.java:19)
I don't think it's a driver issue, since the message indicates that it's tried and failed authentication, so at least managed to connect.
First (basic) question I'd ask is whether you're sure '123456' is the right password for the sa account? Test it by logging in with SQL Server Management Studio as 'sa'.
Next I'd try creating a user in SQL Server and supplying those credentials in the connection string (as suggested by Brandon)
Next (for fun) I'd enable mixed-mode authentication and try using my windows user account credentials in the connection string.
These are some instructions I wrote for configuring the networking in SQL Server in the past. Definitely worth double-checking the settings:
Configuring SQL Server Networking
Out of the box, SQL Server 2008 Express does not support TCP connections on a fixed port. To resolve this:
From the windows start menu open "SQL Server Configuration Manager"
Under "SQL Server Network Configuration" --> "Protocols for SQLEXPRESS"
open the TCP/IP properties
On the "Protocol" tab
set enabled to YES
On tab "IP Addresses" scroll to the bottom
Under "IP All"
change the TCP Port to 1433
ensure the TCP Dynamic Ports is empty, delete the zero
Click apply
In SQL Server Management Studio check:
From server instance right click and select properties
Under the Security option : ensure that "SQL Server and Windows Authentication mode" is checked
Click SQL --> click right mouse --> propretise --> Securiti -->tick SQL server and windows Authentication mode
90% success!
This database url is in wrong format
String DB_URL = "jdbc:sqlserver://localhost:1433;databaseName=Tema6;user=sa;password=123456";
instead of this try this
Connection connection = DriverManager
.getConnection("jdbc:sqlserver://localhost:1433;\\SQLEXPRESS;databaseName=Tema6","sa","123456");
I haven't really checked out your stack trace but it is very common for people to forget about allowing remote connections to server. Try right-click on server instance (Object Explorer) > Properties > Connections and check Allow remote connections to this server
Check the port on which sql server is configured. You get this weird error if the port is configured as "dynamic port" and you are trying to connect on the default port.
You could check if this is the issue or not by trying to connect to the sql server through sqlserver client.
Set the authentication mode to "sql server authentication"
Enter the servername and port in the format "servername,port" in the server field
Enter user name and password and click connect
If the connect didnt succeed and you get the exact error message "login failed for user xxx" then this is the issue
https://msdn.microsoft.com/en-IN/library/ms177440.aspx
Go to sql server, select user properties and change server roles to sysadmin solved the issue.

Categories

Resources