步骤:先安装TCMalloc,在安装mysql
一、为MySQL添加TCMalloc库降低系统负载
TCMalloc(Thread-CachingMalloc)是google开发的开源工具──“google-perftools”中的成员。与标准的glibc库的malloc相比,TCMalloc在内存的分配上效率和速度要高得多,可以在很大程度上提高MySQL服务器在高并发情况下的性能,降低系统负载。
1、64位操作系统请先安装libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API。
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz
tar zxvf libunwind-0.99.tar.gz
cd libunwind-0.99/
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
2、安装google-perftools:
wget http://google-perftools.googlecode.com/files/google-perftools-1.6.tar.gz
tar zxvf google-perftools-1.6.tar.gz
cd google-perftools-1.6/
./configure
make && make install
echo "/usr/local/lib" >/etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig
二、安装MYSQL
[root@mg04 opt]# tar zxvf libevent-1.4.13-stable.tar.gz
[root@mg04 opt]# cd libevent-1.4.13-stable
[root@mg04 libevent-1.4.13-stable]# ./configure --prefix=/usr/local/libevent
[root@mg04 libevent-1.4.13-stable]# make && make install
[root@mg04 libevent-1.4.13-stable]# ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /usr/lib
[root@mg04 opt]# tar zxvf mysql-5.1.49.tar.gz
[root@mg04 opt]# cd mysql-5.1.49
[root@mg04 mysql-5.1.49]# groupadd mysql
[root@mg04 mysql-5.1.49]# useradd -g mysql -s /sbin/nologin mysql
[root@mg04 mysql-5.1.49]# ./configure --prefix=/usr/local/mysql --with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock --localstatedir=/usr/database/mysql_data --enable-assembler --enable-thread-safe-client --enable-local-infile --with-big-tables --with-extra-charsets=utf8,gbk --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --with-mysqld-ldflags=-ltcmalloc --with-mysqld-user=mysql --with-pthread --without-ndb-debug --with-plugins=partition,innobase,myisammrg
[root@mg04 mysql-5.1.49]# make && make install
[root@mg04 mysql-5.1.49]# cp support-files/my-huge.cnf /etc/my.cnf
[root@mg04 mysql-5.1.49]# mkdir -p /usr/database/mysql_data
[root@mg04 mysql-5.1.49]# /usr/local/mysql/bin/mysql_install_db --user=mysql
[root@mg04 mysql-5.1.49]# chown -R root /usr/local/mysql/
[root@mg04 mysql-5.1.49]# chown -R mysql /usr/database/mysql_data/
[root@mg04 mysql-5.1.49]# chgrp -R mysql /usr/local/mysql/
[root@mg04 mysql-5.1.49]# /usr/local/mysql/bin/mysqld_safe &
[root@mg04 mysql-5.1.49]# cp support-files/mysql.server /etc/init.d/mysqld
[root@mg04 mysql-5.1.49]# chmod 755 /etc/init.d/mysqld
[root@mg04 mysql-5.1.49]# /etc/init.d/mysqld stop
Shutting down MySQL.....100712 23:19:20 mysqld_safe mysqld from pid file /mnt/database/mysql_data/mg04.jieshi.org.pid ended
[ OK ]
[2]- Done /mnt/software/mysql/bin/mysqld_safe
[root@mg04 mysql-5.1.49]# /etc/init.d/mysqld start
Starting MySQL. [ OK ]
[root@mg04 mysql-5.1.49]# /etc/init.d/mysqld restart
Shutting down MySQL.... [ OK ]
Starting MySQL. [ OK ]
[root@mg04 mysql-5.1.49]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
[root@data03 mysql-5.1.49]# source /etc/profile
[root@mg04 mysql-5.1.49]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.49-log Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@mg04 mysql-5.1.49]# chkconfig mysqld on
[root@data03 opt]# chkconfig --level 24 mysqld off
[root@data03 opt]# chkconfig --list mysqld
mysqld 0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:启用 6:关闭
[root@mg04 ~]# mysqladmin -uroot password dian588
[root@mg04 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.49-log Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
修改MySQL启动脚本(根据你的MySQL安装位置而定):
vi /usr/local/mysql/bin/mysqld_safe
在# executingmysqld_safe的下一行,加上:
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
保存后退出,然后重启MySQL服务。
使用lsof命令查看tcmalloc是否起效:
lsof -n | grep tcmalloc
如果出现以下信息,说明tcmalloc已经起效:
mysqld 10847 mysql mem REG 8,5 1203756 20484960/usr/local/lib/libtcmalloc.so.0.0.0
参考文献:http://www.iteye.com/topic/485354
http://coralzd.blog.51cto.com/90341/729847