2

I am doing local development on a Flask application with MySQL set up to run on Vagrant. Using PyMySQL with SQLAlchemy, I'm connecting with mysql+pymysql://root:<password>@127.0.0.1:2222/<my_db>. When I run my application, I get the following error:

sqlalchemy.exc.InternalError: (InternalError) Packet sequence number wrong - got 42 expected 0 None None

From looking at some issues on PyMySQL's GitHub repo, this comes up due to multi-threading. AFAIK I haven't done anything specifically to set my app to be multi-threaded so I am assuming that happens automatically with Flask or SQLAlchemy.

FWIW I have been able to set up Sequel Pro to SSH into MySQL running on Vagrant using same info I am using with mysql+pysql://. Using a reduced test case, I ran the code I found in this SO answer and still ran into the same error.

Here is my Vagrant file:

Vagrant.configure("2") do |config|
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"
  config.vm.network :forwarded_port, guest: 3306, host: 3306
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network "private_network", ip: "33.33.33.10", type: "dhcp", auto_config: false

end

Here is my bootstrap.sh file:

#!/usr/bin/env bash

sudo apt-get update
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password password p<password>'
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password_again password p<password>'
sudo apt-get update
sudo apt-get -y install mysql-server-5.5

if [ ! -f /var/log/databasesetup ];
then
    sed -i "s/^bind-address/#bind-address/" /etc/mysql/my.cnf
    echo "GRANT ALL ON *.* TO 'root'@'{99.99.99.99}' IDENTIFIED BY '<password>' WITH GRANT OPTION" | mysql -uroot -p<password>
    echo "FLUSH PRIVILEGES" | mysql -uroot -p<password>
    echo "CREATE DATABASE my_db" | mysql -uroot -p<password>

    touch /var/log/databasesetup

    if [ -f /vagrant/data/initial.sql ];
    then
        mysql -uroot -p<password> my_db < /vagrant/data/initial.sql
    fi
    sudo /etc/init.d/mysql restart
fi

if [ ! -h /var/www ];
then

    rm -rf /var/www sudo
    ln -s /vagrant/public /var/www

    a2enmod rewrite

    sed -i '/AllowOverride None/c AllowOverride All' /etc/apache2/sites-available/default

    service apache2 restart
fi

Here is my app/__init.py__ file:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:<password>@127.0.0.1:2222/my_db'
db = SQLAlchemy(app)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)

    def __init__(self, username, email):
        self.username = username
        self.email = email

    def __repr__(self):
        return '<User %r>' % self.username

admin = User('admin', '[email protected]')

db.create_all() # In case user table doesn't exists already. Else remove it.    

db.session.add(admin)

db.session.commit() # This is needed to write the changes to database

User.query.all()

User.query.filter_by(username='admin').first()

My run.py file:

#!/usr/bin/env python

from app import app

app.run(debug=True)

Based on the error I am getting, I am connecting correctly but running into an issue due to threading. What do I need to change to get PyMySQL to work with this setup?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.