不能SSH隧道访问远程MySQL服务器

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

我做ssh隧道

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

然后在另一个terminal,我尝试

 [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如何将localhost vs 127.0.0.1作为通过套接字或TCP连接。 但是我开始对实际发生的事情感到困惑,或者socket与TCP甚至是问题。

在此先感谢,我打开任何提示和build议!

一些更多信息:

我的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) 

编辑:

我使用-vvv ssh隧道,禁用iptables ,现在当我尝试使用mysql -h 127.0.0.1 -P 3307 -u root --protocol=tcp -p ,我在linode服务器上得到以下debugging日志:

 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) 

我已经回答了这个问题,试试这个步骤,不要使用本地主机使用127.0.0.1的IP地址,而是通过SSH隧道连接到MYSQL

使用“localhost”名称默认情况下会尝试使用UNIX DOMAIN套接字进行连接,这仅在客户端和服务器位于同一台计算机上时才有可能,因为它们需要访问文件系统中的本地套接字条目。 (在UNIX / Linux的许多实现中,连接速度更快,build议使用它)。

127.0.0.1尝试使用Internetnetworking堆栈(AF_INET)进行连接,因此,增加了一点开销,但似乎在您的情况下是与另一个系统进行通信所必需的。

尝试连接使用

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