CentOS6.6系统源代码安装mysql5.5.28教程(附源码包下载地址)+sysbench的安装

时间:2021-02-20 02:23:31

mysql从5.5版本开始,不再使用./configure编译,而是使用cmake编译器,具体的cmake编译参数可以参考mysql官网文档(※ 非常重要)

http://dev.mysql.com/doc/refman/5.5/en/source-configuration-options.html

mysql-5.5.28.tar.gz源码包下载地址:

http://down.51cto.com/data/700556

我的mysql目录配置如下:

安装路径:/usr/local/mysql

数据库路径:/data/mysql

源码包存放位置:/usr/software

准备工作:安装基本依赖包,先用yum安装cmake、automake 、autoconf ,另MySQL 5.5.x需要最少安装的包有:bison,gcc、gcc-c++、ncurses-devel

[root@localhost ~]# yum install cmake make -y

[root@localhost ~]# yum install gcc gcc-c++ autoconf bison automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* -y

[root@localhost ~]# cp /root/mysql-5.5.28.tar.gz /usr/software/

[root@localhost ~]# cd /usr/software

开始编译安装

[root@localhost ~]# tar -zxvf mysql-5.5.28.tar.gz

[root@localhost ~]# cd mysql-5.5.28

[root@localhost ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_READLINE=1 \

-DENABLED_LOCAL_INFILE=1 \

-DMYSQL_DATADIR=/data/mysql/ \

-DMYSQL_TCP_PORT=3306

[root@localhost ~]# make && make install

mysql官网英文文档简单翻译说明一下

The MyISAM, MERGE, MEMORY, and CSV engines are mandatory (always compiled into the server) and need not be installed explicitly.(说明:mysql默认支持的数据库引擎有MyISAM, MERGE, MEMORY, CSV,无需在编译时再声明)

所以上面的编译条件省掉了如下两行

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DWITH_MEMORY_STORAGE_ENGINE=1 \

但INNODB一定要声明式安装,所以多了这一行

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

查看mysql.mysql的用户及组是否存在

CentOS6.6系统源代码安装mysql5.5.28教程(附源码包下载地址)+sysbench的安装CentOS6.6系统源代码安装mysql5.5.28教程(附源码包下载地址)+sysbench的安装

  不OK就执行以下两行命令(否则跳过这一步)

[root@localhost ~]# groupadd mysql #添加mysql用户组

[root@localhost ~]# useradd mysql -g mysql -s /sbin/nologin # 添加mysql用户

以下带红色字体的命令非常非常,必须要执行

[root@localhost ~]# cd /usr/local/mysql

[root@localhost ~]# chown mysql.mysql -R . #将mysql目录赋予mysql用户的执行权限

[root@localhost ~]# chown mysql.mysql -R /data/mysql

[root@localhost ~]# cp support-files/my-medium.cnf /etc/my.cnf #mysql配置文件

[root@localhost ~]# chmod 755 scripts/mysql_install_db #赋予mysql_install_db执行权限

以下命令为mysql 启动及自启动配置

[root@localhost ~]# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/

[root@localhost ~]# cp support-files/mysql.server /etc/init.d/mysqld

[root@localhost ~]# chmod 755 /etc/init.d/mysqld

查看mysqld服务是否设置为开机启动

[root@localhost ~]# chkconfig --list|grep mysqld

设置为开机启动

[root@localhost ~]# chkconfig mysqld on

启动mysql数据库,会输出一系列有用的信息,告诉你接下去如何初始化mysql

[root@centos mysql]# service mysqld start

初始化 MySQL 数据库: Installing MySQL system tables...

OK

Filling help tables...

OK

To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !

To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'

/usr/bin/mysqladmin -u root -h centos.huoba password 'new-password'

Alternatively you can run:

/usr/bin/mysql_secure_installation

which will also give you the option of removing the test

databases and anonymous user created by default. This is

strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

按照上述英文,我们来初始化管理员root的密码

[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -u root password 'yourpassword'

众所周知,mysql有两种帐号类型,即localhost和%,前者限本机连接mysql,后者可用于其它机器远程连接mysql

最后,处理帐号登录问题,让root帐号密码可以本地和远程连接使用

[root@localhost ~]# /usr/local/mysql/bin/mysql -u root -p #敲入该命令后,屏幕会提示输入密码,输入上一步设置的yourpassword

删除root密码为空的记录

mysql> use mysql;

mysql> delete from user where password='';

mysql> flush privileges;

配置mysql允许root远程登录 #登录

mysql> grant all privileges on *.* to root@'%' identified by "root";

mysql> flush privileges;

mysql> select User,Password,Host from user;

mysql> quit

自此,mysql的安装完成,下面开始sysbench的安装!

sysbench 介绍

SysBench是一个模块化的、跨平台、多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况。它主要包括以下几种方式的测试:

  1. cpu性能
  2. 磁盘io性能
  3. 调度程序性能
  4. 内存分配及传输速度
  5. POSIX线程性能
  6. 数据库性能(OLTP基准测试)

目前sysbench主要支持 MySQL,pgsql,Oracle 这3种数据库。

sysbench 安装

默认支持MySQL,如果需要测试Oracle/PostgreSQL,则在configure时需要加上–with-oracle或者–with-pgsql参数,
sysbench默认安装在: /usr/local/bin/sysbench

shell> tar -zxvf sysbench-0.5.tar.gz -C /db/tool/
shell> cd /db/tool/sysbench-0.5
shell> chmod +x autogen.sh shell> ./autogen.sh
automake 1.10.x (aclocal) wasn't found, exiting
shell> yum install automake
shell> ./autogen.sh
libtoolize 1.4+ wasn't found, exiting
shell> yum install libtool shell> ./autogen.sh
./autogen.sh
./autogen.sh: running `aclocal -I m4'
./autogen.sh: running `libtoolize --copy --force'
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `config'.
libtoolize: copying file `config/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `m4'.
libtoolize: copying file `m4/libtool.m4'
libtoolize: copying file `m4/ltoptions.m4'
libtoolize: copying file `m4/ltsugar.m4'
libtoolize: copying file `m4/ltversion.m4'
libtoolize: copying file `m4/lt~obsolete.m4'
./autogen.sh: running `autoheader'
./autogen.sh: running `automake -c --foreign --add-missing'
configure.ac:23: installing `config/compile'
configure.ac:11: installing `config/config.guess'
configure.ac:11: installing `config/config.sub'
configure.ac:16: installing `config/install-sh'
configure.ac:16: installing `config/missing'
sysbench/Makefile.am: installing `config/depcomp'
./autogen.sh: running `autoconf'
Libtoolized with: libtoolize (GNU libtool) 2.2.6b
Automade with: automake (GNU automake) 1.11.1
Configured with: autoconf (GNU Autoconf) 2.63 shell> ./configure --prefix=/db/sysbench --with-mysql-includes=/usr/local/mysql/include --with-mysql-libs=/usr/local/mysql/lib
shell> make && make install

测试安装
shell> cp /db/sysbench/bin/sysbench /usr/local/bin/ shell> sysbench --help
sysbench: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file:
No such file or directory
问题原因:sysbench无法找到mysql的库文件,可能是环境变量LD_LIBRARY_PATH没有设置,设置后即可解决该问题:
添加export LD_LIBRARY_PATH=/db/mysql/lib,如下打开全局变量配置文件
shell>  vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/home/work/local/python/lib/python2.6/site-packages/django/bin/:$HOME/bin:/home/work/local/mysql5/bin/;
LD_LIBRARY_PATH=/home/work/local/mysql5/lib/mysql
alias py='/home/work/local/python/bin/python'
export LD_LIBRARY_PATH=/db/mysql/lib
unset USERNAME
然后保存,重启电脑。 重启后,即可进行一下测试

CPU测试

sysbench CPU测试使用64位整数,测试计算素数直到某个最大值所需要的时间

shell> sysbench --test=cpu --cpu-max-prime=2000 run
sysbench 0.5: multi-threaded system evaluation benchmark Running the test with following options:
Number of threads: 1
Random number generator seed is 0 and will be ignored Primer numbers limit: 2000 Threads started! Test execution summary:
total time: 2.2452s
total number of events: 10000
total time taken by event execution: 2.2347s
per-request statistics:
min: 0.20ms
avg: 0.22ms
max: 3.35ms
approx. 95 percentile: 0.27ms Threads fairness:
events (avg/stddev): 10000.0000/0.00
execution time (avg/stddev): 2.2347/0.00 补充:
查看CPU信息方法
查看物理cpu个数
grep "physical id" /proc/cpuinfo | sort -u | wc -l
查看核心数量
grep "core id" /proc/cpuinfo | sort -u | wc -l
查看线程数量
grep "processor" /proc/cpuinfo | sort -u | wc -l 在sysbench的测试中,--num-threads取值为"线程数量"即可

线程(thread)测试

测试线程调度器的性能。对于高负载情况下测试线程调度器的行为非常有用

shell> sysbench --test=threads --num-threads=64 --thread-yields=100 --thread-locks=2 run
sysbench 0.5: multi-threaded system evaluation benchmark Running the test with following options:
Number of threads: 64
Random number generator seed is 0 and will be ignored Threads started! Test execution summary:
total time: 1.9581s
total number of events: 10000
total time taken by event execution: 124.8938s
per-request statistics:
min: 0.05ms
avg: 12.49ms
max: 151.15ms
approx. 95 percentile: 50.83ms Threads fairness:
events (avg/stddev): 156.2500/14.48
execution time (avg/stddev): 1.9515/0.00

文件IO性能测试

  • 生成需要的测试文件,文件总大小5G,16个并发线程。执行完后会在当前目录下生成一堆小文件。
shell> sysbench --test=fileio --num-threads=16 --file-total-size=5G prepare
  • 执行测试,指定随机读写模式

指定读写模式:

  1. seqwr 顺序写入
  2. seqrewr 顺序重写
  3. seqrd 顺序读取
  4. rndrd 随机读取
  5. rndwr 随机写入
  6. rndrw 混合随机读/写
shell> sysbench --test=fileio --num-threads=16 --init-rng=on --file-total-size=5G --file-test-mode=rndrw run
sysbench 0.5: multi-threaded system evaluation benchmark Running the test with following options:
Number of threads: 16
Random number generator seed is 0 and will be ignored Threads started! Operations performed: 5999 reads, 4001 writes, 12800 Other = 22800 Total
Read 93.734Mb Written 62.516Mb Total transferred 156.25Mb (9.2561Mb/sec) ##吞吐量
592.39 Requests/sec executed Test execution summary:
total time: 16.8808s
total number of events: 10000
total time taken by event execution: 176.1816s
per-request statistics:
min: 0.01ms
avg: 17.62ms
max: 416.73ms
approx. 95 percentile: 104.82ms Threads fairness:
events (avg/stddev): 625.0000/62.39
execution time (avg/stddev): 11.0114/0.67
  • 清除测试文件
shell> sysbench --test=fileio --num-threads=16 --file-total-size=5G cleanup

互斥锁(Mutex)测试

测试互斥锁的性能,方式是模拟所有线程在同一时刻并发运行,并都短暂请求互斥锁。

shell> sysbench --test=mutex --num-threads=16 --mutex-num=1024 --mutex-locks=10000 --mutex-loops=5000 run
sysbench 0.5: multi-threaded system evaluation benchmark Running the test with following options:
Number of threads: 16
Random number generator seed is 0 and will be ignored Threads started! Test execution summary:
total time: 0.0135s
total number of events: 16
total time taken by event execution: 0.0411s
per-request statistics:
min: 0.70ms
avg: 2.57ms
max: 9.19ms
approx. 95 percentile: 9.16ms Threads fairness:
events (avg/stddev): 1.0000/0.00
execution time (avg/stddev): 0.0026/0.00

内存测试

内存测试测试了内存的连续读写性能。

shell> sysbench --test=memory --num-threads=16 --memory-block-size=8192 --memory-total-size=1G run
sysbench 0.5: multi-threaded system evaluation benchmark Running the test with following options:
Number of threads: 16
Random number generator seed is 0 and will be ignored Threads started! Operations performed: 131072 (381158.38 ops/sec) 1024.00 MB transferred (2977.80 MB/sec) ##吞吐量 Test execution summary:
total time: 0.3439s
total number of events: 131072
total time taken by event execution: 3.9915s
per-request statistics:
min: 0.00ms
avg: 0.03ms
max: 51.02ms
approx. 95 percentile: 0.00ms ##大约95%的时间分布 Threads fairness:
events (avg/stddev): 8192.0000/1166.77
execution time (avg/stddev): 0.2495/0.02

MySQL数据库测试

sysbench 0.5通过一系列LUA脚本来替换之前的oltp,来模拟更接近真实的基准测试环境。这些测试脚本包含:insert.lua、oltp.lua、parallel_prepare.lua、select_random_points.lua、update_index.lua、delete.lua oltp_simple.lua、select.lua、select_random_ranges.lua、update_non_index.lua,脚本使用方式基本类似。

sysbench 0.5默认使用sbtest库,但是需要自己手工先创建好,也可以使用--mysql-db指定,其他非默认项指定选项:

  1. -mysql-host
  2. -mysql-port
  3. -mysql-socket
  4. -mysql-user
  5. -mysql-password
  6. -mysql-db
  7. -mysql-ssl
  • prepare
    生成表并插入数据,可使用parallel_prepare.lua脚本来并行准备数据。
  1. –db-driver 服务器类型 mysql | drizzle,默认为mysql
  2. –mysql-table-engine 表存数引擎
  3. –myisam-max-rows MyISAM表MAX_ROWS选项(用于大表)
  4. –oltp-table-count 生成表数量[sbtest1、sbtest2...]
  5. –oltp-table-size 生成表的行数
  6. –oltp-secondary ID列生成二级索引而不是主键
  7. –oltp-auto-inc 设置ID列是否自增 on | off,默认为on
shell> cd /db/tool/sysbench-0.5/sysbench
shell> sysbench --test=./tests/db/oltp.lua --mysql-table-engine=myisam --oltp-table-size=100000 --mysql-db=test
--mysql-user=root --oltp-tables-count=10 --mysql-password=oracle --mysql-socket=/tmp/mysql.sock prepare
sysbench 0.5: multi-threaded system evaluation benchmark Creating table 'sbtest1'...
Inserting 100000 records into 'sbtest1'
Creating table 'sbtest2'...
Inserting 100000 records into 'sbtest2'
Creating table 'sbtest3'...
Inserting 100000 records into 'sbtest3'
Creating table 'sbtest4'...
Inserting 100000 records into 'sbtest4'
Creating table 'sbtest5'...
Inserting 100000 records into 'sbtest5'
Creating table 'sbtest6'...
Inserting 100000 records into 'sbtest6'
Creating table 'sbtest7'...
Inserting 100000 records into 'sbtest7'
Creating table 'sbtest8'...
Inserting 100000 records into 'sbtest8'
Creating table 'sbtest9'...
Inserting 100000 records into 'sbtest9'
Creating table 'sbtest10'...
Inserting 100000 records into 'sbtest10' 也可使用parallel_prepare.lua脚本并行准备测试数据,线程数应该为运行表的倍数
shell> sysbench --test=./tests/db/parallel_prepare.lua --mysql-table-engine=myisam --oltp-table-size=100000
--num-threads=10 --mysql-db=test --mysql-user=root --oltp-tables-count=10 --mysql-password=oracle
--mysql-socket=/tmp/mysql.sock run sysbench 0.5: multi-threaded system evaluation benchmark Running the test with following options:
Number of threads: 10
Random number generator seed is 0 and will be ignored Threads started! thread prepare8
Creating table 'sbtest9'...
thread prepare1
Creating table 'sbtest2'...
thread prepare0
Creating table 'sbtest1'...
thread prepare5
Creating table 'sbtest6'...
thread prepare3
Creating table 'sbtest4'...
thread prepare6
Creating table 'sbtest7'...
thread prepare9
Creating table 'sbtest10'...
thread prepare4
Creating table 'sbtest5'...
thread prepare2
Creating table 'sbtest3'...
thread prepare7
Creating table 'sbtest8'...
Inserting 100000 records into 'sbtest1'
Inserting 100000 records into 'sbtest9'
Inserting 100000 records into 'sbtest8'
Inserting 100000 records into 'sbtest6'
Inserting 100000 records into 'sbtest7'
Inserting 100000 records into 'sbtest2'
Inserting 100000 records into 'sbtest10'
Inserting 100000 records into 'sbtest4'
Inserting 100000 records into 'sbtest5'
Inserting 100000 records into 'sbtest3'
OLTP test statistics:
queries performed:
read: 0
write: 380
other: 20
total: 400
transactions: 0 (0.00 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 380 (32.82 per sec.)
other operations: 20 (1.73 per sec.) Test execution summary:
total time: 11.5785s
total number of events: 10000
total time taken by event execution: 0.1422s
per-request statistics:
min: 0.00ms
avg: 0.01ms
max: 19.55ms
approx. 95 percentile: 0.00ms
http://ww3.sinaimg.cn/bmiddle/671c4d8egw1enjimkujv2g20b40b4b29.gif
Threads fairness:
events (avg/stddev): 1000.0000/2912.74
execution time (avg/stddev): 0.0142/0.04
  • run
  1. –oltp-tables-count
  2. –oltp-read-only 仅执行SELECT测试 on | off,默认为off
  3. –oltp-dist-type 随机数分布状态。uniform(均匀分布)、gauss(高斯分布)、special(特殊分布)
  4. –oltp-dist-pct 特殊分布的百分比值
  5. –oltp-dist-res 特殊分布的百分比
  6. –oltp-point-selects 单个事务中指定的selec查询个数
  7. –oltp-range-size 范围查询的范围大小,该值应比oltp-table-size小
  8. –oltp-simple-ranges 单个事务中指定的简单范围查询个数
  9. –oltp-sum-ranges 单个事务中指定的SUM范围查询个数
  10. –oltp-order-ranges 单个事务中指定的ORDER范围查询个数
  11. –oltp-distinct-ranges 单个事务中指定的DISTINCT范围查询个数
  12. –oltp-index-updates 单个事务中指定的使用索引更新的个数
  13. –oltp-non-index-updates 单个事务中指定的不使用索引更新的个数
shell> sysbench --test=./tests/db/oltp.lua --num_threads=10  --oltp-table-size=100000
--mysql-db=test --mysql-user=root --oltp-tables-count=10 --mysql-password=oracle run
sysbench 0.5: multi-threaded system evaluation benchmark Running the test with following options:
Number of threads: 10
Random number generator seed is 0 and will be ignored Threads started! OLTP test statistics:
queries performed:
read: 140028 -- 读总数
write: 40008 -- 写总数
other: 20004 -- 其他操作总数(SELECT、INSERT、UPDATE、DELETE之外的操作,例如COMMIT等)
total: 200040 -- 全部总数
transactions: 10002 (234.44 per sec.) -- 总事务数(每秒事务数)
deadlocks: 0 (0.00 per sec.) -- 发生死锁总数
read/write requests: 180036 (4219.99 per sec.) -- 读写总数(每秒读写次数)
other operations: 20004 (468.89 per sec.) -- 其他操作总数(每秒其他操作次数) Test execution summary:
total time: 42.6626s -- 总耗时
total number of events: 10002 -- 共发生多少事务数
total time taken by event execution: 426.3020s -- 所有事务耗时相加(不考虑并行因素)
per-request statistics:
min: 5.36ms -- 最小耗时
avg: 42.62ms -- 平均耗时
max: 183.44ms -- 最长耗时
approx. 95 percentile: 59.81ms -- 超过99%平均耗时 Threads fairness:
events (avg/stddev): 1000.2000/23.87
execution time (avg/stddev): 42.6302/0.01
  • clearnup
shell> sysbench --test=./tests/db/oltp.lua --oltp-table-size=100000 --mysql-db=test --mysql-user=root
--oltp-tables-count=10 --mysql-password=oracle cleanup
sysbench 0.5: multi-threaded system evaluation benchmark Dropping table 'sbtest1'...
Dropping table 'sbtest2'...
Dropping table 'sbtest3'...
Dropping table 'sbtest4'...
Dropping table 'sbtest5'...
Dropping table 'sbtest6'...
Dropping table 'sbtest7'...
Dropping table 'sbtest8'...
Dropping table 'sbtest9'...
Dropping table 'sbtest10'...