本文介绍了使用 phpseclib 库连接到 mysql 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 phpscelib 库成功连接到我的 VPS.现在我想连接到我现有的数据库.请帮我解决这个问题?

I have successfully connect to the My VPS using phpscelib library.Now i want to connect to my existing database.Please help me for this ?

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

include('Net/SSH2.php');


$ssh = new Net_SSH2('192.ccc.ccc.ccc');
if (!$ssh->login('ccc', 'cccc')) {
exit('Login Failed');
}

echo $ssh->exec("I need to put MySql commands here");
?>

推荐答案

首先,允许该用户远程访问 mysql 不是更好吗?但是,我不知道您的原因.

First, wouldn't it be better to allow remote access for that user to mysql? However, I don't know your reasons.

最常见的透明方式是创建 ssh 隧道.这可以通过两种不同的方式完成.如果 mysql 端口 (3306) 未在 mysql 机器上打开,您将需要一个反向 ssh 隧道,该隧道必须由远程机器打开.登录mysql机器并发出以下命令:

The most common an transparent way would be create a ssh tunnel. This can be done in two different ways. If the mysql port (3306) isn't open on the mysql machine, you'll need a reverse ssh tunnel which has to be opened by the remote machine. Log into the mysql machine and issue the following command:

ssh -R 12345:localhost:3306 user@php_machine -N

如果远程机器上的mysql端口是打开的,那么php机器就可以打开隧道:

If the mysql port is open on the remote machine then the tunnel can be opened by the php machine:

ssh -f user@mysql_machine -L 12345:mysql_machine:3306 -N

无论隧道的创建方式如何,PHP 应用程序现在都可以只使用 PDO 并连接到本地主机端口 12345.

Regardless of the way the tunnels has been created, the PHP application can now just use PDO and connect to localhost port 12345.

$pdo = new PDO('mysql:host=localhost;port=12345;dbname=test', $user, $password);

所有流量都将通过隧道加密.

All traffic will get crypted through the tunnel.

如果您只想发出几个简单的命令,您可以使用以下替代方法.

If you just want to issue a couple of simple commands you might use the following alternative.

最简单但不安全的方法是使用以下命令:

The simplest but unsecure way would be to use the following command:

echo $ssh->exec('mysql -uUSER -pPASSWORD DATABASE -e "SQL COMMAND"');

这是不安全的,因为系统上的其他用户可以看到密码.

This is insecure because other users on the system could see the password.

您可以使用 expect 解决安全问题.expect 是一个可以更安全地将密码传递给mysql的程序.确保 expect 安装在远程系统上.下面是一个在数据库 test 上使用 SHOW TABLES 命令的例子:

You can workaround the security issue using expect. expect is a program which can pass the password to mysql in a more secure way. Make sure that expect is installed on the remote system. Here comes an example using the SHOW TABLES command on database test:

include('Net/SSH2.php');

$ssh = new Net_SSH2('192.xxx.xxx.xxx');
if (!$ssh->login('ssh_user', 'ssh_password')) {
exit('Login Failed');
}

echo $ssh->exec('expect <<EOF
# disable command output
log_user 0
# start the mysqldump process
spawn mysql -uTHE_USER -p test -e "SHOW TABLES"
# wait for the password prompt
expect "password:"
# send the password (mind the \r)
send "THE_PASSWORD\r"
# enable output again
log_user 1
# echo all outout until the end
expect eof
EOF
');

为了进一步了解发生了什么,我最近写了一个我的博客文章 与此相关.

To further understand what's going, I've recently wrote a my blog article about that.

这篇关于使用 phpseclib 库连接到 mysql 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:33