尝试了我可以在线研究的所有内容:(2003年,"无法连接MySQL服务器' 127.0.0.1'(61)")

时间:2018-01-06 03:34:51

标签: python mysql linux amazon-ec2 ssh

我一直在"(2003年,"无法连接到MySQL服务器' 127.0.0.1'(61)")。&#34 ;

我尝试了我能在网上找到的所有可能的解决方案。有人可以帮我解决这个错误吗?

我想做什么:访问" Linux上的MySQL - AWS EC2"来自"我的MAC - Sierra"通过SSH隧道使用python
已确认的Linux - 可通过我的MAC端口22上的ssh访问AWS EC2

$ ssh -i "pkey.pem" ec2-user@Linux.AWS.EC2.IPAddress -p 22

确认的MySQL端口为3306

mysql> show variables like "port";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

已确认端口3306正在侦听

$ netstat -ant | grep 3306 
tcp        0      0 :::3306                     :::*                        LISTEN

确认的绑定地址未写入" /etc/my.cnf"

确认的mysql可在Linux上的3306上访问 - AWS EC2

$ mysql -u root -p -h 127.0.0.1 -P 3306

确认的root用户有权重新访问MySQL

mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | % |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+------+-----------+
4 rows in set (0.00 sec)

确认的iptables未运行

$ sudo /etc/rc.d/init.d/iptables status

Python脚本无法在我的MAC上运行

# -*- coding: utf-8 -*-
from sshtunnel import SSHTunnelForwarder
from MySQLdb import Connect

with SSHTunnelForwarder(
                        ('Linux.AWS.EC2.IPAddress', 22),
                        ssh_host_key=None,
                        ssh_pkey="/Users/thomas.m/pkey.pem",
                        ssh_username="ec2-user",
                        ssh_password=None,
                        remote_bind_address=('127.0.0.1', 3306)
                        ) as server:

    conn = Connect(
                   user='root', passwd='nhk', host='127.0.0.1', port=3306, db='mydb', charset="utf8")
    curs = conn.cursor()
    try:

        query = ("select distinct category from mytable;")

        curs.execute(query)
        for row in curs.fetchall():
            print(row)
    except:
        pass
    conn.commit()

运行Python脚本的结果

_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (61)")

0 个答案:

没有答案