简体   繁体   English

MySQL通过SSH隧道访问被拒绝

[英]MySQL via SSH Tunnel Access Denied

I want to use SSH tunnel to connect to MySQL DB on remote host.我想使用 SSH 隧道连接到远程主机上的 MySQL 数据库。

I've set up the tunnel with command:我已经用命令设置了隧道:

ssh user@host -L 3307:remote_mysql_hostname:3306

I can successfull connect with HeidiSQL using this settings:我可以使用以下设置成功连接 HeidiSQL:

hostname: localhost
user: remote_mysql_user_login
password: remote_mysql_user_password
port: 3307

But when i use PDO in PHP to connect, i get:但是当我在 PHP 中使用 PDO 进行连接时,我得到:

Access denied for user 'remote_mysql_user_login'@'localhost' (using password: YES)用户 'remote_mysql_user_login'@'localhost' 访问被拒绝(使用密码:YES)

My PDO Dns is sth like this:我的 PDO Dns 是这样的:

mysql:type=Core_Db_Adapter_Pdo_Mysql;host=localhost;port=3307;dbname=db_name;

Where is the trick?诀窍在哪里?


Solution:解决方案:

@symcbean thanks! @symcbean谢谢!

The problem was (as suggested by @symcbean) in hostname.问题出在主机名中(如@symcbean 所建议的那样)。

Changing to '127.0.0.1' fix the problem.**更改为“127.0.0.1”可解决问题。**

I borrowed from your example and was able to connect to a remote host using your same SSH tunnel setup.我借鉴了您的示例,并且能够使用您相同的 SSH 隧道设置连接到远程主机。 Can you share the PHP script you are using to get some more info about your setup?您能否分享您正在使用的 PHP 脚本以获取有关您的设置的更多信息?

You may try a hostname other than 'localhost' for your remote database you are connecting to.您可以为要连接的远程数据库尝试使用除 'localhost' 以外的主机名。

setup ssh tunnel - I setup an entry in /etc/hosts for my_db_host_name instead of using localhost设置 ssh 隧道 - 我在 /etc/hosts 中为 my_db_host_name 设置了一个条目,而不是使用 localhost

ssh my_user@my_remote_host -L 3307:my_db_host_name:3306 ssh my_user@my_remote_host -L 3307:my_db_host_name:3306

<?php

$dsn = 'mysql:host=my_db_host_name;dbname=my_db;port=3307';
$username = 'my_user';
$password = 'my_pass';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
); 

$dbh = new PDO($dsn, $username, $password, $options);

$result = $dbh->query("select * from my_table");

try {
    foreach($result as $row) {
        echo "column1: " . $row['column1'];
    }
} catch (PDOException $pde) {
    echo "PDO exception: $pde";
}

echo "done \n";

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM