最近项目遇到mariaDB cpu占用100%, 网上的解决方案都尝试完了,包括慢日志,数据库锁,应用业务排查,均未发下任何异常。通过排查mysql进程的底层调用,最终发现mysql线程无法释放,导致后台线程堆积,cpu撑满。
解决步骤:
top
top -Hp 27404 //查看该进程下所有线程,按大写P按cpu排序
pstack 27404 //具体到某个线/进程的stack信息
strace -o output.txt -T -tt -e trace=all -p 27404 //跟踪某个pid的starce信息
通过pstack发现数据库结束连接时(one-thread-per-connection) 调用linux底层gcc lib库无法退出线程,数据启动使用的是open jdk ,应该是jdk调用gcc lib库有冲突或bug所致。
解决的2种方式:
1. 升级linux glibc 版本至合适的版本
2. 更改Mariadb 的线程处理方式,规避数据库一个客户端连接一个线程的机制, 采用线程池的方式处理客户端连接,经测试也可以规避这个问题。
thread_handling | pool-of-threads
thread_handling
¶
- Description: Determines how the server handles threads. The default,
one-thread-per-connection
, sees the server use one thread to handle each client connection,pool-of-threads
uses thread_pool_size threads to execute all queries (see Thread Pool in MariaDB, whileno-threads
sees the server use a single thread for all connections (only usable for debugging). - Commandline:
--thread-handling=name
- Scope: Global
- Dynamic: No
- Data Type:
enumeration
- Default Value:
one-thread-per-connection
- Valid Values:
no-threads
,one-thread-per-connection
,pool-of-threads
. - Documentation: Using the thread pool.
- Notes: In MySQL the thread pool is only available in MySQL enterprise 5.5.16 and above. In MariaDB it's available in all versions.