php mysql_connect(): How to connect to MySQL server through SSH tunnel - php

My MySQL server is being hosted somewhere outside and requires an ssh tunnel in order to reach it; how do I connect to this server through PHP? Has anyone experienced same problem before?

I haven't but googling I found the following example:
<?php
shell_exec("ssh -f -L 3307:127.0.0.1:3306 user#remote.rjmetrics.com sleep 60 >> logfile");
$db = mysql_connect('127.0.0.1', 'sqluser', 'sqlpassword', 'rjmadmin', 3307);
?>
It seems you first need to setup a ssh tunel (timeout of 60s). This works at OS level (shell_exec);
In this example, your creating a SSH tunnel redirecting the 3306 port on the mysql host server to the port 3307 locally (webserver);
You then connect (mysql_connect) as the mysql server were in your own webserver, redirect to the 3307 port.
I could't validate this solution, but I hope this helps you a step forward.
(Source)

Related

Laravel Homestead connect to remote database SSH

I'm stuck how to connect to remote database.
I've already tried to run
ssh -fNg -L 3307:127.0.0.1:3306 forge#128.xxx.xxx.xxx
and my .env
DB_CONNECTION=mysql
DB_HOST=128.xxx.xxx.xxx
DB_PORT=3307
DB_DATABASE=projectdb
DB_USERNAME=forge
DB_PASSWORD=password;
I can connect the remote database using HeidiSql. But when I tried to connect using laravel project it always return me
SQLSTATE[HY000] [2002] Connection timed out
Any other solution?
In that scenario, your .env file should have setting DB_HOST=127.0.0.1 since you're tunneling (local) traffic from your Homestead server on port 3307 to traffic into the remote server on port 3306, specifically also on 127.0.0.1.
Another way to write that more explicitly would be:
ssh -fNg -L 127.0.0.1:3307:127.0.0.1:3306 forge#128.xxx.xxx.xxx
(However, you can keep your current command of ssh -fNg -L 3307:127.0.0.1:3306 forge#128.xxx.xxx.xxx, I'm just showing you how you can write that another way to hopefully make the point more clear).
So, from Homestead, if you send traffic to 127.0.0.1:3307,it will get tunneled into the remote server and then connect to 127.0.0.1:3306 within it.

Centralised Mysql DB

I need solution for Centralized DB.
We have DB in one Hosting server (Azure Redhat virtual server) and we need to use the same db in other hosting server too. So it will be centralized DB for both server. I can not use it in navicat also if I use IP of Mysql DB.
I have tried using ( bind-address= IP address of the server where db is hosted ) in My.cnf file but it doesn't allow remote access. to use it in other hosting server nor in any mysql client like navicat / Heidi sql.
Also I have added the port 3306 rule in azure server for inbound security.
Thanks in advance for help.
As Azure VMs disable ICMP and we can use SSH tunnels to allow outside access to internal network resources.
Please try to follow this post, created a SSH tunnel in VM which hosted the MySQL server:
Open port 3306, so a remote client can connect to your MySQL Server. Run the following command to open TCP port 3306
iptables -A INPUT -i eth0 -p tcp -m tcp --dport 3306 -j ACCEPT
Now let’s check if the port 3306 is open by running this command:
sudo netstat -anltp|grep :3306
Create a SSH tunnel for port 3307
sudo ssh -fNg -L 3307:127.0.0.1:3306 azurevmuser#servername
Create an endpoint for the port 3307 rule in azure server for inbound security. Now your Database host name is <VM_ip>:3307
Any further concern, please feel free to let me know.

Telnet connection on localhost Refused

EDIT:
I want to telnet into my web server on localhost, and request my php file from command line:
I have:
1) cd'd into the directory I want to serve, namely "/www" (hello.php is here)
2) run a server at directory www: python -m SimpleHTTPServer
3) telnet localhost 80
but "connection is refused". what am I doing wrong?
You're probably trying to connect to a wrong port. Check with netstat -lntp which port is your http server listening on. The process will be listed as python/pid_number.
If you're using Linux then try this:
$ telnet localhost 80
GET /hello.php

Can't connect Websocket when using vagrant envirionment

I'm using Vagrant box using puphpet, the environment is PHP 5.5 + ubuntu 12.04 + apache + mysql. My Vagrant VM ip: 192.168.11.11, local machine hosts points to 192.168.11.11 reactphp.dev, and it works.
And, I'm using this code: https://github.com/muuknl/phprealtimechat to test websocket.
Then I visit reactphp.dev in my Chrome browser, and I start server script using: php bin/server.php, and after I type in the username I just got the error:
WebSocket connection to 'ws://192.168.11.11:2000/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
It works well when I changed the websocket ip to `ws://127.0.0.1:2000/', but why it does not work if I change to my VM's ip address?
It's been a while I haven't used PuPHPet, but when I had troubles connecting through the port (minus port 22), I've had to issue the command sudo ufw disable in the vagrant machine.
If uwf isn't the managing firewall of your vm, try sudo service iptables stop, or sudo iptables -F.
Perhaps they've changed the behavior since then, but it's still good to try.
The SSH tunnel (over the opened port 22) might allow you to access the local port 2000, which is proxified over. The actual port 2000 of the VM might be limited due to firewall.
Try it, and tell me if this works. If not, I'll dig in deeper.

Accessing mysql dbase with MysqlWorkBench on VirtualBox (Vagrant - ZF - BoilerPLate)

Does anyone know hot to connect to VB MySQL via MysqlWorkBench. I can log in in virtual box via terminal, but I can not connect it throught MySqlWorkBench. Also I can ping google on vb, ifconfig gave me 10.0.2.15 address, and when I enter it in MysqlWorkBench I got error:
Failed to Connect to MySQL at 127.0.0.1:3306 through SSH tunnel at 10.0.2.15:2222 with user davs
My Virtual box use port 2222 for connecting. Also when I try to connect with ip 127.0.0.1
Failed to Connect to MySQL at 127.0.0.1:3306 through SSH tunnel at 127.0.0.1:2222 with user davs Failed to Connect to MySQL at 127.0.0.1:3306 through SSH tunnel at 127.0.0.1:2222 with user davs
Any advice will be helpfull, thanks.
This looks like an old question but since I just ran into the problem, here are the settings that I used on MysqlWorkbench to get everything working on my mac:
Connection Method: Standard TCP/IP over SSH
SSH Hostname: 127.0.0.1:2222
SSH Key File: /Users//.vagrant.d/insecure_private_key
Mysql Hostname: 127.0.0.1
Mysql Server Port: 3306
Username: root
Password: vagrant
Your settings may be slightly different but the key part is using the insecure private key as was mentioned earlier.
Use the 'insecure private key' located at: ~/.vagrant.d/insecure_private_key

Categories