上的MySQL服務器此應用程序中的python運行於Docker容器中。容器正在網絡模式下運行:「主機」。OSX El Capitan sqlalchemy無法連接到'127.0.0.1'(111)
我無法通過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)
也許有超過1個版本的mysql正在運行?只有一個進程在運行:
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'顯示數據庫存在。我已經爲每個數據庫授予了該用戶的所有權限。我也衝過了這些詭計。
也許某種防火牆規則正在停止連接?我使用了小小的snitch,並且整個防火牆已經被取下來測試這個。
我甚至不知道還有什麼要調試的。下面是python腳本的拋出連接錯誤的簡化版本:
DSN_FORMAT = "mysql://%(user)s:%(passwd)[email protected]%(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
你能顯示給出錯誤的實際代碼嗎?或者至少是一個簡化版本。 –
我同意@ gsi-frank--一個簡單的SQLAlchemy引擎和[連接設置(如其文檔中的連接設置)]的示例(http://docs.sqlalchemy.org/en/latest/core/connections.html# module-sqlalchemy.engine)在這裏會很有幫助(既可以簡化調試,又可以解決SO中的問題)。 –
@ gsi-frank done –