前陣子遷移zabbix到tokudb,整理部分操作筆記到這篇博文。 如果轉載,請註明博文來源: www.cnblogs.com/xinysu/ ,版權歸 博客園 蘇家小蘿蔔 所有。望各位支持! 如果轉載,請註明博文來源: www.cnblogs.com/xinysu/ ,版權歸 博客園 蘇家小蘿蔔 ...
前陣子遷移zabbix到tokudb,整理部分操作筆記到這篇博文。
如果轉載,請註明博文來源: www.cnblogs.com/xinysu/ ,版權歸 博客園 蘇家小蘿蔔 所有。望各位支持!
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 找個最新版本安裝就可以了 wget https://github.com/jemalloc/jemalloc/releases/download/5.0.0/jemalloc-5.0.0.tar.bz2 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/data2.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/enabledecho 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.rpm2.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- 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 問題分析
https://www.percona.com/doc/percona-server/5.7/tokudb/tokudb_troubleshooting.html 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
- 實例備份
-
- set tokudb_backup_dir='/data/backup/pmysql3330';
- 執行這個指令則開始備份,備份過程中,支持讀寫,
- 備份出錯驗證
- 表格備份
- 實例備份後還原
-
- tokudbback沒有特定的函數進行數據恢復,可以使用cp或者sync到 數據目錄,或者直接使用備份目錄
- 註意許可權修改