OSX El Capitan sqlalchemy 无法连接到“127.0.0.1”上的 MySQL 服务器 (111)

OSX El Capitan sqlalchemy Can't connect to MySQL server on '127.0.0.1' (111)

此应用中的 python 来自 docker 容器 运行。容器在网络模式下 运行:"host".

我在通过 python 包 sqlalchemy 连接到 MySQL 数据库时遇到问题。我收到以下错误。

OperationalError: (OperationalError) (2003, "Can't connect to MySQL server on '127.0.0.1' (111)") None None

以下终端命令工作正常

mysql -h 127.0.0.1 -u my_user --password='password' db -e "SHOW TABLES;"
enter code here

也许 sqlalchemy 没有使用您所说的正确配置?在第一次连接到 MySQL:

之前,我已经在调试行中打印出配置
  print config.get('repository', 'host') // 127.0.0.1
  print config.get('repository', 'user') // my_user
  print config.get('repository', 'passwd') // password

也许那个配置仍然没有进入 sqlalchemy?让我们打印出引擎字符串

  engine = getUnaffiliatedEngine()
  print engine //Engine(mysql://my_user:***@127.0.0.1:3306)
  with engine.connect() as connection:
    for s in statements:
      if s.strip():
        connection.execute(s)

也许 mysql 运行 不止 1 个版本?只有一个进程是 运行:

ps -ef | grep mysql

74 15459 1 0 10:14AM ?? 0:00.80 /usr/local/mysql/bin/mysqld --user=_mysql

也许该用户没有访问数据库的权限?

mysql> select User, Host from mysql.user;

+------+--------------------------+
| User | Host                     |
+------+--------------------------+
|my_user 127.0.0.1                |
| root | 127.0.0.1                |
| root | ::1                      |
|      | localhost                |
| root | localhost                |
+------+--------------------------+

同样'show databases'表明数据库存在。我已将每个数据库的所有特权授予该用户。我也刷新了特权。

也许某种防火墙规则正在阻止连接?我用的是 little snitch,为了测试这个,整个防火墙都被拆除了。

此时我什至不知道还要调试什么。这是抛出连接错误的 python 脚本的简化版本:

DSN_FORMAT = "mysql://%(user)s:%(passwd)s@%(host)s:%(port)s"

def getDSN():
  return DSN_FORMAT % dict(config.items("repository"))

def getUnaffiliatedEngine():
  return create_engine(getDSN())

def reset(offline=False):
  config.loadConfig()
  dbName = config.get('repository', 'db')
  print config.get('repository', 'host')
  print config.get('repository', 'user')
  print config.get('repository', 'passwd')

  resetDatabaseSQL = (
      "DROP DATABASE IF EXISTS %(database)s; "
      "CREATE DATABASE %(database)s;" % {"database": dbName})
  statements = resetDatabaseSQL.split(";")

  engine = getUnaffiliatedEngine()
  print engine
  with engine.connect() as connection:
    for s in statements:
      if s.strip():
        connection.execute(s)

这是 tcpdump -i lo0 port 3306 的输出:

11:44:41.224036 IP localhost.58797 > localhost.mysql: Flags [P.], seq 3915736486:3915736498, ack 2134634265, win 12519, options [nop,nop,TS val 980567261 ecr 980503692], length 12
11:44:41.224105 IP localhost.mysql > localhost.58797: Flags [.], ack 12, win 12737, options [nop,nop,TS val 980567261 ecr 980567261], length 0
11:44:41.224178 IP localhost.mysql > localhost.58797: Flags [P.], seq 1:19, ack 12, win 12737, options [nop,nop,TS val 980567261 ecr 980567261], length 18
11:44:41.224218 IP localhost.58797 > localhost.mysql: Flags [.], ack 19, win 12519, options [nop,nop,TS val 980567261 ecr 980567261], length 0
11:45:07.422776 IP localhost.58796 > localhost.mysql: Flags [P.], seq 2953728354:2953728366, ack 432872138, win 12483, options [nop,nop,TS val 980593366 ecr 980533534], length 12
11:45:07.422807 IP localhost.mysql > localhost.58796: Flags [.], ack 12, win 12729, options [nop,nop,TS val 980593366 ecr 980593366], length 0
11:45:07.422856 IP localhost.mysql > localhost.58796: Flags [P.], seq 1:19, ack 12, win 12729, options [nop,nop,TS val 980593366 ecr 980593366], length 18
11:45:07.422877 IP localhost.58796 > localhost.mysql: Flags [.], ack 19, win 12482, options [nop,nop,TS val 980593366 ecr 980593366], length 0

MySQL 5.6 OSX 埃尔卡皮坦 10.11.6

您最后写的评论非常重要,可能是问题的原因。

我很确定您的 Docker 容器有自己的环回接口,其地址为 127.0.0.1,这与您 运行 的 OSX 环回接口不同MySQL.

我建议您将 MySQL 监听放在容器内可见的地址上。您可以从容器内部轻松调试该配置,使 telnet ip_address 3306