Connect to remote MySQL database with PHP using SSH - php

I have a remote database I'd like to connect to from PHP running locally. The database doesn't allow remote connections so ordinarily I SSH into the box and use it from the command line, but that's not really a long term solutions.
I have SSH access, I have MySQL access once I SSH in, but I don't know how to get PHP into that workflow. If I could make this work within MAMP, that would be great, too.

For developing or testing, you can use ssh command to setup tunnel first and then access the remote database as a local one. The steps are:
1) setup tunnel with ssh command. command format: ssh -L [local port]:127.0.0.1:[remote mysql port, by default, it is 3306] [user]#[remote mysql server ip]. sample:
ssh -L 3307:127.0.0.1:3306 ford#134.11.21.89
2) keep the command window alive
3) You can access the remote database by mysql string: mysqli://[user]:[password]#127.0.0.1:3307/[database name]

Connect to a MySQL server over SSH in PHP

You could set up a SSH tunnel and then point your php connection code to a local port which is forwarded through the tunnel. Under Windows you might use putty; for Mac there will be similar solutions.

If this is for development, the suggested solution by alex is the way to go; set up a ssh-tunnel.
The tunnel will redirect your 127.0.0.1:3306-requests to the remote machine. The remote machine will also belive the requests will come from 127.0.0.1 (locally).
However, you may encounter problems if your server (shared host? please specify) doesn't allow mysql-connections from 127.0.0.1 (quite commonly only localhost are allowed). There's a tiny difference in those, and it will inhibit your tunnel from reaching the remote mysqld.
Just google tunneling, set it up, and use 127.0.0.1 from your php-connection strings.
regards,
//t

Related

Laravel Create mysql SSH Tunnel on windows10 programmatically?

Trying to setup a local Windows10 environment for Laravel 6 and need to connect with SSH to a remote linux mysql server.
Standard configuration doesnt seem to support a way to connect with SSH tunnels, but I did find a workaround that I need help finishing.
It seems the only way to get the connection going is through port forwarding, which I am able to do with gitbash/windows terminal
ssh -i /c/Users/MyUserName/MyKeyFolder/sitemaster_ssh -fNg -L 3307:127.0.0.1:3306 linuxUser#remoteHost.com
After running that manually in terminal / gitbash, I am able to use localhost with port 3307 successfully for my database configuration in Laravel and visit my site locally
Ideally, I would like to programatically check this port before laravel tries to access port 3307 on startup as well as some type of detection for later calls and reopen only if necessary.
I used this article which lays out the basics but is unclear as to whether the command line code can only be run manually
or if can be run with a script somewhere, somehow.
Laravel MySql DB Connection with SSH
I have tried doing things like
exec('ssh -i /c/Users/MyUserName/MyKeyFolder/sitemaster_ssh -fNg -L 3307:127.0.0.1:3306 linuxUser#remoteHost.com');
as well as putting in its own script nameed port_fwd_3307.sh and then running
exec('port_fwd_3307.sh');
I can navigate to the sh file from terminal and confirm it runs and the script is calling it from the proper directory, but still, I get no connection and no feedback as to whats going on other than server actively refused (but I know its a port issue, since it resolves successfully when run manually).
I have placed that command in the bootstrap/app.php file, as well as tried it in the config/database.php and public/index.php
before laravel even starts and none of this works.
Not sure if this is possible at all, but if it is, what specific part of the framework would it need to be located in to ensure the port is forwarded prior to the database connection?

Connection Firebird DB via SSH

I am creating an application with Laravel and Firebird 2.5 and wants to run it on a DigitalOcean server structure. We have 2 servers for the application, one for the web services (lets call it www) and one for the database services. I did a successful setup with Mysql and it works well but we all know Firebird is a rough one. So this is what we did so far:
Install a LEMP stack in www and Firebird 2.5 SuperServer. As I said these are digital ocean servers, both runs Ubuntu 14.04.
We created an SSH tunnel between the two server with the following structure:
ssh -L 9500:127.0.0.1:3050 username#db_server_ip_address (Private address)
But when we tried to hook up the application on www with the database server got the following error:
unavailable database
In the Laravel Configuration file used the following set up:
DB_HOST=127.0.0.1
DB_DATABASE=/home/username/database.gdb
DB_USERNAME=username
DB_PASSWORD=password
The credentials are correct, we can use it on the remote (db) server.
What do you think what could be the problem? Is it the SSH tunneling?
Warning: I don't use SSH that much, and have never used SSH tunneling, my answer is based on looking at documentation.
The ssh -L 9500:127.0.0.1:3050 does not do what you think it does:
Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side. This works by allocating a socket to listen to either a TCP port on the local side, optionally bound to the specified bind_address, or to a Unix socket. Whenever a connection is made to the local port or socket, the connection is forwarded over the secure channel, and a connection is made to either host port hostport, or the Unix socket remote_socket, from the remote machine.
(from ssh(1))
In other words, as far as I can tell the proper command would be:
ssh -L 9500:<ip-address of the Firebird server>:3050
The second problem seems to be that your Laravel config does not specify a port, so it is likely still trying to connect to port 3050 (the Firebird default port), instead of port 9500 that you configure. I don't know Laravel, but a property DB_PORT=9500 seems logical (but maybe these properties are specific to your own deployment, in which case you may need to do some more work).

What are the behind the scenes MySQL Workbench Standard TCP/IP over SSH commands?

I am able to connect to our MySQL database using Standard TCP/IP over SSH within MySQL Workbench. My question is what are the command line commands that make this possible? I'm trying to write a PHP program that can connect to a remote database through an SSH tunnel but all of the websites I've searched and read assume that the server I'm tunneling to is also the database server. (like this one: http://chxo.com/be2/20040511_5667.html). This is different from my setup. My setup is basically client > server > database_server. If I could find out what the commands are being used in MySQL Workbench, I might be able to figure out how to code my app.
I have included a snapshot of the MySQL Workbench settings dialog that I've been successful working with:
I found a page that described perfectly what I needed to do:
https://support.cloud.engineyard.com/entries/21009887-Access-Your-Database-Remotely-Through-an-SSH-Tunnel
So basically the syntax for me was:
ssh -L 3389:myserver-production.wervyvvr9asdf.us-east-1.rds.amazonaws.com:3306 ec2-user#256.256.256.256 -i ~/.ssh/mykeyfile.pem
The connect string was:
mysql -u myusername -P 3389 -h 127.0.0.1 mydbname -p
I used port 3389 because its the windows remote desktop port which I'll never use and it was already open, so no need to bug our network admins to mess with firewall settings. Also where the IP is 256.256.256.256, you'll want to substitute a valid IP here.

Phpcloud and Mysql Workbench connection

I have been struggling how to connect Mysql Workbench with Phpcloud.com Mysql instance since friday ? Below there is error which I get image
Anyone has did it working properly ?
The error message about the initial package usually indicates there was no server found at that address and port. Keep in mind that due to the SSH tunnel the remote server appears like it is a local one. The remote end of the tunnel sees the server locally too, since it is on the same machine as the MySQL server (I assume). So what you need is the localhost address for the MySQL server. The other thing to check are the ports: 1) Does the SSH connection really work with port 22? 2) Is your firewall blocking this port? 3) Is the remote MySQL server really listening on port 3306?
As a side node: on Windows you can usually just press Ctrl+C in error messages to copy the message to the clipboard.
Please look at the second section "Managing your Database Instance" at http://www.phpcloud.com/help/accessing-the-db
You need to establish a tunnel using SSH client (putty will be best in your case) and then access the server on port 13306 (local).
I don't use Workbench but I assume it tries to establish a regular SSH connection and not a tunnel and this will not work.

Querying a MySQL database on a remote webserver through SSH

We have an internal MySQL database that our customer service department uses to do quotes, and an external MySQL database on our website that our customers use to do quotes. I have a PHP report that I've written that gives me the information from the internal database. Now I'd like to modify the report to include the data from the external database as well. I need to connect to the external database by using a SSH connection. I've been looking into using cURL or SSH2, but I'm not sure if either are the right way to do this. Has anyone out there queried a SSH MySQL database on a webserver before, or does anyone know what I'd need to do to make this work? Thanks for any help you can give!
I actually use a MySQL GUI client called "Sequel Pro", which does offer the option to connect to MySQL through a SSH connection. This is totally doable, though if you have the option to connect to the DB without it (e.g. you can connect to it from "outside" the host), you should prefer this.
the easiest way I conceive you could query your DB through SSH is to use SSH port-forwarding, so you'd spawn a process that would do ssh user#host-that-has-the-db -L3306:localhost:3306, and then you'd have your MySQL connector connect on localhost.
The -L argument to SSH instructs it to do local port forwarding, so SSH will listen on a local port and forward what it receives there directly to the specified remote host/port from the other end. obviously my example uses port 3306 on both ends, but that can be changed as appropriate (RTM to have more about this)
Yes you can query mysql from bash over SSH but this is not the best and scalable way to go. The best solution would be to create a REST Web Service on the production servers. Then your internal system will query that remote service with authentication.
You set up a tunnel from some available port on the local server to the mysql port on the remote server:
ssh -L 3307:localhost:3306 someuser#remoteserver
That creates a tunnel listening on port 3307 on the local system, which connects to port 3306 (MySQL) on the remoteserver.
Then point your PHP code to connect to port 3307 instead of 3306 , and it'll get tunneled to mysql on the remoteserver.

Categories