intrepidws / laravel-ssh-tunnel-fixed
轻松创建和维护Laravel/Lumen的SSH隧道
Requires
- illuminate/support: ^5.2
README
通过SSH隧道访问远程主机上的服务!例如,人们已经多年在询问如何在PHP中通过SSH连接到MySQL服务器。
- 通过SSH在PHP中连接到MySQL服务器
- 通过SSH在PHP中连接到MySQL服务器
- 通过PHP通过SSH连接到mysql数据库
- 使用SSH通过PHP连接到远程MySQL数据库
- Laravel MySql DB连接使用SSH
我们遇到了类似的挑战,特别是访问通过SSH隧道连接的MySQL数据库,所有的问题和答案都在寻找解决方案中很有帮助。然而,我们想要的是可以即插即用与我们的Laravel应用程序和Lumen服务。
因此,我们编写了这个包。希望你们喜欢它!
要求
此包已针对Laravel/Lumen版本5.2+进行了测试
我们不支持版本 <=5.1。
安装
composer require intrepidws/laravel-ssh-tunnel-fixed
注册Provider
对于Lumen服务,添加
$app->register(IntrepidWS\Tunneler\TunnelerServiceProvider::class);
到 bootstrap/app.php。对于Laravel应用程序,添加
IntrepidWS\Tunneler\TunnelerServiceProvider::class,
到config/app.php中的providers数组。
配置
所有配置都可以并在你的.env文件中完成。
; Process used to verify connection ; Use bash if your distro uses nmap-ncat (RHEL/CentOS 7.x) TUNNELER_VERIFY_PROCESS=nc ; Path to the nc executable TUNNELER_NC_PATH=/usr/bin/nc ; Path to the bash executable TUNNELER_BASH_PATH=/usr/bin/bash ; Path to the ssh executable TUNNELER_SSH_PATH=/usr/bin/ssh ; Path to the nohup executable TUNNELER_NOHUP_PATH=/usr/bin/nohup ; Log messages for troubleshooting SSH_VERBOSITY= NOHUP_LOG=/dev/null ; The identity file you want to use for ssh auth TUNNELER_IDENTITY_FILE=/home/user/.ssh/id_rsa ; The local address and port for the tunnel TUNNELER_LOCAL_PORT=13306 TUNNELER_LOCAL_ADDRESS=127.0.0.1 ; The remote address and port for the tunnel TUNNELER_BIND_PORT=3306 TUNNELER_BIND_ADDRESS=127.0.0.1 ; The ssh connection: sshuser@sshhost:sshport TUNNELER_USER=sshuser TUNNELER_HOSTNAME=sshhost TUNNELER_PORT=sshport ; How long to wait, in microseconds, before testing to see if the tunnel is created. ; Depending on your network speeds you will want to modify the default of .5 seconds TUNNELER_CONN_WAIT=500000 ; Do you want to ensure you have the Tunnel in place for each bootstrap of the framework? TUNNELER_ON_BOOT=false ; Do you want to use additional SSH options when the tunnel is created? TUNNELER_SSH_OPTIONS="-o StrictHostKeyChecking=no"
快速入门
使用Tunneler的最简单方法是设置TUNNELER_ON_BOOT=true在你的.env文件中。这将确保每次框架引导时隧道都在位。
但是,由于隧道将被重用,因此会有最小性能影响。你只有在隧道因某种原因断开连接时才需要承担连接成本。
然后你可以配置你的服务,我们将使用数据库连接进行演示。在你的config/database.php文件中的'connections'下添加以下内容
'mysql_tunnel' => [ 'driver' => 'mysql', 'host' => env('TUNNELER_LOCAL_ADDRESS'), 'port' => env('TUNNELER_LOCAL_PORT'), 'database' => env('DB_DATABASE'), 'username' => env('DB_USERNAME'), 'password' => env('DB_PASSWORD'), 'charset' => env('DB_CHARSET', 'utf8'), 'collation' => env('DB_COLLATION', 'utf8_unicode_ci'), 'prefix' => env('DB_PREFIX', ''), 'timezone' => env('DB_TIMEZONE', '+00:00'), 'strict' => env('DB_STRICT_MODE', false), ],
就这样。现在去设置你的Eloquent模型。
Artisan命令
php artisan tunneler:activate
此Artisan命令将验证连接是否已建立,或创建连接。这可能对手动运行没有太大帮助,除了测试你的配置。
但是,如果你想确保隧道始终可用,而不是在引导时做这项工作,你可以使用Laravel Scheduler来安排Artisan命令在你认为最佳的时间间隔运行,以维护连接。例如,在你的App\Console\Kernel中
protected function schedule(Schedule $schedule) { $schedule->command('tunneler:activate')->everyFiveMinutes(); }
然后,假设你已经正确设置了cron中的调度器,Artisan命令将每五分钟检查一次隧道,如果隧道未建立,则重新启动它。
调度它
也许你的应用程序很少需要这样做,但在需要时,你希望有一个简单的方法来确保在尝试连接之前隧道已经就绪。
$app->get('/mysql_tunnel', function () use ($app) { dispatch(new STS\Tunneler\Jobs\CreateTunnel()); $users = DB::connection('mysql_tunnel') ->table('users') ->get(); dd($users); });
它如何工作?
它首先使用exec通过netcat(nc)检查本地端口,看看隧道是否打开。如果端口存在,它就不再做其他任何事情。
如果端口不存在,它将创建SSH隧道连接命令,并通过exec执行该命令。执行后,我们等待定义的TUNNELER_CONN_WAIT时间,然后再运行netcat以验证连接是否就绪。
就这样。隧道将持续到超时,如果超时,根据你选择的确保它在需要时始终可用和可用的策略,它将简单地按需重新创建。