【问题标题】:MySQLPython is ignoring my my.cnf file. Where does it get its information?MySQLPython 忽略了我的 my.cnf 文件。它从哪里得到它的信息?
【发布时间】:2010-05-30 21:25:36
【问题描述】:

当我尝试使用 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> 

为什么 MySQLdb for python 不能解决这个问题?如果不是 my.cnf 文件,它会在哪里查看?

【问题讨论】:

    标签: python mysql


    【解决方案1】:

    您是否尝试将选项 read_default_file='~/.my.cnf' 添加到您的 connect() 调用中,以告诉 Python 数据库驱动程序读取您的配置文件?我认为否则默认情况下它会忽略该文件。

    【讨论】:

    • 或者,如果你在 SQLAlchemy 中使用 create_engine:engine = create_engine(DB_DSN, ... connect_args={'read_default_file': './etc/my.cnf'})
    【解决方案2】:

    MysqlDBlib 默认不读取 /etc/my.cnf。将 read_default_group="client" 添加到 MySQLdb.connect 调用将使其读取客户端部分。

    【讨论】:

      【解决方案3】:

      FWIW:我通过将 my.cnf 中的 .sock 文件位置更改为 MySQLdb 想要的位置来“修复”它(/opt/local/var/run/mysql5/mysqld.sock),但我讨厌这样做那个。

      【讨论】:

      • 见布兰登的回答。我没有意识到这是可能的。
      【解决方案4】:

      connect 将unix_socket 作为参数,它是 UNIX 套接字文件的路径。 MySQLdb 不会读取您的 my.cnf(按设计)。

      【讨论】:

      • 该死的。太糟糕了。我需要使用 DSN,然后如果 DSN 在本地并且您在 OSX 上,则它不起作用。我讨厌 OSX。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      相关资源
      最近更新 更多