MySQL_第三方數據庫引擎_tokudb

    前陣子遷移zabbix到tokudb,整理部分操做筆記到這篇博文。html

 



 
    若是轉載,請註明博文來源:  www.cnblogs.com/xinysu/   ,版權歸 博客園 蘇家小蘿蔔 全部。望各位支持!
 


 1 tokudb引擎介紹

  1. 特性:高壓縮,可支持多個彙集索引,支持ACID、MVCC,使用 Fractal Tree 索引
  2. 優勢:
    • 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

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引擎須要啓動透明大頁,更好的提供內存管理。node

[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

 

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.rpm
rpm -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

 
使用 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.

 
安裝生產後,能夠在
 
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
 
  1. tokudb.environment:tokudb贏取的環境變量存儲文件,像建立時間、當前版本號
  2. tokudb.rollback:每個tokudb的事務,都有本身的回滾日誌,具體回滾日誌分配相關信息記錄在這裏
  3. tokudb.directory:映射文件夾的名字

3.2 tokudb文件管理

能夠統一把tokudb的數據文件存儲在某個文件夾下,參數是 tokudb_data_dir
能夠統一把tokudb的臨時文件存儲在某個文件夾下,參數是 tokudb_tmp_dir
能夠統一把tokudb的日誌文件存儲在某個文件夾下,參數是 tokudb_log_dir
  • 如何遷移:
    1. 關閉數據庫服務
    2. 配置文件中添加或者修改 tokudb_data_dir、tokudb_tmp_dir、、、
    3. 建立 tokudb 文件的存儲目錄
    4. 移動文件
      1. 移動 *.tokudb 文件 跟 __tokudb_lock_dont_delete_me_data 到新的存儲目錄
      2. 移動 __tokudb_lock_dont_delete_me_temp 到新的 存儲目錄
      3. 移動 到新的 log*.tokulog* files and your __tokudb_lock_dont_delete_me_logs file 存儲目錄
    5. 啓動數據庫服務器
    6. 查看文件的存放位置
      1. 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
相關文章
相關標籤/搜索