2014-09-29 62 views
1

命令行的MySQL按預期方式工作:如何在MAMP中使用mysql-connector-python?

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

生產:

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

但下面的腳本(改編自MySQL的連接器,蟒== 1.2.3網站)生產:

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

爲什麼這應該是?

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() 

使用局域網上,但不嘗試使用本地MAMP MySQL服務器時,MySQL服務器此腳本工作。因此,只要我有這個問題,發展環境不是那麼便攜!

回答

4

經過一點點hunting around我意識到我需要明確說明MAMP正在使用的unix套接字。

所以需要進行如下修改中的問題測試程序的連接表達式行:

... 
host='localhost', port='8889', \ 
unix_socket='/Applications/MAMP/tmp/mysql/mysql.sock', 
... 
0

發現connection.py和編輯:
'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