这个应用程序中的python是从docker容器运行的。容器以网络模式运行:“host”。
我无法通过python包sqlalchemy连接到MySQL数据库。我收到以下错误。
OperationalError :( OperationalError)(2003,“无法连接到MySQL 服务器'127.0.0.1'(111)“)无无
以下终端命令可以正常工作
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)
也许有超过1个版本的mysql在运行?只有一个进程正在运行:
ps -ef | grep mysql
74 15459 1 0 10:14 AM ?? 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”显示数据库存在。我已为每个数据库授予该用户所有权限。我也冲了过来。
也许某种防火墙规则会阻止连接?我使用了一点小故障,整个防火墙已被拆除以测试这一点。
此时我甚至不知道还有什么要调试的。这是抛出连接错误的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 El Capitan 10.11.6
答案 0 :(得分:1)
你写的最后一条评论非常重要,可能是问题的原因。
我很确定您的Docker容器有自己的环回接口,地址为127.0.0.1,与您运行MySQL的OSX环回接口不同。
我建议您将MySQL监听到从容器内部可见的地址。您可以从容器内轻松调试该配置。