如果我的 mysql 套接字不在/tmp 中,是否可以将 Mysql 与 SqlAlchemy 和 Flask 一起使用? [英] Is it possible to use Mysql with SqlAlchemy and Flask if my mysql socket isn't in /tmp?

查看:16
本文介绍了如果我的 mysql 套接字不在/tmp 中,是否可以将 Mysql 与 SqlAlchemy 和 Flask 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mysql.sock 在我系统上的位置是 /usr/local/mysql5/mysqld.sock

The location for mysql.sock on my system is /usr/local/mysql5/mysqld.sock

thrilllap-2:tmp reuven$ mysqld --print-defaults
mysqld would have been started with the following arguments:
--socket=/usr/local/mysql5/mysqld.sock --port=3306 

当我尝试通过flask中的sqlalchemy使用mysql时,我得到:

When I try to use mysql via sqlalchemy from flask, I get:

  File "build/bdist.macosx-10.6-intel/egg/MySQLdb/connections.py", line 187, in __init__
sqlalchemy.exc.OperationalError: (OperationalError) (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)") None None

mysql 程序正确连接到数据库,就像我系统上的所有其他 mysql 客户端一样.

The mysql program connects correctly to the database, as does every other mysql client on my system.

我的 my.cnf 有正确的套接字位置

My my.cnf has the correct location for the socket

[client]
port            = 3306
socket          = /usr/local/mysql5/mysqld.sock 

[safe_mysqld]
socket      = /usr/local/mysql5/mysqld.sock 

[mysqld_safe]
socket          = /usr/local/mysql5/mysqld.sock 

[mysqld]
socket          = /usr/local/mysql5/mysqld.sock 

port            = 3306

基本的SQLAlchemy"库有一个选项,您可以在其中指定 mysql.sock 的位置,但这不会通过 sqlalchemy/flask 库公开

The base "SQLAlchemy" library has an option where you can specify the location of the mysql.sock, but this isn't exposed through the sqlalchemy / flask library

http://packages.python.org/Flask-SQLAlchemy/config.html

我的问题:

  1. sqlalchemy 从哪里得到/tmp/mysql.sock 是正确位置的想法?
  2. 有没有办法通过 Flash-SQLAlchemy 连接器更改默认值

推荐答案

您必须挖掘确切的语法,但对于 MySQL,我认为他们使用 unix_socket 查询选项.类似的东西:

You'll have to dig up the exact syntax, but for MySQL I think they use a unix_socket query opt. Something like:

mysql:///dbname?unix_socket=/opt/mysql/mysql.sock'

应该是 SQLAlchemy 的连接 URI.

Should be your connect URI for SQLAlchemy.

这篇关于如果我的 mysql 套接字不在/tmp 中,是否可以将 Mysql 与 SqlAlchemy 和 Flask 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆