无法通过 ssh 隧道访问远程 mysql 服务器

无法通过 ssh 隧道访问远程 mysql 服务器

我似乎无法弄清楚为什么我不能使用 ssh 隧道连接到我的远程 MySQL 服务器。

我使用 ssh 隧道

[hobbes3@hobbes3] ~ $ ssh linode -L 3307:localhost:3306

然后在另一个终端上,我尝试

[hobbes3@hobbes3] ~ $ mysql -h localhost -P 3307 -u root --protocol=tcp -p
Enter password:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2

在服务器上,显示如下内容:

root@li534-120 ~ # channel 4: open failed: connect failed: Connection refused

这是我的my.cnf服务器:

[mysqld]
# Settings user and group are ignored when systemd is used (fedora >= 15).
# If you need to run mysqld under different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd
user=mysql

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Semisynchronous Replication
# http://dev.mysql.com/doc/refman/5.5/en/replication-semisync.html
# uncomment next line on MASTER
;plugin-load=rpl_semi_sync_master=semisync_master.so
# uncomment next line on SLAVE
;plugin-load=rpl_semi_sync_slave=semisync_slave.so

# Others options for Semisynchronous Replication
;rpl_semi_sync_master_enabled=1
;rpl_semi_sync_master_timeout=10
;rpl_semi_sync_slave_enabled=1

# http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html
;performance_schema


[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[mysqld]
port = 3306
socket=/var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size = 64M
max_allowed_packet = 128M
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache = 8
max_connections = 25
query_cache_size = 16M
table_open_cache = 1024
table_definition_cache = 1024
tmp_table_size = 32M
max_heap_table_size = 32M
bind-address = 0.0.0.0

现在确定这是否有帮助,但这里是 MySQL 用户列表:

mysql> select * from mysql.user;
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
| Host      | User | Password                                  | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string |
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
| localhost | root | *664328D3C5E263F4FB25185681AAE7E92B01B2B0 | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 |        |                       |
| 127.0.0.1 | root | *664328D3C5E263F4FB25185681AAE7E92B01B2B0 | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 |        |                       |
| ::1       | root | *664328D3C5E263F4FB25185681AAE7E92B01B2B0 | Y           | Y           | Y           | Y           | Y           | Y         | Y           | Y             | Y            | Y         | Y          | Y               | Y          | Y          | Y            | Y          | Y                     | Y                | Y            | Y               | Y                | Y                | Y              | Y                   | Y                  | Y                | Y          | Y            | Y                      |          |            |             |              |             0 |           0 |               0 |                    0 |        |                       |
+-----------+------+-------------------------------------------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+--------+-----------------------+
3 rows in set (0.00 sec)

我读到过 MySQL 如何分别将localhostvs127.0.0.1视为通过套接字或 TCP 进行连接。但我开始搞不清楚到底发生了什么,或者套接字与 TCP 是否是问题所在。

提前致谢,我愿意接受任何提示和建议!

更多信息:

我的 MySQL 客户端运行的是 OS X 10.8.4,

mysql  Ver 14.14 Distrib 5.6.10, for osx10.8 (x86_64) using  EditLine wrapper

我的 MySQL 服务器运行在 CentOS 6.4 32 位上,

mysql> SHOW VARIABLES LIKE "%version%";
+-------------------------+--------------------------------------+
| Variable_name           | Value                                |
+-------------------------+--------------------------------------+
| innodb_version          | 1.1.8                                |
| protocol_version        | 10                                   |
| slave_type_conversions  |                                      |
| version                 | 5.5.28                               |
| version_comment         | MySQL Community Server (GPL) by Remi |
| version_compile_machine | i686                                 |
| version_compile_os      | Linux                                |
+-------------------------+--------------------------------------+
7 rows in set (0.00 sec)

编辑:

我重新创建了 ssh 隧道-vvv,并禁用了iptables,现在当我尝试时mysql -h 127.0.0.1 -P 3307 -u root --protocol=tcp -p,我在 linode 服务器上收到以下调试日志:

debug1: Connection to port 3307 forwarding to localhost port 3306 requested.
debug2: fd 11 setting TCP_NODELAY
debug3: fd 11 is O_NONBLOCK
debug3: fd 11 is O_NONBLOCK
debug1: channel 4: new [direct-tcpip]
channel 4: open failed: connect failed: Connection refused
debug2: channel 4: zombie
debug2: channel 4: garbage collecting
debug1: channel 4: free: direct-tcpip: listening port 3307 for localhost port 3306, connect from 127.0.0.1 port 49995, nchannels 5
debug3: channel 4: status: The following connections are open:
#3 client-session (t4 r0 i0/0 o0/0 fd 8/9 cc -1)

答案1

我已经回答了这个问题,请尝试此步骤,不要使用 localhost,而是使用 127.0.0.1 ip 地址 通过 SSH 隧道连接到 MYSQL

答案2

使用名称“localhost”尝试使用UNIX 域默认情况下使用套接字,这仅当客户端和服务器位于同一台机器上时才有可能,因为它们需要访问文件系统中的本地套接字条目。(在许多 UNIX/Linux 实现中,这是更快的连接方式,并且是推荐的)。

127.0.0.1 尝试使用 Internet 网络堆栈(AF_INET)进行连接,因此会增加一些开销,但在您的情况下,这似乎对于与另一个系统通信是必要的。

尝试使用

mysql -h 127.0.0.1 -P 3307 -u root --protocol=tcp -p

相关内容