Laravel Homestead connect to remote database SSH - php

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.

Related

Connect to remote database from Laravel Homestead

I am trying to connect my Homestead Laravel project to a remote db hosted on Fasthosts but I get the error ;
ssh: connect to host 213.xxx.xxx.xx port 22: Connection timed out
I have tried this in my homestead
ssh -fNg -L 3333:localhost:3306 user#213.xxx.xxx.xx
This is my .env db settings
DB_CONNECTION=mysql
DB_HOST=213.xxx.xxx.xx
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=user
DB_PASSWORD=xxxxxxx
I am not sure why the connection to the remote database is timing out. Any ideas to help is muchly appreciated.
if your databse stack is bitnami there is a process to open port, and if your stack is on AWS and is bitnami stack you must open ports in the cloud platform of amazon

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.

DOCKER + PDO: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

When trying to connect a dockerized PHP-Application to a MySQL-Container, I get the following error: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Dockerfile:
FROM tutum/apache-php
RUN rm -fr /app
COPY . /app
ENV DB_HOST=192.168.99.100
ENV DB_NAME=azk
ENV DB_USER=root
ENV DB_PW=my-secret-pw
If I do not use PDO, the connection works, so there musst be something wrong with PDO, but I have no idea what...
PDO uses unix socket connection instead of tcp. see the answer how to force using tcp connection.
in your case probably you don't applied ENV DB_HOST to php application config file. (you can use getenv('DB_HOST') function)

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

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)

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