学习狗书flask web,将容器内置的sqlite数据库换成外接mysql容器,便于应用升级

照着书上的的步骤换了数据库,开启了两个容器以后,访问网页报错
打印mysql容器的logs:

[Note] Aborted connection 59 to db: 'flasky' user: 'flasky' host: '192.168.1.1' (Got an error reading communication packets)

数据库断开链接?(后来查资料得知少许这种警告是正常的)

打印app容器的logs:

sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1054, "Unknown column 'users.name' in 'field list'")

查资料由参考1参考2得知:没有迁移好数据库,进容器内flask db stamp head + flask db migrate + flask db upgrade
(迁移过程中遇到PermissionError: [Errno 13]报错,可以参考我之前写的

换了新数据库以后,要注意两点
1:新数据库没有被deploy(忘了吗,你的代码flasky.py最后赋予了deploy命令用来做一些自动化操作)

# flasky.py
@app.cli.command()
def deploy():
    """Run deployment tasks."""
    # migrate database to latest revision
    upgrade()

    # create or update user roles
    Role.insert_roles()

    # ensure all users are following themselves
    User.add_self_follows()

upgrade():数据库迁移刚才已经做了
接下来:exec指令进入应用容器,接着flask shell 指令进入shell,导入db:from app import db,再手动执行Role.insert_roles()和 User.add_self_follows()
否则你注册了新用户,登录进去也看不见上传博客的输入框,上传不了博客。为什么?
下面代码中有一个判断if current_user.can(Permission.WRITE),没有deploy,Role表是空的(不信你进mysql容器用sql语句查一查,进入方法在后面会讲),新注册的用户没有被分配角色,更别提有WRITE权限了(参考知乎的一个回答

#app/main/views.py
@main.route('/', methods=['GET', 'POST'])
def index():
    form = PostForm()
    if current_user.can(Permission.WRITE) and form.validate_on_submit():
        post = Post(body=form.body.data,....
        ......

注意点2: 跟着书上选的mysql:5.7版本不支持中文字段的输入(查看dockerhub的官方mysql镜像可选版本) 参考资料
如果用mysql5.7,在博客输入框中输入中文,提交后会报错:

sqlalchemy.exc.PendingRollbackError: 
This Session's transaction has been rolled back due to a previous exception during flush.
To begin a new transaction with this Session, first issue Session.rollback().
Original exception was: (pymysql.err.InternalError) 
(1366, "Incorrect string value: '\\xE5\\xBC\\xA0\\xE4\\xB8\\xB9...' for column 'name' at row 1")

如果不想换版本,可以进mysql进行事后补救:
进入mysql的命令(注意跟进入一般容器不同): docker exec -it mysql_container-name mysql -uroot -p
进去后输入: use flasky(flasky是你跟着书上设置的数据库名) ->show tables;(末尾的分号不能忘) ->执行命令
ALTER table 表的名字 convert to character set utf8mb4;(所有可能接收中文字段的表都要改:users,posts,comments)
方法参考命令参考

------分割线-----------------------
时隔四个月重新启动两个容器,发现网站内但凡需要访问数据库的地方都特别慢:查看部署应用的容器日志,响应很久后自动重启

[2023-10-18 08:01:07 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:25)
[2023-10-18 08:01:07 +0000] [25] [INFO] Worker exiting (pid: 25)
[2023-10-18 08:01:07 +0000] [26] [INFO] Booting worker with pid: 26

查看部署mysql的容器:

2023-10-18T07:41:26.416525Z 20 [Note] Aborted connection 20 to db: 'flasky' user: 'flasky' host: '192.168.1.2' (Got an error reading communication packets)
2023-10-18T07:49:04.080247Z 24 [Note] Aborted connection 24 to db: 'flasky' user: 'flasky' host: '192.168.1.2' (Got an error reading communication packets)
2023-10-18T07:50:25.411642Z 40 [Note] Aborted connection 40 to db: 'flasky' user: 'flasky' host: '192.168.1.2' (Got an error reading communication packets)
2023-10-18T07:51:50.684336Z 44 [Note] Aborted connection 44 to db: 'flasky' user: 'flasky' host: '192.168.1.2' (Got an error reading communication packets)
2023-10-18T07:53:31.016601Z 48 [Note] Aborted connection 48 to db: 'flasky' user: 'flasky' host: '192.168.1.2' (Got an error reading communication packets)
2023-10-18T07:54:37.318570Z 52 [Note] Aborted connection 52 to db: 'flasky' user: 'flasky' host: '192.168.1.2' (Got an error reading communication packets)
2023-10-18T07:59:30.867738Z 55 [Note] Aborted connection 55 to db: 'flasky' user: 'flasky' host: '192.168.1.2' (Got an error reading communication packets)
2023-10-18T08:01:07.206371Z 66 [Note] Aborted connection 66 to db: 'flasky' user: 'flasky' host: '192.168.1.2' (Got an error reading communication packets)

很快找到解决方案:进mysql更改最大包

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值