title: Python script (PyMySQL) does not connect to database on localhost
body: <p>When I am using PHP I can connect to the phpMyAdmin at the localhost simply by writing the following lines in my php script:</p>
  
  <pre><code>&lt;?php
  
  $dbServername = 'localhost';
  $dbUsername = 'root';
  $dbPassword = '';
  $dbName = 'Work';
  
  $conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
  
  ?&gt;
  </code></pre>
  
  <p><em>(The directory of this script is <code>/Users/User/.bitnami/stackman/machines/xampp/volumes/root/htdocs/index.php</code>)</em></p>
  
  <p>Before running the above lines, I have firstly activated <code>XAMPP</code> and I have enabled port <code>localhost:8080</code>. <code>phpMyAdmin</code> is located at <code>http://localhost:8080/phpmyadmin/</code>. <strong>This connects perfectly fine to the database.</strong></p>
  
  <p>Now, in Python I have downloaded <code>PyMySQL</code> and I have written the following lines to do the same simple task:</p>
  
  <pre><code>import pymysql
  
  conn = pymysql.connect(host='localhost', user='root', password='', db='Work')
  </code></pre>
  
  <p><em>(The directory of this script is <code>/Users/User/PycharmProjects/First/Main.py</code>)</em></p>
  
  <p>However, when I try to run this (after I have firstly activated <code>XAMPP</code> and I have enabled port <code>localhost:8080</code>) I am getting the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/User/Library/Python/3.6/lib/python/site-packages/pymysql/connections.py", line 920, in connect
      **kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 724, in create_connection
      raise err
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 713, in create_connection
      sock.connect(sa)
  ConnectionRefusedError: [Errno 61] Connection refused
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "/Users/User/PycharmProjects/GCP-OCR/Main.py", line 14, in &lt;module&gt;
      conn = pymysql.connect(host='localhost', user='root', password='', db='Work')
    File "/Users/User/Library/Python/3.6/lib/python/site-packages/pymysql/__init__.py", line 90, in Connect
      return Connection(*args, **kwargs)
    File "/Users/User/Library/Python/3.6/lib/python/site-packages/pymysql/connections.py", line 699, in __init__
      self.connect()
    File "/Users/User/Library/Python/3.6/lib/python/site-packages/pymysql/connections.py", line 967, in connect
      raise exc
  pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 61] Connection refused)")
  </code></pre>
  
  <p><strong>How can I fix this error and finally connect my Python script to the MySQL database?</strong></p>
  
  <p>Also, keep in mind that even if I replace <code>localhost</code> with <code>127.0.0.1</code> I am getting exactly the same error. Moreover, if I replace <code>localhost</code> with <code>localhost:3306</code> then I am an error too (<code>socket.gaierror: [Errno 8] nodename nor servname provided, or not known</code>).</p>