Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

python - Is it possible to use Mysql with SqlAlchemy and Flask if my mysql socket isn't in /tmp?

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 

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

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

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

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

My questions:

  1. Where does sqlalchemy get the idea that /tmp/mysql.sock is the correct location?
  2. Is there a way to change the default via the Flash-SQLAlchemy connector
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'

Should be your connect URI for SQLAlchemy.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...