我正在用mysql在vagrant上运行的flask应用程序上进行本地开发。使用pymysql和sqlalchemy,我正在连接 mysql+pymysql://root:<password>@127.0.0.1:2222/<my_db>
. 运行应用程序时,出现以下错误: sqlalchemy.exc.InternalError: (InternalError) Packet sequence number wrong - got 42 expected 0 None None
从pymysql的github repo的一些问题来看,这是由于多线程造成的。我还没有做任何具体的设置我的应用程序是多线程的,所以我假设这发生自动与 flask 或sqlalchemy。
fwiw我已经能够将sequel-pro-to-ssh设置到mysql中,并使用我正在使用的相同信息在vagrant上运行 mysql+pysql://
. 使用简化的测试用例,我运行了在这个so答案中找到的代码,但仍然遇到了相同的错误。
这是我的流浪档案:
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
这是我的 bootstrap.sh
文件:
# !/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
这是我的 app/__init.py__
文件:
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', 'admin@example.com')
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()
我的 run.py
文件:
# !/usr/bin/env python
from app import app
app.run(debug=True)
根据我得到的错误,我连接正确,但遇到了一个问题,由于线程。为了让pymysql使用这个设置,我需要做什么更改?
暂无答案!
目前还没有任何答案,快来回答吧!