Reputation: 3442
I'm trying to use the Google App Engine's dev server with MySQL and sqlalchemy on my local machine. For some reason, I can connect to my MySQL server just fine in an interactive session, but the same connection code fails when run from the dev server.
I have a virtualenv with sqlalchemy installed, and I've symlinked the virtualenv's site-packages directory into a directory that the dev server can see [a]. I also have mysql-connector installed system-wide, and I have symlinked the system wide install into my virtualenv's site-packages.
Here's an interactive session showing that the libs are set up properly (assemblies
is the root directory of my GAE project):
>>> import sqlalchemy as sa
>>> import sqlalchemy.orm as orm
>>> import assemblies.models as models
>>> engine = sa.create_engine('mysql+mysqlconnector://<username>:<password>@localhost/test')
>>> S = orm.sessionmaker(bind=engine)
>>> session = S()
>>> session.query(models.User).all()
[<assemblies.models.User object at 0x0000000003043978>, <assemblies.models.User
object at 0x0000000003043F28>, <assemblies.models.User object at 0x0000000003043
F98>]
However, when I run almost the same code in my GAE program, I get a connection error from the MySQL server. Here's the main app file:
import sqlalchemy as sa
import sqlalchemy.orm as orm
engine = sa.create_engine(<db url>, echo=True)
Session = orm.sessionmaker(bind=engine)
import models as models
from google.appengine.api import users
<unrelated JINJA stuff etc.>
class Users(webapp2.RequestHandler):
def get(self):
session = Session()
users = session.query(models.User).all()
This raises a connection error:
File "D:\gaetest\assemblies\main.py", line 34, in get
users = session.query(models.User).all()
File "D:\gaetest\assemblies\lib\sqlalchemy\orm\query.py", line 2398, in all
return list(self)
...
File "D:\gaetest\assemblies\lib\mysql\connector\network.py", line 251, in recv_plain
errno=2055, values=(self.get_address(), _strioerror(err)))
OperationalError: (mysql.connector.errors.OperationalError) 2055: Lost connectio n to MySQL server at 'localhost:3306', system error:
I checked the database url and made sure I activated the virtualenv. What could be causing this connection problem? Does GAE need additional configuration for this to work?
[a]: I essentially use the official instructions on "vendoring" but where the vendor directory (called lib
in the instructions) is a symlink to the virtualenv's site-packages. This lets the virtualenv see the libs when I'm working independently of the GAE dev server.
Upvotes: 1
Views: 79
Reputation: 3442
The problem was this
I also have mysql-connector installed system-wide, and I have symlinked the system wide install into my virtualenv's site-packages.
GAE doesn't support mysql-connector. It only supports mysql-python which uses MySQLdb. Therefore, you have to install mysql-python. Installing it system-wide and using the symlink trick mentioned in OP works fine if you want to do that.
Upvotes: 1