Translating PHPMyAdmin MySQL connection configuration to JDBC - java

I have PHPMyAdmin connecting to a remote MySQL server, using those parameters:
$cfg['blowfish_secret'] = 'ba17c1ec07d65003';
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '*remoteServer*:3306';
$cfg['Servers'][$i]['extension'] = 'mysql';
$cfg['Servers'][$i]['controluser'] = '*user*';
$cfg['Servers'][$i]['controlpass'] = '*password*';
I'm trying to connect to this MySQL server in Java, neither of those work:
final String url ="jdbc:mysql://*remoteServer*:3306/*user*";
Connection conn = DriverManager.getConnection(url, "*user*", "*password*");
and, with ":3306" removed :
final String url ="jdbc:mysql://*remoteServer*/*user*";
Connection conn = DriverManager.getConnection(url, "*user*", "*password*");
I'm getting this error:
SQLException: 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.
SQLState: 08S01
VendorError: 0
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)
...
I think it comes from the "blowfish_secret" (which is just a random cookie) and "auth_type"="cookie" not being accounted for in Java.
How do I fix this?
Also, do I need to tell MySQL that he has to accept to talk to the jdbc driver? (if yes, I'm screwed, I don't have access to the MySQL config files)

blowfish_secret and auth_type are used only as part of the phpmyadmin software, they have nothing to do with a mysql connection.
Do you have firewalls allowing access to MySQL from the machine running java and do you have grants set up to access that user from that specific host?

Related

Code runs for sometime and then throws Communications link failure Exception MySQL

I have a Scala code using scalalikejdbc to connect and do INSERTs into MySQL. The code runs fine for about 2-3 hours and then throws
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.
My connection settings are as below:
val url = "jdbc:mysql://www.domain.com:3306/schema";
val settings = ConnectionPoolSettings(
initialSize = 1,
maxSize = 2,
connectionTimeoutMillis = 100000L,
validationQuery = "select 1 from dual");
ConnectionPool.add('dbval, url, username, pwd, settings);
Any reason why this is happening and how do I resolve it?
I found the solution, I was opening a new connection before insert and closing it after INSERT. This was perhaps causing some kind of a bottleneck due to too many INSERT queries one after the other.
Resolution: I opened only one connection and added it to the ConnectionPool and used the same connection for every INSERT.

MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up [duplicate]

This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 5 years ago.
I'm having trouble connecting to my database. I think there is a setting somewhere that I don't know about and needs to be changed. I have some really basic code here:
public static void main(String[] args)
{
try
{
Connection con = DriverManager.getConnection("jdbc:mysql://IP:3306/TABLENAME?autoReconnect=true","USERNAME", "PASSWORD");
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Obviously with the login info. And it causes this error:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Caused by: 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
I've looked through about 10 different posts about the same problem and tried their solutions and still couldn't get it to work.
This problem occurs when the MySQL service is not running and you're trying to connect to the mysql database.
To resolve this in Windows,
Start >> type 'services' and open it.
Find the MySQL service. Probably with name MySQLxx. xx being the version.
eg. MySQL56, MySQL57 etc
Start the service and now try to connection to database.
Increase Mysql max connection,
SET GLOBAL max_connections = 1500;
If still getting an error then queries might be taking too long to respond.
In that case increase attempt while creating jdbc connection,
&maxReconnects=XX
You have to use database name instead of table name

Connecting to remote MS sqlserver in java using windows authentication

I am using sqlserver with Windows authentication, with a Windows server.
I am trying to connect to a remote MS SQLSERVER on my local network using java eclipse but keep getting this error:
Error : com.microsoft.sqlserver.jdbc.SQLServerException: The port number 64038 databaseName = Data is not valid.
Here is the code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("# - driver loaded");
String server = "moddbs169d.network.local\\Moddbs169d\\SQL2008";
int port = 64038;
String database = "Data";
String jdbcUrl = "jdbc:sqlserver://"+server+":"+port+" databaseName = "+database+";integratedSecurity=true";
Connection con = DriverManager.getConnection(jdbcUrl);
System.out.println("# - Connection obtained");
If all is successful it should tell me connection obtained. The local connection and name of the database are both correct, so that is not the issue. The jdbc driver is also installed and working properly.
You are missing a semicolon before the "databaseName=xxx" property.
Without the semicolon, you are setting the port number to "64038 databaseName = Data". Admittedly, the error message could have used brackets to make it a bit clearer.
See (http://msdn.microsoft.com/en-us/library/ms378428(v=sql.110).aspx) for the form of the connection URL.

Unable to Connect with my Database 000webhost.com

try {
String connectionURL = "jdbc:mysql://sql4.000webhost.com/a7291194_xxx";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "a7291194_xxx", "xxx");
if(!connection.isClosed())
System.out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}catch(Exception ex){
System.out.println("Unable to connect to database"+ex);
}
With the following connector code I am trying to connect with the database on 000webhost.com but it gives me an error:
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.
I am trying my best for the first time as I am totally new to webhosting so please help me thanks in advance!
its same either on your local or on server
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://DomainName OR IP:3306/databasename", "username", "password");
At 000webhost.com remote MySQL connections are disabled for security and server performance reasons.
If you upgrade your account, MySQL connections will be enabled.

Unable to establish database connection to SQL Server 2008 using java in Eclipse IDE

I am trying to connect to HP Operations Manager Database using Java code in Eclipse IDE.
I am able to connect successfully through Microsoft SQL Server Management Studio 2008 but it fails through code.
I have installed "Microsoft JDBC Driver 4.0 for SQL Server"
Code:
import java.sql.*;
public class ConnectDatabase {
Connection dbConnection = null;
String dbName = "openview";
String serverip="10.105.219.102";
String serverport="1433";
String url = "jdbc:sqlserver://"+serverip+"\\OVOPS;databaseName="+dbName+"";
String userName = "HPOM-QA-WIN\\Administrator";
String password = "Nbv12345";
final String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Statement statement = null;
ResultSet rs = null;
int updateQuery = 0;
public Connection getConnection() {
System.out.println(url);
try{
Class.forName(driverName).newInstance();
dbConnection = DriverManager.getConnection(url,userName,password);
System.out.println(DriverManager.getDrivers());
statement = dbConnection.createStatement();
String QueryString = "select Id from openview.dbo.OV_MS_Message where OriginalServiceId like '{FaultDn[1]}'";
updateQuery = statement.executeUpdate(QueryString);
if(updateQuery!=0){
System.out.println("success" + updateQuery);
}
statement.close();
dbConnection.close();
}catch (Exception e){
e.printStackTrace();
}
return dbConnection;
}
public static void main(String[] args) {
ConnectDatabase cDB = new ConnectDatabase();
cDB.getConnection();
}
}
I get the following error when I execute this code:
jdbc:sqlserver://10.105.219.102\OVOPS;databaseName=openview com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host 10.105.219.102, named instance ovops failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:3589)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:1225)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:972)
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 com.ucs.test.ConnectDatabase.getConnection(ConnectDatabase.java:27)
at com.ucs.test.ConnectDatabase.main(ConnectDatabase.java:51)
When I change the url to
String url = "jdbc:sqlserver://"+serverip+"\\OVOPS:"+serverport+";databaseName="+dbName+"";
I get the below error:
jdbc:sqlserver://10.105.219.102\OVOPS:1433;databaseName=openview com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'HPOM-QA-WIN\Administrator'. ClientConnectionId:f1d323b7-9998-418c-b2a2-f2a7bd7b9b04
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 com.ucs.test.ConnectDatabase.getConnection(ConnectDatabase.java:27)
at com.ucs.test.ConnectDatabase.main(ConnectDatabase.java:51)
I have explicitly added an inbound rule in windows firewall to allow UPD traffic on 1434 port, then disabled the firewall. But I still get this error.
The credentials provided here are used for connection using Microsoft SQL Server Management Studio and it works perfectly fine. But it fails through the code.
I am not sure where I am going wrong. I am unable to establish a successful connection through the code. Please help me.
Hey Thanks all for your responses. Finally I was able to resolve the issue. The problem was with the url and auth dll. Changed the url to
"jdbc:sqlserver://10.105.219.102:1433;instance=OVOPS;DatabaseName=openview;integratedSecurity=true"
and added the location of "sqljdbc_auth.dll" in java.library.path. It worked!
Thanks again for your efforts to help me :)
It took me a while to sort this issue out, but you have to head into the SQL Server Configuration Manager application. When that's loaded, expand the [SQL Native Client 11.0 Configuration] > Client Protocols.
Enable all three (Shared Memory, TCP/IP, and Named Pipes), if they aren't already.
Then click on TCP/IP and ensure that the default port is 1433.
If you have a 32bit system or version of SQL Server installed, do the same in the SQL Native Client 11.0 Configuration (32it) menu, enabling Shared Memory, TCP/IP, and Named Pipes and setting the default port to 1433.
Then click open the [SQL Server Network Configuration] or (32bit if applicable), and select the [Protocols for "YOURSERVERNAME"].
Ensure again that Shared Memory, TCP/IP, and Named Pipes are all enabled.
Then click on the [TCP/IP] Protocol Name, then select the [IP Addresses] tab at the top of the new popup window. For IP1 ensure that Active is YES; Enabled is YES (this is No by default, even if it is Active); and set the TCP Port to 1433 (tbh I don't know if you have to do this step, but I did and it worked!!); my TCP Dynamic Ports are set to 0 and I didn't change any of the IP addresses;
I did the same thing for IP10, which has IP: 127.0.0.1, which is the local machine.
I also scrolled down to the bottom of the page, and set IPAll TCP Ports to 1433, (dynamic ports is 49163).
Then you need to Apply all changes, close the properties window, and click on SQL Server Services in SQL Server Configuration Manager, and restart all running Servers.
This should do it :D
Catch example:
String url = "jdbc:sqlserver://localhost:1433/databaseName";
String username = "user";
String password = "pass";
Connection connection = DriverManager.getConnection(url, username, password);
1433 is default port.
Dont use '\' in url.
This is probably not an answer to your question, but I hope this will help you. Here is the way how to properly return connection (mysql):
import java.sql.*;
public class ConnectToDatabase{
public Connection getConn(){
final String DBPATH="jdbc:mysql://localhost:3306/mydb";
final String DBUSER="root";
final String DBPASS="";
Connection conn=null;
try {
conn = DriverManager.getConnection(DBPATH,DBUSER,DBPASS);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
Try changing your database user password. Make sure new password doesn't have special characters.
I faced same problem and it got resolved after changing the db password.
Steps of my workaround:
I was getting sqlserverexception.java 190 error stating unable to connect to host port: 4500 which was my localhost port.
Opened Sql Server Configuration Manager -> SQL Server Network Configuration -> Protocols for MSSQLSERVER -> Enabled TCP/IP.
Checked TCP port number. It was 1433. So I changed 4500 to 1433 in my registry file.
After following above steps, I got a different error this time startingpassword for my db user(sql-test) is incorrect. I changed the password to sqltest123 and it worked.

Categories