Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use mysql-connector-python with MAMP?

Tags:

python

mysql

Command line mysql works as expected:

mysql --host=localhost --user=django3 --password=django3 \
      --database=django3 --port=8889

producing:

...
Server version: 5.5.34 Source distribution
...

but the script below (adapted from mysql-connector-python==1.2.3 website) produces:

2003: Can't connect to MySQL server on 'localhost:8889' \
     (61 Connection refused)

Why this should be?

import mysql.connector
from mysql.connector import errorcode
try:
    cnx = mysql.connector.connect(user='django3', password='django3',
                              host='localhost', port='8889',
                              database='django3')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exists")
  else:
    print(err)
else:
  cnx.close()

Note

This script will work using a MySQL server on the LAN but not when trying to use the local MAMP MySQL server. Thus devel environment not that portable as long as I have this issue!

like image 984
iainH Avatar asked Oct 22 '25 16:10

iainH


1 Answers

After a little hunting around I realised that I needed to be explicit about the unix socket that MAMP is using.

So the line in the connection expression in the test program in the question needs to be amended as follows:

 ...
 host='localhost', port='8889',  \
 unix_socket='/Applications/MAMP/tmp/mysql/mysql.sock',
 ...
like image 56
iainH Avatar answered Oct 25 '25 06:10

iainH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!