MySQL更换内存分配器

操作系统环境:ubuntu 20 (arm 架构)
数据库版本:MySQL 5.7 (arm 架构)

1、查看当前 MySQL 使用内存分配器

ps -ef|grep mysqld 获取 mysqld 进程号,结果没有 jemalloc 或 tcmalloc。默认是 glibc 内存分配器。

root@db-0:~# lsof -p 30722 |grep lib
mysqld  30722 root  mem    REG              253,0   1450832  263589 /usr/lib/aarch64-linux-gnu/libc-2.31.so
mysqld  30722 root  mem    REG              253,0     80200  266507 /usr/lib/aarch64-linux-gnu/libgcc_s.so.1
mysqld  30722 root  mem    REG              253,0    633832  263700 /usr/lib/aarch64-linux-gnu/libm-2.31.so
mysqld  30722 root  mem    REG              253,0   1907976  263286 /usr/lib/aarch64-linux-gnu/libstdc++.so.6.0.28
mysqld  30722 root  mem    REG              253,0     38864  263188 /usr/lib/aarch64-linux-gnu/libatomic.so.1.2.0
mysqld  30722 root  mem    REG              253,0     31584  263774 /usr/lib/aarch64-linux-gnu/librt-2.31.so
mysqld  30722 root  mem    REG              253,0     14560  263607 /usr/lib/aarch64-linux-gnu/libdl-2.31.so
mysqld  30722 root  mem    REG              253,0    198584  263595 /usr/lib/aarch64-linux-gnu/libcrypt.so.1.1.0
mysqld  30722 root  mem    REG              253,0     10016  263559 /usr/lib/aarch64-linux-gnu/libaio.so.1.0.1
mysqld  30722 root  mem    REG              253,0    164304  263767 /usr/lib/aarch64-linux-gnu/libpthread-2.31.so
mysqld  30722 root  mem    REG              253,0    146432  263449 /usr/lib/aarch64-linux-gnu/ld-2.31.so
2、下载安装 jemalloc
Github链接:https://github.com/jemalloc/jemalloc

root@db-0:~# git clone https://github.com/jemalloc/jemalloc.git
root@db-0:~# cd jemalloc/
root@db-0:~/jemalloc# ./autogen.sh 
root@db-0:~/jemalloc# ./configure 
root@db-0:~/jemalloc# make && make install
3、修改 MySQL 配置文件使用 jemalloc 内存分配器
3.1 修改 my.cnf 配置文件

添加如下内容,因为 malloc-lib 是 mysqld_safe 参数值,并不是 mysqld 的,所以这段一定要放到 [mysqld_safe] 下,启动数据库时使用 mysqld_safe 进程启动数据库,
https://dev.mysql.com/doc/refman/8.0/en/mysqld-safe.html#option_mysqld_safe_malloc-lib

[mysqld_safe]
malloc-lib=/usr/local/lib/libjemalloc.so
3.2 启动数据库
root@db-0:/usr/local/mysql57# bin/mysqld_safe  --defaults-file=/etc/my57.cnf --user=root &


报错, --malloc-lib 参数必须要指向 /usr/lib /usr/lib64 等其中一个目录 
mysqld_safe --malloc-lib must be located in one of the directories: /usr/lib /usr/lib64 /usr/lib/i386-linux-gnu /usr/lib/x86_64-linux-gnu

在 /usr/lib 目录创建 libjemalloc.so 的软链接
root@db-0:/usr/local# ln -s /usr/local/lib/libjemalloc.so /usr/lib/libjemalloc.so
同时修改 my.cnf 配置文件
[mysqld_safe]
malloc-lib=/usr/lib/libjemalloc.so

再次启动数据库
root@db-0:/usr/local/mysql57# bin/mysqld_safe  --defaults-file=/etc/my57.cnf --user=root &
[1] 30005
root@db-0:/usr/local/mysql57#  mysqld_safe Adding '/usr/lib/libjemalloc.so' to LD_PRELOAD for mysqld
2023-10-17T12:15:37.463780Z mysqld_safe Logging to '/usr/local/mysql57/mysqld.log'.
2023-10-17T12:15:37.473024Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql57/data
3.3 查看数据库内存分配器

跟之前比多了一行
mysqld 30651 root mem REG 253,0 9481720 393764 /usr/local/lib/libjemalloc.so.2

root@db-0:~# lsof -p 30651 |grep lib
mysqld  30651 root  mem    REG              253,0   1450832  263589 /usr/lib/aarch64-linux-gnu/libc-2.31.so
mysqld  30651 root  mem    REG              253,0     80200  266507 /usr/lib/aarch64-linux-gnu/libgcc_s.so.1
mysqld  30651 root  mem    REG              253,0    633832  263700 /usr/lib/aarch64-linux-gnu/libm-2.31.so
mysqld  30651 root  mem    REG              253,0   1907976  263286 /usr/lib/aarch64-linux-gnu/libstdc++.so.6.0.28
mysqld  30651 root  mem    REG              253,0     38864  263188 /usr/lib/aarch64-linux-gnu/libatomic.so.1.2.0
mysqld  30651 root  mem    REG              253,0     31584  263774 /usr/lib/aarch64-linux-gnu/librt-2.31.so
mysqld  30651 root  mem    REG              253,0     14560  263607 /usr/lib/aarch64-linux-gnu/libdl-2.31.so
mysqld  30651 root  mem    REG              253,0    198584  263595 /usr/lib/aarch64-linux-gnu/libcrypt.so.1.1.0
mysqld  30651 root  mem    REG              253,0     10016  263559 /usr/lib/aarch64-linux-gnu/libaio.so.1.0.1
mysqld  30651 root  mem    REG              253,0    164304  263767 /usr/lib/aarch64-linux-gnu/libpthread-2.31.so
mysqld  30651 root  mem    REG              253,0   9481720  393764 /usr/local/lib/libjemalloc.so.2
mysqld  30651 root  mem    REG              253,0    146432  263449 /usr/lib/aarch64-linux-gnu/ld-2.31.so

posted on 2023-10-18 09:51  柴米油盐酱醋  阅读(523)  评论(0编辑  收藏  举报

导航