You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3.10+MariaDB环境下Discord Bot新增数据触发Segmentation fault

问题描述

我给Discord Bot接入MariaDB数据库后,在Ubuntu 20.04服务器上新增数据库条目时总会触发Segmentation fault,但本地Windows电脑运行完全正常。启用Xfaulthandler后的控制台输出如下:

Fatal Python error: Segmentation fault

Thread 0x00007fa3d7fff700 (most recent call first):
File "/usr/lib/python3.10/threading.py", line 324 in wait
File "/usr/lib/python3.10/threading.py", line 607 in wait
File "/usr/local/lib/python3.10/dist-packages/discord/gateway.py", line 152 in run
File "/usr/lib/python3.10/threading.py", line 1016 in _bootstrap_inner
File "/usr/lib/python3.10/threading.py", line 973 in _bootstrap

Thread 0x00007fa3ddfac700 (most recent call first):
File "/usr/lib/python3.10/concurrent/futures/thread.py", line 81 in _worker
File "/usr/lib/python3.10/threading.py", line 953 in run
File "/usr/lib/python3.10/threading.py", line 1016 in _bootstrap_inner
File "/usr/lib/python3.10/threading.py", line 973 in _bootstrap

Current thread 0x00007fa3f8aa7740 (most recent call first):
File "/usr/local/lib/python3.10/dist-packages/mariadb/cursors.py", line 296 in execute
File "/root/ayumi/Bot.py", line 217 in on_message
File "/usr/local/lib/python3.10/dist-packages/discord/client.py", line 441 in _run_event
File "/usr/lib/python3.10/asyncio/events.py", line 80 in _run
File "/usr/lib/python3.10/asyncio/base_events.py", line 1909 in _run_once
File "/usr/lib/python3.10/asyncio/base_events.py", line 603 in run_forever
File "/usr/lib/python3.10/asyncio/base_events.py", line 636 in run_until_complete
File "/usr/lib/python3.10/asyncio/runners.py", line 44 in run
File "/usr/local/lib/python3.10/dist-packages/discord/client.py", line 860 in run
File "/root/ayumi/Bot.py", line 1381 in

Extension modules: multidict._multidict, yarl._quoting_c, aiohttp._helpers, aiohttp._http_writer, aiohttp._http_parser, aiohttp._websocket, charset_normalizer.md, frozenlist._frozenlist, numpy.core._multiarray_umath, numpy.core._multiarray_tests, numpy.linalg._umath_linalg, numpy.fft._pocketfft_internal, numpy.random._common, numpy.random.bit_generator, numpy.random._bounded_integers, numpy.random._mt19937, numpy.random.mtrand, numpy.random._philox, numpy.random._pcg64, numpy.random._sfc64, numpy.random._generator, psutil._psutil_linux, psutil._psutil_posix, mariadb._mariadb (total: 24)
Segmentation fault

可能的解决思路

1. 修复MariaDB驱动与系统库兼容性

Segmentation fault大多是C扩展模块和系统底层库不匹配导致的,Ubuntu 20.04自带的MariaDB库版本可能和你安装的mariadb Python包不兼容:

  • 卸载当前驱动包:pip uninstall mariadb
  • 安装适配系统的版本,建议从源码编译:
    sudo apt-get install libmariadb3 libmariadb-dev
    pip install mariadb --no-binary :all:
    
  • 确保驱动版本和服务器版本一致:用mariadb --version看服务端版本,pip show mariadb看包版本,尽量保持大版本同步。

2. 解决异步环境下的线程安全问题

Discord.py是异步框架,而MariaDB默认游标不是线程安全的,如果在异步回调中复用连接/游标会触发崩溃:

  • 不要共享connection或cursor对象,每次操作从连接池获取新连接:
    import mariadb
    from mariadb import ConnectionPool
    
    # 初始化连接池
    pool = ConnectionPool(
        user="你的用户名",
        password="你的密码",
        host="localhost",
        database="你的数据库名",
        pool_size=5
    )
    
    # 在消息回调中使用连接池
    async def on_message(message):
        conn = pool.get_connection()
        cursor = conn.cursor()
        try:
            cursor.execute("INSERT INTO 表名 (列名) VALUES (%s)", ("值",))
            conn.commit()
        finally:
            cursor.close()
            conn.close()
    
  • 或者直接使用异步驱动aiomysql(兼容MariaDB),完全适配异步环境:
    pip install aiomysql
    

3. 排查Python版本兼容性

你当前用Python 3.10,但Ubuntu 20.04默认是Python 3.8,手动升级的版本可能和C扩展库存在冲突:

  • 切换到系统默认Python 3.8重新安装依赖测试:
    sudo apt-get install python3.8 python3.8-pip
    python3.8 -m pip install discord.py mariadb
    
  • 或者更新系统所有依赖到兼容版本:
    sudo apt-get update && sudo apt-get upgrade -y
    

4. 检查代码参数与SQL语句

崩溃发生在execute方法,可能是参数传递异常:

  • 查看Bot.py第217行的execute调用,确保参数是基础Python类型(str、int等),避免传递numpy数组、自定义对象等非预期类型。
  • 开启MariaDB查询日志,确认崩溃前的SQL语句是否正常:
    在/etc/mysql/my.cnf中添加配置:
    [mysqld]
    general_log = 1
    general_log_file = /var/log/mysql/general.log
    
    重启服务后查看日志,排查SQL语法或参数问题。

内容的提问来源于stack exchange,提问作者ItsKoga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 08:20:10