Home Database Mysql Tutorial 安装和使用percona-toolkit来辅助操作MySQL的基本教程_MySQL

安装和使用percona-toolkit来辅助操作MySQL的基本教程_MySQL

May 27, 2016 pm 01:45 PM

一、percona-toolkit简介
percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括:

  • 检查master和slave数据的一致性

  • 有效地对记录进行归档

  • 查找重复的索引

  • 对服务器信息进行汇总

  • 分析来自日志和tcpdump的查询

  • 当系统出问题的时候收集重要的系统信息

percona-toolkit源自Maatkit 和Aspersa工具,这两个工具是管理mysql的最有名的工具,现在Maatkit工具已经不维护了,请大家还是使用percona-toolkit吧!这些工具主要包括开发、性能、配置、监控、复制、系统、实用六大类,作为一个优秀的DBA,里面有的工具非常有用,如果能掌握并加以灵活应用,将能极大的提高工作效率。

二、percona-toolkit工具包安装
1.  软件包下载
访问http://www.percona.com/software/percona-toolkit/下载最新版本的Percona Toolkit 或者通过如下命令行来获取最新的版本:

wget percona.com/get/percona-toolkit.tar.gz
wget percona.com/get/percona-toolkit.rpm
Copy after login

我这里选择直接从网站上找到最新版本下载:

wget http://www.percona.com/redir/downloads/percona-toolkit/2.1.1/percona-toolkit-2.1.1-1.noarch.rpm
wget http://www.percona.com/redir/downloads/percona-toolkit/2.1.1/percona-toolkit-2.1.1.tar.gz
Copy after login

从http://pkgs.repoforge.org/perl-TermReadKey/下载最新的TermReadKey包

wget http://pkgs.repoforge.org/perl-TermReadKey/perl-TermReadKey-2.30-1.el5.rf.x86_64.rpm
Copy after login


2. 软件包安装
我的环境是Centos 5.5 64 BIT
A. percona-toolkit的rpm安装方式

rpm -ivh perl-TermReadKey-2.30-1.el5.rf.x86_64.rpm
rpm -ivh percona-toolkit-2.1.1-1.noarch.rpm
Copy after login

注意:需要安装Term::ReadKey 包,否则会报perl(Term::ReadKey) >= 2.10 is needed by percona-toolkit-2.1.1-1.noarch错误
B. percona-toolkit的编译安装方式
tar xzvf percona-toolkit-2.1.1.tar.gz
cd percona-toolkit-2.1.1
perl Makefile.PL
make
make test
make install

三、常用功能
1. pt-duplicate-key-checker
功能介绍:
功能为从mysql表中找出重复的索引和外键,这个工具会将重复的索引和外键都列出来,并生成了删除重复索引的语句,非常方便
用法介绍:
pt-duplicate-key-checker [OPTION...] [DSN]
包含比较多的选项,具体的可以通过命令pt-duplicate-key-checker --help来查看具体支持那些选项,我这里就不一一列举了。DNS为数据库或者表。
使用示例:
查看test数据库的重复索引和外键使用情况使用如下命令

pt-duplicate-key-checker --host=localhost --user=root --password=zhang@123 --databases=test
Copy after login


2. pt-online-schema-change
功能介绍:
功能为在alter操作更改表结构的时候不用锁定表,也就是说执行alter的时候不会阻塞写和读取操作,注意执行这个工具的时候必须做好备份,操作之前最好详细读一下官方文档http://www.percona.com/doc/percona-toolkit/2.1/pt-online-schema-change.html。
工作原理是创建一个和你要执行alter操作的表一样的空表结构,执行表结构修改,然后从原表中copy原始数据到表结构修改后的表,当数据copy完成以后就会将原表移走,用新表代替原表,默认动作是将原表drop掉。在copy数据的过程中,任何在原表的更新操作都会更新到新表,因为这个工具在会在原表上创建触发器,触发器会将在原表上更新的内容更新到新表。如果表中已经定义了触发器这个工具就不能工作了。
用法介绍:
pt-online-schema-change [OPTIONS] DSN
options可以自行查看help,DNS为你要操作的数据库和表。
这里有两个参数需要介绍一下:
--dry-run 这个参数不建立触发器,不拷贝数据,也不会替换原表。只是创建和更改新表。
--execute 这个参数的作用和前面工作原理的介绍的一样,会建立触发器,来保证最新变更的数据会影响至新表。注意:如果不加这个参数,这个工具会在执行一些检查后退出。这一举措是为了让使用这充分了解了这个工具的原理,同时阅读了官方文档。
使用示例:
在线更改表的的引擎,这个尤其在整理innodb表的时候非常有用,示例如下:

pt-online-schema-change --user=root --password=zhang@123 --host=localhost --lock-wait-time=120 
--alter="ENGINE=InnoDB" D=test,t=oss_pvinfo2 --execute
Copy after login

从下面的日志中可以看出它的执行过程:

Altering `test`.`oss_pvinfo2`...
Creating new table...
Created new table test._oss_pvinfo2_new OK.
Altering new table...
Altered `test`.`_oss_pvinfo2_new` OK.
Creating triggers...
Created triggers OK.
Copying approximately 995696 rows...
Copied rows OK.
Swapping tables...
Swapped original and new tables OK.
Dropping old table...
Dropped old table `test`.`_oss_pvinfo2_old` OK.
Dropping triggers...
Dropped triggers OK.
Successfully altered `test`.`oss_pvinfo2`.
Copy after login

在来一个范例,大表添加字段的,语句如下:

pt-online-schema-change --user=root --password=zhang@123 --host=localhost --lock-wait-time=120 
--alter="ADD COLUMN domain_id INT" D=test,t=oss_pvinfo2 --execute
Copy after login

3. pt-query-advisor
功能介绍:
根据一些规则分析查询语句,对可能的问题提出建议,这些评判规则大家可以看一下官网的链接:http://www.percona.com/doc/percona-toolkit/2.1/pt-query-advisor.html,这里就不详细列举了。那些查询语句可以来自慢查询文件、general日志文件或者使用pt-query-digest截获的查询语句。目前这个版本有bug,当日志文件非常大的时候会需要很长时间甚至进入死循环。
用法介绍:

pt-query-advisor /path/to/slow-query.log
pt-query-advisor --type genlog mysql.log
pt-query-digest --type tcpdump.txt --print --no-report | pt-query-advisor
Copy after login


使用示例:
分析一个语句的例子:

pt-query-advisor --query "select * from aaa"
Copy after login

分析general log中的查询语句的例子:

pt-query-advisor /data/dbdata/general.log
Copy after login

分析慢查询中的查询语句的例子:

pt-query-advisor /data/dbdata/localhost-slow.log
Copy after login


4. pt-show-grants
功能介绍:
规范化和打印mysql权限,让你在复制、比较mysql权限以及进行版本控制的时候更有效率!
用法介绍:
pt-show-grants [OPTION...] [DSN]
选项自行用help查看,DSN选项也请查看help,选项区分大小写。
使用示例:
查看指定mysql的所有用户权限:

pt-show-grants --host='localhost' --user='root' --password='zhang@123'
Copy after login

查看执行数据库的权限:

pt-show-grants --host='localhost' --user='root' --password='zhang@123' --database='hostsops'
Copy after login

查看每个用户权限生成revoke收回权限的语句:

pt-show-grants --host='localhost' --user='root' --password='zhang@123' --revoke
Copy after login


5. pt-upgrade
功能介绍:
在多台服务器上执行查询,并比较有什么不同!这在升级服务器的时候非常有用,可以先安装并导数据到新的服务器上,然后使用这个工具跑一下sql看看有什么不同,可以找出不同版本之间的差异。
用法介绍:
pt-upgrade [OPTION...] DSN [DSN...] [FILE]
比较文件中每一个查询语句在两个主机上执行的结果,并检查在每个服务器上执行的结果、错误和警告。
使用示例:
只查看某个sql在两个服务器的运行结果范例:

pt-upgrade h='localhost' h=192.168.3.92 --user=root --password=zhang@123 --query="select * from user_data.
collect_data limit 5"
Copy after login

查看文件中的对应sql在两个服务器的运行结果范例:

pt-upgrade h='localhost' h=192.168.3.92 --user=root --password=zhang@123 aaa.sql
Copy after login

查看慢查询中的对应的查询SQL在两个服务器的运行结果范例:

pt-upgrade h='localhost' h=192.168.3.92 --user=root --password=zhang@123 slow.log
Copy after login

此外还可以执行compare的类型,主要包含三个query_times,results,warnings,比如下面的例子,只比较sql的执行时间

pt-upgrade h=192.168.3.91 h=192.168.3.92 --user=root --password=zhang@123 --query="select * from user_data.
collect_data" --compare query_times
Copy after login

 以上就是安装和使用percona-toolkit来辅助操作MySQL的基本教程_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to view database password in Navicat for MariaDB? How to view database password in Navicat for MariaDB? Apr 08, 2025 pm 09:18 PM

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).

How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to execute sql in navicat How to execute sql in navicat Apr 08, 2025 pm 11:42 PM

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

See all articles