Connect PHP to database hosted within PhpStorm via SSH Tunnel - phpstorm

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).

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).

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

Securely connect MS Access database front-end to MySQL back-end on web host?

We have a fairly simple M$ Access db, split into front-end (forms, reports, etc.) and back-end (tables). Currently looking for a way to get the tables with all the critical data off of one desktop and hopefully into a MySQL database on our web host, and be able to connect to it from multiple PCs (still probably only one or two people connecting to it at any give time), and eventually, hopefully, migrate to a web application when time allows. Many of the examples I've read about people connecting an Access db front-end to a MySQL back-end seem to imply that they are doing so on a LAN, probably behind a firewall, etc.
Is it at all safe to connect a M$ Access front-end to a MySQL backend when that mysql server is running on a remote web host? Does the ODBC connector take care of encryption?
TIA,
Monte
You could use putty to mount a ssh tunnel to your mysql server and redirect the remote mysql port to your machine.
Using putty is pretty straightforward:
Give it your mysql server dns name as the host and go to "Connection/SSH/Tunnels", there you define the local port to connect in the "Source Port" field (e.g. 3307).
In the the "Destination" field put the dns name of your mysql server followed by a colon and the port mysqld is running in (e.g. mysql.example.org:3306).
Save this as a profile then connect and the remote mysql port will be availbable locally on port 3307.
Just make sure you restrict the user because by default he will have an ssh shell on the server.
Setting up key authentication would also be practical because you won't have to enter a password to connect to the server (but be sure to protect your key on disk by encrypting it).
EDIT: It seems the mysql odbc connector support ssl, you could use that too but I'd personnally choose to use SSH anyway as you will have it already on your mysql server.

How to use HeidiSQL (or similar MySQL managers) with hosted databases

I'm using HeidiSQL ( http://www.heidisql.com/ ) to manage my databases at work (I prefer this over phpMyAdmin).
Now, I want to use HeidiSQL with my personnal host plan, but when I connect to MySQL with my host, I connect with "localhost" in PHP but at work, I connect to databases with something like "supersqlserver.foo.net"...
Is there a way to connect to my hosted database with HeidiSQL? Is it common practice with hosts to allow the use of software like this?
BTW, the host where I want HeidiSQL to work is HostGator.
When you can't connect to the MySQL server directly, for example a firewall blocks the access from all network protocols, except HTTP protocol, you can use HTTP tunneling.
Here an example of connecting to MySQl through Httptunnel using HeidiSQL.
Ask your HostGator support. They should be able to help you. Maybe they only listen on localhost, and then you can't connect from the outside. It's rather common to not listen for external MySQL connections due to security policies, but some hosts allow it.

mysql proxy socks

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?