简体   繁体   中英

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

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!

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',
 ...

find connection.py and edit:
'unix_socket': '/Applications/MAMP/tmp/mysql/mysql.sock',
...
self._unix_socket = '/Applications/MAMP/tmp/mysql/mysql.sock'

mine: /Library/Python/2.7/site-packages/mysql/connector/connection.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM