MySQL_第三方数据库引擎_tokudb
前阵子迁移zabbix到tokudb,整理部分操作笔记到这篇博文。
1 tokudb引擎介绍
- 特性:高压缩,可支持多个聚集索引,支持ACID、MVCC,使用 Fractal Tree 索引
- 优点:
-
- fractal tree 可以再了解下,对随机IO有非常大的改善作用
- fractal tree 可以快速插入及删除,在随机IO方面的性能比 B-tree的性能更优
- 高压缩,官网给出最大25倍
- 对数据及索引进行压缩,根据数据的可压缩性,官网给出的最大压缩比是 25 倍
- 在线索引创建,创建期间,insert delete的DML支持,update呢 ?
- 在线列增加删除重命名,修改期间,insert delete的DML支持,update呢 ?
- 在线备份
- 多个聚集索引
- 无io读复制
- TokuDB slave可以配置,让来自master修改可以最小化。通过记录fractal tree索引:
- Insert/update/delete操作可以控制取消read-modify-write的行为,然后注入消息到合适的fractal tree。
- Update/delete操作可以配置取消需要io的一致性检查。
- 为了使用使用无io读复制,服务需要配置:
- 在replication master:
- 设置为binlog行模式:BINLOG_FORMAT=ROW
- 在replication slave:
- Slave必须为只读:read_only=1
- 取消一致性检查:tokudb_rpl_unique_checks=0
- 关闭查找(read-modify-write) :tokudb_rpl_lookup_rows=0
- 可以在一个或者多个slave上配置。只要master使用了基于行的复制,优化在tokudb slave就可用。也就是说如果master使用innodb或者myisam表也是可用的。
- 无损化,少碎片
- 4Mb为单位进行存储
- 快速恢复
- 恢复时长少于1min
- fractal tree 可以再了解下,对随机IO有非常大的改善作用
2 Tokudb安装
2.1 percona mysql安装
1 下载最新percona mysql二进制包 2 解压 tar zvxf Percona-Server-5.7.17-13-Linux.x86_64.ssl101.tar.gz 3 软连接 ln -s /opt/percona/Percona-Server-5.7.17-13-Linux.x86_64.ssl101 /usr/local/pmysql 4 建立用户及用户组 groupadd mysql useradd -g mysql -s /sbin/nologin -d /usr/local/mysql/ -M mysql 5 建立文件夹 mkdir -p /data/mysql/pmysql3330/{data,tmp,logs} 6 数据库配置文件 pmysql3330.cnf 7 授权 [root@sutest242 percona]# chown -R mysql:mysql /usr/local/pmysql/ [root@sutest242 percona]# chown -R mysql:mysql /opt/percona/Percona-Server-5.7.17-13-Linux.x86_64.ssl101 [root@sutest242 percona]# chown -R mysql:mysql /data/mysql/pmysql3330/ 8 看是否需要配置环境变量 vim /etc/profile PATH=$PATH:/usr/local/pmysql/bin source profile 9 初始化实例 /usr/local/pmysql/bin/mysqld --defaults-file=/data/mysql/pmysql3330.cnf --initialize 10 检查是否安装有问题 vim /data/mysql/pmysql3330/data/error.log 11 启动数据库实例 /usr/local/pmysql/bin/mysqld --defaults-file=/data/mysql/pmysql3330.cnf & 12 修改密码 cat /data/mysql/pmysql3330/data/error.log | grep password /usr/local/pmysql/bin/mysql --socket=/tmp/pmysql3330.sock -uroot -p alter user root@localhost idnetified by '******'; flush privileges;
2.2 安装 jemalloc,管理内存
https://github.com/jemalloc/jemalloc/releases 找个最新版本安装就可以了
tar xjf jemalloc-4.2.1.tar.bz2
cd jemalloc-4.2.1
./configure
make && make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
解压过程中如果报错如下,请安装 bzip2 包 :yum install -y bzip2
[root@sutest242 percona]# tar -xjf jemalloc-4.2.1.tar.bz2
tar (child): bzip2: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
安装成功后,在数据库配置文件中的mysqld_safe模块,添加引用libjemalloc.so,记得需要先找下这个文件的实际位置,然后cp或者ls一份到/usr/lib64目录下。
在数据库配置文件中添加引用
[mysqld_safe]
malloc-lib=/usr/lib64/libjemalloc.so.1
注意,数据库关闭后,需要用mysqld_safe启动服务,使用 jemalloc 管理内存。
[root@sutest242 jemalloc-5.0.0]# /usr/local/pmysql/bin/mysqladmin --socket=/tmp/pmysql3330.sock -uroot -p shutdown
Enter password:
[root@sutest242 jemalloc-5.0.0]# ps axu | grep pmysql
root 12418 0.0 0.0 112648 964 pts/1 S+ 10:29 0:00 grep --color=auto pmysql
[root@sutest242 jemalloc-5.0.0]# cd /usr/local/pmysql/
[root@sutest242 pmysql]# ./bin/mysqld_safe --defaults-file=/data/mysql/pmysql3330.cnf &
[1] 13745
[root@sutest242 pmysql]# mysqld_safe Adding '/usr/lib/libjemalloc.so.1' to LD_PRELOAD for mysqld
2017-06-24T02:31:06.679622Z mysqld_safe Logging to '/data/mysql/pmysql3330/data/error.log'.
2017-06-24T02:31:06.685066Z mysqld_safe Logging to '/data/mysql/pmysql3330/data/error.log'.
2017-06-24T02:31:06.788240Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/pmysql3330/data
2.3 修改transparent_hugepage
tokudb引擎需要启动透明大页,更好的提供内存管理。
[root@sutest244 percona5718]# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
需要修改为never,如果不修改,使用tokudb引擎的时候,会在error.log中报错
Transparent huge pages are enabled, according to /sys/kernel/mm/redhat_transparent_hugepage/enabled
Transparent huge pages are enabled, according to /sys/kernel/mm/transparent_hugepage/enabled
配置如下:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
echo never > /sys/kernel/mm/transparent_hugepage/defrag
2.4 安装tokudb引擎跟percona client
官网下载后安装。
rpm -ivhPercona-Server-shared-57-5.7.17-13.1.el6.x86_64.rpm
rpm -ivhPercona-Server-client-57-5.7.17-13.1.el7.x86_64.rpm
rpm -ivhPercona-Server-server-57-5.7.17-13.1.el6.x86_64.rpmrpm -ivh --force --nodeps Percona-Server-tokudb-57-5.7.17-13.1.el7.x86_64.rpm
2.5 启用tokudb存储引擎
安装tokudb引擎后,会提示启动指令语法如下:
* 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_tokudb_admin --enable -u <mysql_admin_user> -p[mysql_admin_pass] [-S <socket>] [-h <host> -P <port>]
* See http://www.percona.com/doc/percona-server/5.7/tokudb/tokudb_installation.html for more installation details
* See http://www.percona.com/doc/percona-server/5.7/tokudb/tokudb_intro.html for an introduction to TokuDB
* Run the following script to enable the TokuDB storage engine in Percona Server:
ps_tokudb_admin --enable -u <mysql_admin_user> -p[mysql_admin_pass] [-S <socket>] [-h <host> -P <port>]
* See http://www.percona.com/doc/percona-server/5.7/tokudb/tokudb_installation.html for more installation details
* See http://www.percona.com/doc/percona-server/5.7/tokudb/tokudb_intro.html for an introduction to TokuDB
使用 ps_tokudb_admin指令 :
ps_tokudb_admin --enable -uroot -p --sock=/tmp/pmysql.sock --defaults-file=/data/mysql/mysql3306.cnf如果正常,则是以下显示,不正常的话,看下前面的安装步骤是不是少了
Checking if Percona server is running with jemalloc enabled...
>> Percona server is running with jemalloc enabled.
Checking transparent huge pages status on the system...
>> Transparent huge pages are currently disabled on the system.
Checking if thp-setting=never option is already set in config file...
>> Option thp-setting=never is not set in the config file.
>> (needed only if THP is not disabled permanently on the system)
Checking TokuDB plugin status...
>> TokuDB plugin is not installed.
Adding thp-setting=never option into /etc/mysql/my.cnf
>> Successfuly added thp-setting=never option into /etc/mysql/my.cnf
Installing TokuDB engine...
>> Successfuly installed TokuDB plugin.
>> Percona server is running with jemalloc enabled.
Checking transparent huge pages status on the system...
>> Transparent huge pages are currently disabled on the system.
Checking if thp-setting=never option is already set in config file...
>> Option thp-setting=never is not set in the config file.
>> (needed only if THP is not disabled permanently on the system)
Checking TokuDB plugin status...
>> TokuDB plugin is not installed.
Adding thp-setting=never option into /etc/mysql/my.cnf
>> Successfuly added thp-setting=never option into /etc/mysql/my.cnf
Installing TokuDB engine...
>> Successfuly installed TokuDB plugin.
安装生产后,可以在
SHOW PLUGINS;
SHOW ENGINES;
TokuDB Version
root@localhost:pmysql3330.sock 10:42:50 [(none)]>SELECT @@tokudb_version;
+------------------+
| @@tokudb_version |
+------------------+
| 5.7.17-13 |
+------------------+
1 row in set (0.00 sec)
3 文件管理及分析
3.1 安装tokudb引擎后的新增文件
在数据目录中,新增了8个文件
[root@sutest242 data]# pwd
/data/mysql/pmysql3330/data
[root@sutest242 data]# ls -lh | grep tokudb
-rw-r-----. 1 mysql mysql 16K Jun 24 10:42 tokudb.directory
-rw-r-----. 1 mysql mysql 16K Jun 24 10:42 tokudb.environment
-rw-------. 1 mysql mysql 0 Jun 24 10:42 __tokudb_lock_dont_delete_me_data
-rw-------. 1 mysql mysql 0 Jun 24 10:42 __tokudb_lock_dont_delete_me_environment
-rw-------. 1 mysql mysql 0 Jun 24 10:42 __tokudb_lock_dont_delete_me_logs
-rw-------. 1 mysql mysql 0 Jun 24 10:42 __tokudb_lock_dont_delete_me_recovery
-rw-------. 1 mysql mysql 0 Jun 24 10:42 __tokudb_lock_dont_delete_me_temp
-rw-r-----. 1 mysql mysql 16K Jun 24 10:42 tokudb.rollback
- tokudb.environment:tokudb赢取的环境变量存储文件,像创建时间、当前版本号
- tokudb.rollback:每一个tokudb的事务,都有自己的回滚日志,具体回滚日志分配相关信息记录在这里
- tokudb.directory:映射文件夹的名字
3.2 tokudb文件管理
可以统一把tokudb的数据文件存储在某个文件夹下,参数是 tokudb_data_dir
可以统一把tokudb的临时文件存储在某个文件夹下,参数是 tokudb_tmp_dir
可以统一把tokudb的日志文件存储在某个文件夹下,参数是 tokudb_log_dir
- 如何迁移:
- 关闭数据库服务
- 配置文件中添加或者修改 tokudb_data_dir、tokudb_tmp_dir、、、
- 创建 tokudb 文件的存储目录
- 移动文件
- 移动 *.tokudb 文件 跟 __tokudb_lock_dont_delete_me_data 到新的存储目录
- 移动 __tokudb_lock_dont_delete_me_temp 到新的 存储目录
- 移动 到新的 log*.tokulog* files and your __tokudb_lock_dont_delete_me_logs file 存储目录
- 启动数据库服务器
- 查看文件的存放位置
- SELECT dictionary_name, internal_file_name FROM INFORMATION_SCHEMA.TokuDB_file_map;
3.3 问题分析
information_schema库里边的相应系统视图分析
root@localhost:pmysql3330.sock 17:15:33 [information_schema]>show tables like '%tokudb%';
+-----------------------------------------+
| Tables_in_information_schema (%tokudb%) |
+-----------------------------------------+
| TokuDB_fractal_tree_block_map |
| TokuDB_lock_waits |
| TokuDB_trx |
| TokuDB_file_map |
| TokuDB_locks |
| TokuDB_fractal_tree_info |
| TokuDB_background_job_status |
+-----------------------------------------+
7 rows in set (0.00 sec)
4 备份
tokudb如何备份呢?
单表也可以使用mysqldump。全库呢? tokudb引擎全库备份,官方版本是使用 tokudb_backup 引擎来进行,支持在线热备。也就是,需要另外安装这个引擎,安装相对简单,使用 ps_tokudb_admin + 重启数据库服务预加载引擎 + ps_tokudb_admin。
4.1 安装备份引擎
首先,确保数据库是启动正常状态,执行ps_tokudb_admin,如果 数据库配置文件 cnf不是在 /etc/my.cnf,需要手动指定 --defaults-file,避免无法找到配置文件写入 preload-hotbackup。
[root@sutest242 pmysql]# ps_tokudb_admin --enable-backup --socket=/tmp/pmysql3330.sock -uroot -p*** --defaults-file=/data/mysql/pmysql3330.cnf
Checking SELinux status...
INFO: SELinux is in permissive mode.
Checking if Percona Server is running with jemalloc enabled...
INFO: Percona Server is running with jemalloc enabled.
Checking transparent huge pages status on the system...
INFO: Transparent huge pages are currently disabled on the system.
Checking if thp-setting=never option is already set in config file...
INFO: Option thp-setting=never is not set in the config file.
(needed only if THP is not disabled permanently on the system)
Checking if preload-hotbackup option is already set in config file...
INFO: Option preload-hotbackup is not set in the config file.
Checking TokuDB engine plugin status...
INFO: TokuDB engine plugin is installed.
Checking TokuBackup plugin status...
INFO: TokuBackup plugin is not installed.
Adding preload-hotbackup option into /data/mysql/pmysql3330.cnf
INFO: Successfully added preload-hotbackup option into /data/mysql/pmysql3330.cnf
PLEASE RESTART MYSQL SERVICE AND RUN THIS SCRIPT AGAIN TO FINISH INSTALLATION!
这个时候,可以看到提示:Successfully added preload-hotbackup option into /data/mysql/pmysql3330.cnf,查看配置文件,在 mysqld_safe 配置中,添加多了一行 配置 :preload-hotbackup。
[mysqld_safe]
preload-hotbackup
malloc-lib=/usr/lib/libjemalloc.so.1
重启数据库服务,使用 mysqladmin关闭,mysqld_safe 启动
[root@sutest242 pmysql]# /usr/local/pmysql/bin/mysqladmin --socket=/tmp/pmysql3330.sock -uroot -p shutdown
Enter password:
2017-06-26T03:48:20.192200Z mysqld_safe mysqld from pid file /data/mysql/pmysql3330/data/mysql.pid ended
[1]+ Done ./bin/mysqld_safe --defaults-file=/data/mysql/pmysql3330.cnf
[root@sutest242 pmysql]# ./bin/mysqld_safe --defaults-file=/data/mysql/pmysql3330.cnf &
[1] 10069
[root@sutest242 pmysql]# mysqld_safe Adding '/usr/lib/libjemalloc.so.1' to LD_PRELOAD for mysqld
mysqld_safe Adding '/usr/lib64/libHotBackup.so' to LD_PRELOAD for mysqld
2017-06-26T03:50:32.005677Z mysqld_safe Logging to '/data/mysql/pmysql3330/data/error.log'.
2017-06-26T03:50:32.013350Z mysqld_safe Logging to '/data/mysql/pmysql3330/data/error.log'.
2017-06-26T03:50:32.099577Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/pmysql3330/data
这里有个地方注意下, libHotBackup.so 记得要cp到 /usr/lib 或者 /usr/lib64的目录下,不然mysqld_safe启动数据库的时候,会报错如下:
mysqld_safe Adding '/usr/lib/libjemalloc.so.1' to LD_PRELOAD for mysqld
mysqld_safe Adding '/opt/percona/Percona-Server-5.7.17-13-Linux.x86_64.ssl101/lib/libHotBackup.so' to LD_PRELOAD for mysqld
mysqld_safe ld_preload libraries can only be loaded from system directories (/usr/lib64, /usr/lib, /usr/local/pmysql/lib)
这种情况下,它检查到 安装目录下有 libHotBackup.so 引擎文件,会使用这个加载,需要 把这个文件拷贝到 /usr/lib64 或者 /usr/lib,再重新加载这个文件,然后再次执行 mysqld_saft启动服务。
cp /opt/percona/Percona-Server-5.7.17-13-Linux.x86_64.ssl101/lib/libHotBackup.so /usr/lib/libHotBackup.so
mv /opt/percona/Percona-Server-5.7.17-13-Linux.x86_64.ssl101/lib/libHotBackup.so /opt/percona/Percona-Server-5.7.17-13-Linux.x86_64.ssl101/lib/libHotBackup.so.bak
数据库启动后,安装备份引擎
[root@sutest242 pmysql]# ps_tokudb_admin --enable-backup --socket=/tmp/pmysql3330.sock -uroot -p
Enter password:
Checking SELinux status...
INFO: SELinux is in permissive mode.
Checking if Percona Server is running with jemalloc enabled...
INFO: Percona Server is running with jemalloc enabled.
Checking transparent huge pages status on the system...
INFO: Transparent huge pages are currently disabled on the system.
Checking if thp-setting=never option is already set in config file...
INFO: Option thp-setting=never is not set in the config file.
(needed only if THP is not disabled permanently on the system)
Checking if preload-hotbackup option is already set in config file...
INFO: Option preload-hotbackup is set in the config file.
Checking TokuDB engine plugin status...
INFO: TokuDB engine plugin is installed.
Checking TokuBackup plugin status...
INFO: TokuBackup plugin is not installed.
Checking if Percona Server is running with libHotBackup.so preloaded...
INFO: Percona Server is running with libHotBackup.so preloaded.
Adding thp-setting=never option into /etc/my.cnf
INFO: Successfully added thp-setting=never option into /etc/my.cnf
Installing TokuBackup plugin...
INFO: Successfully installed TokuBackup plugin.
检查
root@localhost:pmysql3330.sock 14:34:21 [(none)]>show plugins;
......
| TokuDB | ACTIVE | STORAGE ENGINE | ha_tokudb.so | GPL |
| TokuDB_background_job_status | ACTIVE | INFORMATION SCHEMA | ha_tokudb.so | GPL |
| TokuDB_file_map | ACTIVE | INFORMATION SCHEMA | ha_tokudb.so | GPL |
| TokuDB_fractal_tree_block_map | ACTIVE | INFORMATION SCHEMA | ha_tokudb.so | GPL |
| TokuDB_fractal_tree_info | ACTIVE | INFORMATION SCHEMA | ha_tokudb.so | GPL |
| TokuDB_locks | ACTIVE | INFORMATION SCHEMA | ha_tokudb.so | GPL |
| TokuDB_lock_waits | ACTIVE | INFORMATION SCHEMA | ha_tokudb.so | GPL |
| TokuDB_trx | ACTIVE | INFORMATION SCHEMA | ha_tokudb.so | GPL |
| tokudb_backup | ACTIVE | DAEMON | tokudb_backup.so | GPL |
+-------------------------------+----------+--------------------+------------------+---------+
59 rows in set (0.00 sec)
4.2 备份tokudb
实例备份
- 配置前提
-
- 如果实例含有innodb引擎的表格,需要关闭 innodb异步io的变量 innodb_use_native_aio,避免出现数据不一致,无法恢复的备份。这个变量只能在配置文件中添加,属于readonly变量
- 建立备份文件夹,这个文件夹必须是为空、授权备份文件夹可写,因为备份的时候,需要写入及创建文件夹,所以对其所在的文件夹需要有权限
- 注意,tokudb_back对本身引擎的表格,支持对单独存储的 tokudb_data_dir\ tokudb_log_dir \binary log备份,但是,不支持不支持不支持 innodb或者myisam引擎的表格另外存储,也就是 所有的InnoDB存储引擎,MyISAM,及其他文件必须在MySQL datadir
- tokubackup不备份MySQL的配置文件(S)
- tokubackup不支持增量备份
- 配置
-
- 在配置文件中设置 innodb_use_native_aio = OFF,重启数据库实例,这里注意,启动数据库需要到 /usr/local/pmysql 目录下,执行 ./bin/mysqld_safe --defaults-file=/data/mysql/pmysql3330.cnf
- 建立文件夹 /data/backup/pmysql3330 ,并授权
-
- chown -R mysql:mysql /data/backup
备份分为2中,一种是实例备份,一种是指定备份,目前tokudb_backup仅支持整个实例备份,支持混合引擎,指定备份,目前采用mysqldump方法。
- 实例备份
-
- set tokudb_backup_dir='/data/backup/pmysql3330';
- 执行这个指令则开始备份,备份过程中,支持读写,
- 备份出错验证
root@localhost:pmysql3330.sock 10:57:13 [(none)]>select @@tokudb_backup_last_error;
+----------------------------+
| @@tokudb_backup_last_error |
+----------------------------+
| 13 |
+----------------------------+
1 row in set (0.00 sec)
root@localhost:pmysql3330.sock 10:57:30 [(none)]>select @@tokudb_backup_last_error_string;
+---------------------------------------------------+
| @@tokudb_backup_last_error_string |
+---------------------------------------------------+
| tokudb backup couldn't create needed directories. |
+---------------------------------------------------+
1 row in set (0.00 sec)
- 表格备份
[root@sutest242 local]# /usr/local/pmysql/bin/mysqldump --socket=/tmp/pmysql3330.sock -uroot -p --set-gtid-purged=OFF --databases toku --tables tbback_toku > /tmp/tbback_toku.sql
Enter password:
- 实例备份后还原
-
- tokudbback没有特定的函数进行数据恢复,可以使用cp或者sync到 数据目录,或者直接使用备份目录
- 注意权限修改
rsync -avrP /data/backup/ /var/lib/mysql/
chown -R mysql:mysql /var/lib/mysql
如果转载,请注明博文来源: www.cnblogs.com/xinysu/ ,版权归 博客园 苏家小萝卜 所有。望各位支持!