0

I keep getting "(2003, "Can't connect to MySQL server on '127.0.0.1' (61)")."

I tried every possible solution I could find online. Could some help me solve this error?

What I want to do: access "MySQL on Linux - AWS EC2" from "my MAC - Sierra" using python via SSH tunnel
Confirmed Linux - AWS EC2 is accessible via ssh on port 22 from my MAC:

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

Confirmed MySQL port is 3306:

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

Confirmed port 3306 is listening:

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

Confirmed bind-address is not written in "/etc/my.cnf"

Confirmed mysql is accessible on 3306 on Linux - AWS EC2:

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

Confirmed root user has the privilege to access MySQL remortely:

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)

Confirmed iptables is not running:

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

Python script which does not work on my 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()

The result of running the Python script:

_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (61)")
M. Thomas
  • 61
  • 1
  • 7
  • Welcome to Stackoverflow. It would be better if you checkout [How to Ask](http://stackoverflow.com/help/how-to-ask) page for future endeavor at Stack overflow.Great question tend to provide quicker, better answers from the community -Thank you – Momin Jan 06 '18 at 04:07
  • Thank you. I added the following to SSHTunnelForwarder() local_bind_address = ('0.0.0.0', 3306), and it worked. – M. Thomas Jan 06 '18 at 04:11

0 Answers0