title: Can't connect to MySQL server on 'localhost:3306' even though mysqld is running on port 3306
body: <p>Trying to connect to mysql through python using mysql-connector, but I am getting this error:</p>
  
  <pre><code> Traceback (most recent call last):
   File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/network.py", line 509, in open_connection
      self.sock.connect(sockaddr)
  ConnectionRefusedError: [Errno 111] Connection refused
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "./get_stf_file_names.py", line 26, in update_aws_script_details
      database="service_portal"
    File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/__init__.py", line 179, in connect
      return MySQLConnection(*args, **kwargs)
    File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/connection.py", line 95, in __init__
      self.connect(**kwargs)
    File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/abstracts.py", line 716, in connect
      self._open_connection()
    File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/connection.py", line 206, in _open_connection
      self._socket.open_connection()
    File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/network.py", line 512, in open_connection
      errno=2003, values=(self.get_address(), _strioerror(err)))
  mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)
  </code></pre>
  
  <p>I have checked if port 3306 is being used by mysqld and it is:</p>
  
  <pre><code>netstat -lnp | grep 3306
  </code></pre>
  
  <p>which gave:</p>
  
  <pre><code>tcp        0      0 my-ip:3306      0.0.0.0:*               LISTEN      12003/mysqld
  </code></pre>
  
  <p>I also tried this command and it works perfectly:</p>
  
  <pre><code>mysql -h localhost -u root -p
  </code></pre>
  
  <p>(As in I can connect to the database using this)</p>
  
  <p>I am trying to access the database in the following way:</p>
  
  <pre><code>mydb = mysql.connector.connect(host="localhost",user="****",
      passwd="****",
      database="**********"
      )
      cursor = mydb.cursor(dictionary=True)
  </code></pre>
  
  <p>If I change localhost in the above command to my-ip, then it works fine. Is there anything that I am doing wrong?</p>