Connect to remote mysql host from network with proxy - mysql

Ubuntu machine in internet zone with proxy server.
And i want to connect to remote mysql server mysql -u userName -p -h hostname.
And also i have java application that connect to that remote mysql host using hibernate ORM.
And i can't connect with the server.
Edit
I dont want to use mysql proxy but my database exists on remote server but my local machine have connection to the internet throw web proxy so i can't connect to remote db server.
And when i try connect from machine that has direct access to the internet That Work Perfectly
Trials.
1-System -> Preferences -> Network Proxy.
2- export http_proxy = http:userName:password#proxyserver:port
but that doesn't work.
and help will be appreciated..
Update:
static {
// SOCKS Proxy
System.setProperty("proxySet", "true");
System.setProperty("socksProxyHost", "proxy-host");
System.setProperty("socksProxyPort", "proxy-port");
System.setProperty("http.proxyHost", "proxy-host");
System.setProperty("http.proxyPort", "proxy-port");
System.setProperty("java.net.socks.username", "username");
System.setProperty("java.net.socks.password", "password");
Authenticator.setDefault(new ProxyAuthenticator("username", "password"));
// Open Web Site Work good
try {
URL url = new URL("http://www.kooora.com/");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
// Read it ...
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (IOException iOException) {
iOException.printStackTrace();
}
// But open mysql connection throw exception
Class.forName("com.mysql.jdbc.Driver").newInstance();
return DriverManager.getConnection(getDBUrl(), USERNAME, PASSWORD);
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.

Related

AS3: negotiating FTP-ES connection

I'm trying to modify an ActionScript 3 FTP library to support explicit secure (FTP-ES) connections.
With FTP-ES, the client initially makes an insecure FTP connection, then explicitly asks the server to switch to a secure SSL/TLS connection.
Right now, I initially connect to the FTP server with a regular Socket, ask the server to switch to TLS, and then try connect with a SecureSocket after the server returns FTP code 234:
if (code.indexOf('220 ') == 0 || code.indexOf('220-') == 0) { //Send user name
if (_useTLS) {
trace("AUTH TLS");
cmdSocket.writeUTFBytes('AUTH TLS\r\n');
} else {
cmdSocket.writeUTFBytes('USER ' + _username + '\r\n');
}
cmdSocket.flush();
}
if (responseCode.indexOf('234 ') == 0) { //Auth OK, expecting SSL/TLS negotatiation
if (SecureSocket.isSupported) {
secureCommandSocket = new SecureSocket();
secureCommandSocket.addEventListener(ProgressEvent.SOCKET_DATA, onReceivedSCmd);
secureCommandSocket.addEventListener(Event.CONNECT, onConnectSCmd);
secureCommandSocket.addEventListener(IOErrorEvent.IO_ERROR, onSocketError);
secureCommandSocket.addEventListener(Event.CLOSE, onCloseSCmd);
secureCommandSocket.connect(_host, _port);
} else {
throw new Error("Secure socket is not supported on this platform");
}
}
However, the socket returns an IOError 2031 (socket error). Here's the output:
MESSAGE: CONNECTED TO FTP
MESSAGE: Received command: 220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 1 of 50 allowed.
AUTH TLS
MESSAGE: Received command: 234 AUTH TLS OK.
MESSAGE: 2031:Error #2031: Socket Error. URL: xx.xx.xxx.xx
ERROR: Connection failed
With no more information about what's going wrong, I have no idea why the secure socket connection is failing. Is it because I'm trying to switch from a regular Socket to a SecureSocket mid-connection? If so, is there any other way to handle this in AS3?

Unable to connect to MySQL Database with Java through SSH

I know that there are many questions regarding this problem already, and believe me I've looked through all of them.
Some background, I'm using IntelliJ and I've been able to both connect to the database with IntelliJ's data source tool and I've been able to SSH into the server itself using the terminal. For some reason, even if I try to do the same thing with Java it doesn't work. Here's my code:
String serverIP = "123.45.67.890"
String username = "root";
String password = "password";
// Establish SSH tunnel with server if not running locally
try {
if (!Inet4Address.getLocalHost().getHostAddress().equals(ServerIP) {
System.out.println("Remote connection detected.");
System.out.println("Establishing SSH Tunnel...");
JSch jSch = new JSch();
Session session = jSch.getSession(username, ServerIP, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
System.out.printf("Connecting to remote server at %s...\n", ServerIP);
session.connect();
System.out.println("Connected!");
session.setPortForwardingL(3306, ServerIP, 3306);
}
} catch (Exception e) {
e.printStackTrace();
}
// Load mySQL Driver
System.out.println("Loading driver...");
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded!");
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Cannot find the driver in the classpath!", e);
}
String url = String.format("jdbc:mysql://%s:3306/db_name", ServerIP);
// Connect to database
System.out.println("Connecting to database...");
try (Connection connection = DriverManager.getConnection(url, username, password)) {
System.out.println("Database connected!");
this.connection = connection;
} catch (SQLException e) {
this.connection = null;
throw new IllegalStateException("Cannot connect to database!", e);
}
And here's the error I get:
Remote connection detected.
Establishing SSH Tunnel...
Connecting to remote server at 123.45.67.890...
Connected!
Loading driver...
Driver loaded!
Connecting to database...
java.lang.IllegalStateException: Cannot connect to database!
...
a bunch of crap
...
Caused by: java.sql.SQLException: null, message from server: "Host 'my.personal.computer.local.hostname' is not allowed to connect to this MySQL server"
...
a bunch more crap
...
It seems to me like for some reason, the Java code isn't using the SSH tunnel I put so much effort into copying StackOverflow code to make, and is instead rudely trying to connect normally. What am I doing wrong?
Some more info: The server itself is configured to listen for SQL on 0.0.0.0 and I also set the firewall to accept connections on port 3306.
Edit for clarity: I understand that I can probably fix this by giving my local computer privileges on the server, but I want to be able to connect through SSH.
Solution by OP.
The reason SSH wasn't working was because I needed to use a local open port, connect to SQL through that, then forward that to the remote port 3306. Also, I needed to port forward through localhost, not the server address.
(That is, change the session.setPortForwardingL(3306, ServerIP, 3306); line to session.setPortForwardingL(some-open-port, "localhost", 3306); and "jdbc:mysql://%s:3306/db_name" to "jdbc:mysql://%s:same-open-port/db_name".

JDBC Connection error persists

I am using MySQL Workbench (5.6.19). It has several connections among which the table that i want to access is in "Connection1"(name of connection). Connection Host: 122.0.0.0 . My database name is "sorder". user = "root", password = "password",port = 3306.
Here's my piece of code:
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
PrintWriter pw = new PrintWriter(System.out, true);
pw.println("Driver Connection Failed.");
e.printStackTrace();
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try
{
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sorder","root", "password");
} catch (SQLException e)
{
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
My driver connection failed. Plus i am not sure what my path Strin url should be since there are more than 1 connections in mySQL workbench.
error :
-------- MySQL JDBC Connection Testing ------------
Driver Connection Failed.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.apache.jsp.NewFile_jsp._jspService(NewFile_jsp.java:65)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
I followed whatever suggestions i got in different forums, like:
reassured that JAR file is there in build path.
reassured that JAR file is there in Window>Prefernces>Java>Build Path>Classpath variable
JAR file explicitily in WEB-INF/lib buildpath.
Thankful for any suggestions.
Regards.
when we manually created a xml file and added welcome-file-list, it worked. Default xml file was missing.

Unable to connect to any of the specified MySQL hosts in C# using SSH.Net Tunnel

I'm trying to connect to a remote mysql server using a SSH tunnel set up using OpenSSH key file and SSH.Net library as shown in the code section below...
try
{
PrivateKeyFile pkfile = new PrivateKeyFile("C:\\Users\\OpenSSHKey", "mypassword");
var client = new SshClient("servername.com", 22, "myusername", pkfile);
client.Connect();
if (client.IsConnected)
{
var local = new ForwardedPortLocal(22, "localhost", 3306);
client.AddForwardedPort(local);
try
{
local.Start();
}
catch (SocketException se)
{
}
string connStr = "server=localhost;user=mysql_username;database=database_name;port=3306;password=MyDBPassword;";
MySqlConnection sql = new MySqlConnection(connStr);
sql.Ping();
try
{
sql.Open();
}
catch (MySqlException se)
{
}
}
}
catch(Exception e)
{
}
the tunnel is definitely set up because it throws the following error "Unable to connect to any of the specified MySQL hosts" on the sql.Open() call...
Im sure the database is on the server named mysername because I can connect to it using putty or workbench with the credentials used in the MYSQL connection string... I hope someone can help
I've just solved exactly this problem myself. Simply change your ForwardedPortLocal to:
var local = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306);
In your original, you were giving the source port as 22. However, your MySql connection would have been connecting to port 3306 on your local machine (which you would then tunnel across to port 3306 on the destination), so you need to ensure that the forwarded port is listening locally on 3306 to start with.
In addition, I've changed localhost to 127.0.0.1. A secondary issue I tripped over was that localhost on my machine was actually resolving to ::1, an IPv6 address, and the destination server didn't 'speak' IPv6. By switching this to the 127.0.0.1 (IPv4's version of localhost), it ensures that IPv4 is used by default.

connecting to MySQL using JDBC in eclipse

So I want to create a JDBC connection to MySQL server that is installed on my pc, here are the steps,
I installed MySQL with the username and password "root", downloaded mysql-connector-java and from theere I coped the JAR "mysql-connector-java-5.1.12-bin" to "C:\Sun\SDK\jdk\jre\lib\ext", I then added it as an external JAR in my project in eclipse, now in my class I have this code:
public void initialiseDatabase()
{
try {
// Load the Driver class.
Class.forName("com.mysql.jdbc.Driver");
//Create the connection using the static getConnection method
databaseConnection = DriverManager.getConnection (databaseUrl+databaseName,
dbUserName, dbPassword);
sqlStatement = databaseConnection.createStatement();
}
catch (SQLException e) {e.printStackTrace();}
catch (Exception e) {e.printStackTrace();}
}
(this is going to be psuedocode cause I am reading from a properties file and don't want the one helping me reading through long lines of code from main to figure out all the variables),
where databaseUrl = "127.0.0.1"
dbUserName = "root"
dbPassword = "root"
databaseName = "MySQL" //this one I am not sure of, do I need to create it or is it set inherenrly?
now the MySQL server is up and running, but when I call the method initialiseDatabase the following exception is thrown:
"java.sql.SQLException: No suitable driver found for rootroot
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Proxy$JDBCConnection.initialiseDatabase(Proxy.java:721)"
when line 721 is:
sqlStatement = databaseConnection.createStatement();
Where have I gone wrong?
thanks
Your database url should look like this:
jdbc:mysql://host:port/database
Example, if you use localhost, the default port and a database named cachedb, your url would be:
jdbc:mysql://localhost/cachedb