ubuntu 11.04下,使用google-perftools的tcmalloc加速MySQL

Posted by Elton's Blog on July 15, 2011

1.在 /usr/local/src 下准备好以下文件 libunwind-0.99.tar.gz (64位操作系统需要) google-perftools-1.7.tar.gz

2.安装libunwind(32位操作系统忽略此步骤)

cd /usr/local/src
tar zxvf libunwind-0.99.tar.gz
cd libunwind-0.99
CFLAGS=-fPIC ./configure --enable-shared
make CFLAGS=-fPIC
make CFLAGS=-fPIC install

3.安装google-perftools (最小化安装)

cd /usr/local/src
tar zvxf google-perftools-1.7.tar.gz
cd google-perftools-1.7
mkdir /tmp/tc
./configure --prefix=/tmp/tc --disable-cpu-profiler --disable-heap-profiler --disable-heap-checker --disable-debugalloc --enable-minimal
 make && make install

4.复制tcmalloc动态库到系统库目录,并建立软连接

ls -alt /tmp/tc/lib/*
cp /tmp/tc/lib/libtcmalloc_minimal.so.0.1.0 /usr/local/lib
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.1.0 /usr/local/lib/libtcmalloc.so
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.1.0 /usr/local/lib/libtcmalloc.so.0
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.1.0 /usr/local/lib/libtcmalloc.so.0.1.0
ln -s /usr/local/lib/libtcmalloc_minimal.so.0.1.0 /usr/local/lib/libtcmalloc_minimal.so.0

5.删除临时目录

rm -rf /tmp/tc

6.设置Mysql运行时加载tcmalloc (根据mysql安装位置而定)

vim /usr/local/mysql/bin/mysqld_safe

在# executing mysqld_safe的下一行,加入以下内容

export LD_PRELOAD="/usr/local/lib/libtcmalloc.so"
  1. 重启MySQL
/etc/init.d/mysql start

8.使用lsof命令查看tcmalloc是否起效

sudo lsof -n |grep tcmalloc

应该看到下面的内容

mysqld    12484      mysql  mem       REG        8,7       860657    5382577 /usr/local/lib/libtcmalloc_minimal.so.0.1.0

说明MySQL已经使用上了tcmalloc进行加速了。