文章目录
一、环境部署
1.1 OS配置
1.1.1 关闭防火墙
systemctl stop firewalld.service #停止防火墙
systemctl disable firewalld.service #禁止开机启动
1.1.2 关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
cat /etc/sysconfig/selinux
1.1.3 关闭大页
echo never > /sys/kernel/mm/transparent_hugepage/enabled
1.2 数据库部署
tar xf Percona-Server-8.0.21-12-r7ddfdfe-el7-x86_64-bundle.tar
yum localinstall percona-server-*
...
Installing : percona-server-server-8.0.21-12.2.el7.x86_64 4/9
Percona Server is distributed with several useful UDF (User Defined Function) from Percona Toolkit.
Run the following commands to create these functions:
mysql -e "CREATE FUNCTION fnv1a_64 RETURNS INTEGER SONAME 'libfnv1a_udf.so'"
mysql -e "CREATE FUNCTION fnv_64 RETURNS INTEGER SONAME 'libfnv_udf.so'"
mysql -e "CREATE FUNCTION murmur_hash RETURNS INTEGER SONAME 'libmurmur_udf.so'"
See http://www.percona.com/doc/percona-server/8.0/management/udf_percona_toolkit.html for more details
Installing : percona-server-rocksdb-8.0.21-12.2.el7.x86_64 5/9
* This release of Percona Server is distributed with RocksDB storage engine.
* Run the following script to enable the RocksDB storage engine in Percona Server:
ps-admin --enable-rocksdb -u <mysql_admin_user> -p[mysql_admin_pass] [-S <socket>] [-h <host> -P <port>]
Installing : percona-server-test-8.0.21-12.2.el7.x86_64 6/9
Installing : percona-server-tokudb-8.0.21-12.2.el7.x86_64 7/9
* This release of Percona Server is distributed with TokuDB storage engine.
* Run the following script to enable the TokuDB storage engine in Percona Server:
ps-admin --enable-tokudb -u <mysql_admin_user> -p[mysql_admin_pass] [-S <socket>] [-h <host> -P <port>]
...
systemctl start mysqld
cat /data/mysql/logs/error.log | grep 'temporary password'
1.3 启用tokudb和rocksdb
[root@zijie lib]# ps-admin --enable-tokudb -uroot -pZIjie@97125
Checking SELinux status...
INFO: SELinux is disabled.
Checking if TokuDB plugin is available for installation ...
INFO: ha_tokudb.so library for TokuDB found at /usr/lib64/mysql/plugin/ha_tokudb.so.
Checking location of jemalloc library ...
INFO: jemalloc library needed for TokuDB found at /usr/lib64/libjemalloc.so.1
Checking transparent huge pages status on the system...
INFO: Transparent huge pages are currently disabled on the system.
Checking if THP_SETTING variable is set to never or madvise in /etc/sysconfig/mysql...
INFO: Variable THP_SETTING is set in /etc/sysconfig/mysql.
Checking if LD_PRELOAD variable is set for libjemalloc.so.1 in /etc/sysconfig/mysql...
INFO: Variable LD_PRELOAD for libjemalloc.so.1 is set in /etc/sysconfig/mysql.
Checking TokuDB engine plugin status...
INFO: TokuDB engine plugin is not installed.
Installing TokuDB engine...
INFO: Successfully installed TokuDB engine plugin.
[root@zijie lib]# ps-admin --enable-rocksdb -uroot -pZIjie@97125
Checking if RocksDB plugin is available for installation ...
INFO: ha_rocksdb.so library for RocksDB found at /usr/lib64/mysql/plugin/ha_rocksdb.so.
Checking RocksDB engine plugin status...
INFO: RocksDB engine plugin is not installed.
Installing RocksDB engine...
INFO: Successfully installed RocksDB engine plugin.
root@mysql 17:39: [(none)]> show engines;
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| TokuDB | YES | Percona TokuDB Storage Engine with Fractal Tree(tm) Technology | YES | YES | YES |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| ROCKSDB | YES | RocksDB storage engine | YES | YES | YES |
| InnoDB | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
11 rows in set (0.00 sec)
二、引擎对比
2.1 TokuDB
TokuDB 是 一个支持高并发插入存储引擎,适用 MySQL 和 MariaDB。其独特的 “分形树”索引:提供了将“随机读写” 变成了 “顺序读写” 的功能,从而拥有了高insert性能;使用的块更大,使数据得以更好的压缩。
优点
-
高压缩比,默认使用zlib进行压缩,尤其是对字符串(varchar,text等)类型有非常高的压缩比,比较适合存储日志、原始数据等。官方宣称可以达到1:12。
-
在线添加索引,不影响读写操作
-
HCADER 特性,支持在线字段增加、删除、扩展、重命名操作,(瞬间或秒级完成)
-
支持完整的ACID特性和事务机制
-
非常快的写入性能, Fractal-tree在事务实现上有优势,无undo log,官方称至少比innodb高9倍。
-
支持show processlist 进度查看
-
数据量可以扩展到几个TB;
-
不会产生索引碎片;
缺点
-
不支持外键(foreign key)功能,如果表有外键,切换到 TokuDB引擎后,此约束将被忽略。
-
TokuDB 不适大量读取的场景,因为压缩解压缩的原因。CPU占用会高2-3倍,但由于压缩后空间小,IO开销低,平均响应时间大概是2倍左右。
-
online ddl 对text,blob等类型的字段不适用
-
没有完善的热备工具,只能通过mysqldump进行逻辑备份
适用场景
- 访问频率不高的数据或历史数据归档
- 数据表非常大并且时不时还需要进行DDL操作
2.2 RocksDB
RocksDB是由 Facebook 基于 LevelDB 开发的一款提供键值存储与读写功能的 LSM-tree 架构引擎。用户写入的键值对会先写入磁盘上的 WAL (Write Ahead Log),然后再写入内存中的跳表(SkipList,这部分结构又被称作 MemTable)。LSM-tree 引擎由于将用户的随机修改(插入)转化为了对 WAL 文件的顺序写,因此具有比 B 树类存储引擎更高的写吞吐。内存中的数据达到一定阈值后,会刷到磁盘上生成 SST 文件 (Sorted String Table),SST 又分为多层(默认至多 6 层),每一层的数据达到一定阈值后会挑选一部分 SST 合并到下一层,每一层的数据是上一层的 10 倍(因此 90% 的数据存储在最后一层)。RocksDB 允许用户创建多个 ColumnFamily ,这些 ColumnFamily 各自拥有独立的内存跳表以及 SST 文件,但是共享同一个 WAL 文件,这样的好处是可以根据应用特点为不同的 ColumnFamily 选择不同的配置,但是又没有增加对 WAL 的写次数。
优点
- innodb空间浪费, B tree分裂导致page内有较多空闲,page利用率不高。innodb现有的压缩效率也不高,压缩以block为单位,也会造成浪费。
- 写入放大:innodb 更新以页为单位,最坏的情况更新N行会更新N个页。RocksDB append only方式 另外,innodb开启double write也会增加写入。
- RocksDB对齐开销小:SST file (默认2MB)需要对齐,但远大于4k, RocksDB_block_size(默认4k) 不需要对齐,因此对齐浪费空间较少
- RocksDB索引前缀相同值压缩存储,节省空间
- RocksDB占总数据量90%的最底层数据,行内不需要存储系统列seqid (innodb聚簇索引列包含trxid,roll_ptr等信息)
缺点
- 大value的场景不友好
- 不适于大规模数据的存取
适用场景
- 对写性能要求很高,同时有较大内存来缓存SST块以提供快速读的场景;
- SSD等对写放大比较敏感以及磁盘等对随机写比较敏感的场景;
- 需要变长kv存储的场景;
- 小规模元数据的存取;