I am running OSX 10.6.7 and trying to connect to a remote MySQL server via ssh to run some PHP scripts. Currently, I run the following commands with no problem:

我正在运行osx10.6.7,并试图通过ssh连接到远程MySQL服务器,以运行一些PHP脚本。目前,我运行以下命令没有问题:

ssh -i /Users/xxxx/key.pem user@data.server.com -L 53306:localhost:3306 -f sleep 60 >> logfile
mysql -u user -p -h 127.0.0.1 -P 53306

After I authenticate with a password, it works perfectly. (So long as it's before the sleep timeout, of course.)

在我使用密码进行身份验证后,它工作得非常好。(当然,前提是在睡眠暂停之前。)

When I run this PHP script, however...

然而,当我运行这个PHP脚本时…

$shell = shell_exec("ssh -i /Users/xxxx/key1.pem user@data.server.com -L 53306:localhost:3306 -f sleep 60 >> logfile");

$mysqli = mysqli_connect('127.0.0.1', 'user', 'password', 'database', 53306);

I get connection refused. When I omit the shell_exec(), I get the same error. When I echo $shell, nothing is outputted.

我拒绝连接。当我省略shell_exec()时,会得到相同的错误。当我回复$shell时,什么都没有输出。

key1.pem has chmod 400 permission, is a copy of the key.pem, and I ran chown _www on it. I get the same errors if I try to use key.pem or key1.pem. I also tried 440 permissions as well with no luck.

key1。pem有chmod 400权限,是密钥的副本。是的,我在上面写了一篇文章。如果我尝试使用键,我也会得到相同的错误。pem或key1.pem。我也尝试了440权限,但运气不好。

Thanks for the help; this is driving me nuts.

谢谢你的帮助;这真让我发疯。

1 个解决方案

#1


3

Why do you need a password if you're using a private key? Do you mean a password for the key file? I think it could be the problem because ssh accept authentication using passwords only in tty and php *exec functions hasn't it.

如果使用私钥,为什么需要密码?你是说密钥文件的密码吗?我认为这可能是个问题,因为ssh只接受tty中的密码身份验证,而php *exec函数没有。

For the sleep question you can try the -N option of ssh, which is useful for tunnels and it doesn't require a remote command to be executed. Try to establish a tunnel using the shell and then simply connect to that tunnel with php.

对于睡眠问题,您可以尝试ssh的-N选项,它对于隧道很有用,并且不需要执行远程命令。尝试使用shell建立一个隧道,然后用php简单地连接到那个隧道。

Update:

更新:

I tried this and it does work on my Mac

我试过这个,它在我的Mac电脑上运行

<?
exec('ssh -f -N -n -L 5500:localhost:3306 your-host-name &> /dev/null');
var_dump(mysql_connect('127.0.0.1:5500', 'root'));

you should add your -i option and replace your hostname.

您应该添加-i选项并替换您的主机名。

This is the output on my computer

这是我电脑上的输出

resource(9) of type (mysql link)

But keep in mind that you'll have a couple of problems with this. The &> /dev/null statement makes the exec function return immediately, but it will suppress any output (mostly errors) when you execute the script multiple times, because every new request will try to open a new connection, it will succeed but it cannot setup the tunnel because port 5500 is already in use. After a while you'll have a lot of ssh process pending on your machine.

但是记住,你会遇到一些问题。& > / dev / null声明使exec函数立即返回,但它会抑制任何输出(主要是错误)当你多次执行该脚本,因为每一个新的请求将试图打开一个新连接,它会成功但不能设置隧道因为5500端口已经被使用。过一会儿,您的机器上将会有大量的ssh进程。

To avoid this you should try to connect to mysql before the ssh call and if you can't then setup the tunnel and redo the mysql connect call. In this way you can solve the problem.

为了避免这种情况,您应该在ssh调用之前尝试连接到mysql,如果不能,则设置隧道并重新执行mysql连接调用。用这种方法你可以解决这个问题。

更多相关文章

  1. PHP万能密码登陆
  2. 从mysql中选择加密密码
  3. Zend数据库适配器-未捕获异常-堆栈跟踪显示用户名和密码
  4. Windows下 MySQL更新密码
  5. linux下mysql的root密码忘记,怎么改root密码
  6. Linux下修改MySQL初始密码、开启远程登录、授权远程登录用户
  7. mysql获取某个范围内的随机数,写了个存储过程自动生成随机6为密码
  8. 使用随机盐改进密码哈希
  9. 阿里云服务器忘记mysql的登录密码时候如何修改密码

随机推荐

  1. 15个Android很有用的代码片段
  2. Android Intent Service Usage
  3. Android tp的虚拟按键(virtual key)处理
  4. Android Debug Bridge命令介绍
  5. android源码编译出现No private recovery
  6. Android系统升级的完整过程
  7. Android中使用log4j
  8. add study URL
  9. Simulate android behaviors on win32
  10. Android录音下————AudioRecord源码分