当我尝试使用MySQLPython(通过SQLAlchemy)时,我收到错误
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.6-x86_64.egg/MySQLdb/connections.py", line 188, in __init__
super(Connection, self).__init__(*args, **kwargs2)
sqlalchemy.exc.OperationalError: (OperationalError) (2002, "Can't connect to local MySQL server through socket '/opt/local/var/run/mysql5/mysqld.sock' (2)") None None
但我的机器上没有其他mysql客户端看得很好!
我的my.cnf文件说明:
[client]
port = 3306
socket = /tmp/mysql/mysql.sock
[safe_mysqld]
socket = /tmp/mysql/mysql.sock
[mysqld_safe]
socket = /tmp/mysql/mysql.sock
[mysqld]
socket = /tmp/mysql/mysql.sock
port = 3306
,mysql.sock文件确实位于/ tmp / mysql
中我确认〜/ .my.cnf和/var/lib/mysql/my.cnf没有覆盖它。 mysql5客户端程序等没有连接问题,也没有使用jdbc / mysql连接在同一台机器上安装groovy / grails
thrilllap-2:~ swirsky$ mysql5
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.47 Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)
mysql>
为什么mysdb的MySQLdb不能解决这个问题?如果不是my.cnf文件,它会在哪里看?
答案 0 :(得分:4)
您是否尝试在read_default_file='~/.my.cnf'
调用中添加选项connect()
,告诉Python数据库驱动程序读取您的配置文件?我认为否则它会默认忽略该文件。
答案 1 :(得分:3)
默认情况下,MysqlDBlib不读取/etc/my.cnf。将read_default_group =“client”添加到MySQLdb.connect调用将使其读取客户端部分。
答案 2 :(得分:2)
FWIW:我通过将my.cnf中的.sock文件位置更改为MySQLdb想要的位置来修复它(/opt/local/var/run/mysql5/mysqld.sock)但我不喜欢这样做这一点。
答案 3 :(得分:1)
connect
将unix_socket
作为参数,该参数是UNIX套接字文件的路径。 MySQLdb不会读取你的my.cnf(按设计)。