Laravel Create mysql SSH Tunnel on windows10 programmatically? - php

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?

Related

Remote MySQL not working with Laravel Config

Our database server has grant access to all local ip's with user root and it works well to access them via cli in all local IP. But in our webserver (different machine), Laravel still can't connected to the database, here's the error:
I've tried to clear cache, reinstall the Laravel (install composer, generate new key etc.) change db config drive to sqli, it still error connecting the remote db, but why the DB works when I querying the DB via tinker on that machine, this is so unusual.
If your MySQL database is on a remote server it may well be one of the following issues:
Firewall Block, the server with the MySQL service may be behind a firewall that is set to block external access to the port that MySQL operates on.
MySQL User Permissions, if the MySQL service is not sat behind a firewall then the next cause may be that the user has only localhost access permissions.
You should try to log in to your remote server and, from there, connect to your database via some shell command to verify you actually can.
Security-Enhanced Linux (SELinux) is a Linux kernel security module that provides a mechanism for supporting access control security policies, including United States Department of Defense–style mandatory access controls (MAC).
httpd_can_network_connect (HTTPD Service):: Allow HTTPD scripts and modules to connect to the network.
Run as root:
# setsebool -P httpd_can_network_connect 1
Solved: php can't connect to mysql with error 13 (but command line can)
Now Laravel can connect the DB, By executing: setsebool httpd_can_network_connect=1 in Database Server. But I still don't understand why. Is there any reasonable explanation?

Start PHPmyAdmin from putty - Server is remote

Bit stuck here, I have looked on here, for similar, but cant find what I need.
Have looked at how to start PHPmyAdmin via putty on here.
I have a server, which is on a network. I log into the network via Putty.
The database usual access is for example 10.32.187.100/m . This is how I used to access it before the server reboot.
I have had to restart the server. But I cannot log into the database as I believe I have to turn it on.
As it is not on a local machine I do not know how. It is a linux server, and the location of ‘m’ is /var/www/html
In ‘m’ are the files for phpmyAdmin.
Nothing has been over written. The server had to restart. Hence why I need to start up PHPmyAdmin.
Can I ask how do I turn on PHPmyAdmin via putty I the method I need to use?
try this
sudo service mysql start
sudo service apache2 start

Unable to access MySql db

I have been working on php for some time. I have cloned a git repo yesterday. Just after that i am able to connect to MySQL server, access MySQL db and phpmyadmin properly. Today, I started the server and tried to access phpmyadmin from xampp control panel, as soon as I try to open it, apache port is changing automatically to 49868 and mysql connection terminates.Then, again apache changes its ports to previous states.
Any help is appreciated. Thanks in advance.
EDIT : I reinstalled everything and it worked.
Check if another application is using port 80.
In command prompt use:
netstat -aon | findstr :80
enter image description here
If windows verify if in task manager is already running XAMPP.

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.

Connect to remote MySQL database with PHP using SSH

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

Categories