mysql proxy socks - mysql

Plain and simple, can anyone explain me how to connect to a mysql server through a proxy (socks4/5). Preferable via the mysql command line (although there are no options for that in the client).
If it's not possible through the mysql command line than ANY other method will work.

All you need is to install and configure tsocks (transparent socks). It's available in most if not all linux distibutions. Afterwards you only need to prefix your command with 'tsocks', for example:
tsocks mysql -h -P .....

To my knowledge, it can't be done through the command line because the mysql command does not support proxy connections.
If both client and server are on a UNIX machine and one of them is accessible from the outside, I suggest using an SSH tunnel. It's basically a securely tunneled TCP connection that can be used for anything and the local mysql command can connect to the tunnel port easily.
If that's no option, you could write some kind of wrapper executable. For example, Java and Ruby have libraries that enable you to talk to SOCKS proxies and hook those sockets up to a MySQL or JDBC protocol implementation. What happens then depends entirely on what you have planned next.

If you've got admin access to the proxy server would there be much mileage in installing MySQL Proxy on it?

Related

Mysql redirect traffic to another server

is it possible to redirect entire mysql query to another server?
I've have many apps in differents server that comunicate with a single database (windows machine).
For transfer database (to a linux server) without change any IP inside many many php files (there are a lot of things that i don't know because is not my creation but is my legacy) there's something i can do?
Proxy?port forwarding?
Afaik, mysql implementations don't offer this functionality, however, you can "proxy" connections using an ssh tunnel like this:
ssh -N ssh-user#host.domain -L 3306:127.0.0.1:3306
This requires an ssh-server on the host that serves the mysql (on port 3306) and you having ssh-access to that machine. You run the command on the machine your php is excecuted from, then you can, as long as it runs, access the mysql host on "localhost". All requests will be securely tunneled to the remote mysql server.
The -L switch instructs ssh to forward tcp connections on local port 3306 to port 3306 on the machine you ssh to.
The -N switch instructs ssh to do just that and not run a command.
If you add the -f switch, the command will immidiately fork into background, which may be useful for this usecase.
You probably also want to use passwordless ssh
However, this is probably not a clean solution for production. I use this method only for development purposes and I'd suggest you fix your codebase on the long run (i.e. put your mysql configuration in a central place).

Connect PHP to database hosted within PhpStorm via SSH Tunnel

I used this tutorial to connect to a remote MySQL database via SSH tunnel within PhpStorm. When I try to connect to this via PHP, the database is not found.
The URL that PhpStorm gives me is jdbc:mysql://localhost:3306/dbName
How can I connect to this database via PHP script? (running via the default built-in php server in PhpStorm).
"Connect PHP to database hosted within PhpStorm"
First of all -- PhpStorm does not host any databases (especially MySQL) -- it only can connect to them (it acts like built-in DB client/viewer/editor).
It uses own SSH tunnel (interacts with it directly in the code -- so DB connection is aware of such possibility).
Generally speaking -- for your PHP code to be able to connect to a remote database via SSH tunnel .. you should either:
establish such SSH connection in your code and then use it internally (not sure at all if PHP can do that, especially if DB-related routines can use it -- most likely not)..
or establish SSH tunnel outside as OS level so it's available to all processes. This way it will be transparent to your code (it will not be aware of tunnel thinking that it connects to local/remote DB in a "natural" way).
So the 2nd option is the way to go.
Here you can check how to setup such SSH tunnel -- it has examples fr=or Linux/Mac and Windows as well (you will have to adjust it for MySQL ports .. as those examples are for PHP debugging).

How to use Navicat to connect to the MySQL database in Openshift

I'm using openshift to build my apps.
And I add mysql to my gear.
but, if I want to reach my database. I can't use Navicat which is my usual way to manage my database. I must ssh to my openshift server and then use command line 'mysql' to reach my database which is a bad way compared to Navicat.
So, how can I reach my database in Openshift with Navicat?
I've used env | grep MYSQL to get my mysql configration and use it in Navicat.
However, all is none effect.
If its a scalable application you should be able to connect to it externally via the connection information supplied by the environment variables. If its not a scalable app, then you'll need to use the rhc port-forward command to forward the necessary ports needed to connect.
Take a look at the following article here for more information. https://www.openshift.com/blogs/getting-started-with-port-forwarding-on-openshift

How to config Yii framework to connect MySQL over SSH?

I have local MySql server and want Yii to connect to it over SSH. I have try to config config/main.php but failed. How can I do that?
You need to forward port 3306 from the remote server to your local machine and then set Yii to connect to the forwarded port on localhost.
As stated by hd1: This is something you need to do outside of PHP.
The best way is to create an SSH tunnel so that you have a port on the local server to talk to, that connects to your actual database server. There was a discussion on the Yii forum about exactly this, you can find it here
You might want to take a look at AutoSSH as well.

Pentaho Data Integration - MySQL Database connect with SSH

I'm trying to connect to a remote MySQL database with Pentaho Data Integration (JDBC Driver).
Therefore I need to build up a SSH connection.
Is it possible to do this inside/with the tool?
Guess I solved it:
I just had to build up a SSH Port Forwarding:
ssh -L 3306:localhost:3306 remote.url
There's no way to do this that I know of in PDI. But you wouldn't really want to. I would take the approach of configuring your MySQL client to use an encrypted connection, and then connect your JDBC driver to that.
Sorry, I don't do MySQL, so I don't know how to do that off the top of my head.
Brian