简体   繁体   English

尝试将 MySQL 与 sqlalchemy (Flask) 连接时出错

[英]Error while trying to connect MySQL with sqlalchemy (Flask)

I have been looking for this error for a while but nothing seems to solve my problem, the thing is.. I was using sqlite3 with flask, but it seems like sqlite3 does not support migrations, so I decided to change to Mysql.我一直在寻找这个错误一段时间,但似乎没有解决我的问题,问题是..我正在使用带有烧瓶的 sqlite3,但似乎 sqlite3 不支持迁移,所以我决定更改为 Mysql。 I have installed LAMPP, and I created a symbolik link from:我已经安装了 LAMPP,并从以下位置创建了一个符号链接:

/opt/lampp/var/mysql/mysql.sock

to:到:

/var/run/mysqld/mysql.sock

but still I get this error (it is a little long but the trouble relapse on the last line):但我仍然收到这个错误(它有点长,但最后一行的问题复发了):

Traceback (most recent call last):
File "db_create.py", line 5, in <module>
db.create_all()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 895, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 887, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 3634, in create_all
tables=tables)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1851, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1844, in _optional_conn_ctx_manager
with self.contextual_connect() as conn:
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2035, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2074, in _wrap_pool_connect
e, dialect, self)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1405, in _handle_dbapi_exception_noconnection
exc_info
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 2070, in _wrap_pool_connect
return fn()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 376, in connect
return _ConnectionFairy._checkout(self)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 708, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 480, in checkout
rec = pool._do_get()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 1055, in _do_get
self._dec_overflow()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 1052, in _do_get
return self._create_connection()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 323, in _create_connection
return _ConnectionRecord(self)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 449, in __init__
self.connection = self.__connect()
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/pool.py", line 602, in __connect
connection = self.__pool._invoke_creator(self)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 97, in connect
return dialect.connect(*cargs, **cparams)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 385, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/home/gerardo/Documentos/python_web_dev/flask-intro/venv/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2) 
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

On my research, I found that I have to change the line:在我的研究中,我发现我必须改变这一行:

socket      = /var/run/mysqld/mysqld.sock

placed on /etc/mysql/my.cnf to:放在 /etc/mysql/my.cnf 上:

socket      = /opt/lampp/var/mysql/mysql.sock

(The path were "mysql.sock" is located), but still it does not solve my problem. (路径是“mysql.sock”所在),但它仍然没有解决我的问题。 Thank you!谢谢!

In the database configuration.在数据库配置中。 Replace localhost with 127.0.0.1 :将 localhost 替换为127.0.0.1

environ.get('APP_DATABASE_HOST', 'localhost')

to

environ.get('APP_DATABASE_HOST', '127.0.0.1')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM