使用percona-toolkit操作MySQL的实用命令小结_MySQL
1.pt-archiver
功能介绍:
将mysql数据库中表的记录归档到另外一个表或者文件
用法介绍:
pt-archiver [OPTION...] --source DSN --where WHERE
这个工具只是归档旧的数据,不会对线上数据的OLTP查询造成太大影响,你可以将数据插入另外一台服务器的其他表中,也可以写入到一个文件中,方便使用load data infile命令导入数据。另外你还可以用它来执行delete操作。这个工具默认的会删除源中的数据。使用的时候请注意。
使用示例:
范例1:将192.168.3.135上的sanmao库的oss_log表id小于100000的记录转移到192.168.3.92上的sanmao库,并归档到oss_log_archive_20120605.log文件中:
pt-archiver --source h=192.168.3.135,D=sanmao,t=oss_log --user=root --password=zhang@123 --dest
h=192.168.3.92,D=sanmao,t=oss_log --file '/var/log/oss_log_archive_20120605.log' --where "id<=100000" --commit-each
范例2:将192.168.3.135上的sanmao库的oss_log小于160000的记录归档到oss_log_archive_20120607.log文件中:
pt-archiver --source h=192.168.3.135,D=sanmao,t=oss_log --user=root --password=zhang@123 --file '/var/log/oss_log_archive_20120607.log' --where "id<=160000" --commit-each
范例3:删除192.168.3.135上的sanmao库的oss_log表中id小于167050的记录:
pt-archiver --source h=192.168.3.135,D=sanmao,t=oss_log --user=root --password=zhang@123 --purge --where 'id<=167050'
注意:如果是字符集是utf8的话,需要在my.cnf中的[client]下面添加default-character-set = utf8,否则导出的文件内容中文会乱码。
2.pt-find
功能介绍:
查找mysql表并执行指定的命令,和gnu的find命令类似。
用法介绍:
pt-find [OPTION...] [DATABASE...]
默认动作是打印数据库名和表名
使用示例:
范例1:查找192.168.3.135中1天以前创建的InnoDB的表 ,并打印。
pt-find --ctime +1 --host=192.168.3.135 --engine InnoDB --user=root --password=zhang@123
范例2:查找192.168.3.135中1天以前更改过的数据库名字匹配%hostsops%的并且引擎为MYISAM的表,并将表的引擎更改为InnoDB引擎。
pt-find --mtime +1 --dblike hostsops --engine MyISAM --host=192.168.3.135 --user=root --password=zhang@123 --exec "ALTER TABLE %D.%N ENGINE=InnoDB"
范例3:查找192.168.3.135中aaa库和zhang库中的空表,并删除。
pt-find --empty aaa zhang --host=192.168.3.135 --user=root --password=zhang@123 --exec-plus "DROP TABLE %s"
范例4:查找192.168.3.135中超过100M的表:
pt-find --tablesize +100M --host=192.168.3.135 --user=root --password=zhang@123
3.pt-kill
功能介绍:
Kill掉符合指定条件mysql语句
用法介绍:
pt-kill [OPTIONS]
加入没有指定文件的话pt-kill连接到mysql并通过SHOW PROCESSLIST找到指定的语句,反之pt-kill从包含SHOW PROCESSLIST结果的文件中读取mysql语句
使用示例:
范例1:查找192.168.3.135服务器运行时间超过60s的语句,并打印
pt-kill --busy-time 60 --print --host=192.168.3.135 --user=root --password=zhang@123
范例2:查找192.168.3.135服务器运行时间超过60s的语句,并kill
pt-kill --busy-time 60 --kill --host=192.168.3.135 --user=root --password=zhang@123
范例3:从proccesslist文件中查找执行时间超过60s的语句
mysql -uroot -pzhang@123 -h192.168.3.135 -e "show processlist" > processlist.txt pt-kill --test-matching processlist.txt --busy-time 60 --print
4.pt-config-diff
功能介绍:
比较mysql配置文件和服务器参数
用法介绍:
pt-config-diff [OPTION...] CONFIG CONFIG [CONFIG...]
CONFIG可以是文件也可以是数据源名称,最少必须指定两个配置文件源,就像unix下面的diff命令一样,如果配置完全一样就不会输出任何东西。
使用示例:
范例1:查看本地和远程服务器的配置文件差异:
pt-config-diff h=localhost h=192.168.3.92 --user=root --password=zhang@123
比较出来内容如下:
22 config differences Variable localhost.localdomain localhost.localdomain ========================= ===================== ===================== binlog_cache_size 8388608 2097152 have_ndbcluster DISABLED NO innodb_additional_mem_... 16777216 33554432 innodb_buffer_pool_size 1677721600 1073741824
范例2:比较本地配置文件和远程服务器的差异:
pt-config-diff /etc/my.cnf h=192.168.3.92 --user=root --password=zhang@123
比较出来内容如下:
12 config differences Variable /etc/my.cnf localhost.localdomain ========================= =========== ===================== binlog_cache_size 8388608 2097152 binlog_format mixed MIXED
范例3:比较本地两个配置文件的差异:
pt-config-diff /usr/local/mysql/share/mysql/my-large.cnf /usr/local/mysql/share/mysql/my-medium.cnf
5.pt-mysql-summary
功能介绍:
精细地对mysql的配置和sataus信息进行汇总,汇总后你直接看一眼就能看明白。
用法介绍:
pt-mysql-summary [OPTIONS] [-- MYSQL OPTIONS]
工作原理:连接mysql后查询出status和配置信息保存到临时目录中,然后用awk和其他的脚本工具进行格式化。OPTIONS可以查阅官网的相关页面。
使用示例:
范例1:汇总本地mysql服务器的status和配置信息:
pt-mysql-summary -- --user=root --password=zhang@123 --host=localhost
范例2:汇总本地mysql服务器192.168.3.92的status和配置信息:
pt-mysql-summary -- --user=root --password=zhang@123 --host=192.168.3.92
6.pt-variable-advisor
功能介绍:
分析mysql的参数变量,并对可能存在的问题提出建议
用法介绍:
pt-variable-advisor [OPTION...] [DSN]
原理:根据预先定义的规则检查show variables中的配置错误的设置和值。
使用示例:
范例1:从localhost获取变量值
pt-variable-advisor --user=root --password=zhang@123 localhost
范例2:从指定的文件中读取配置,这个有格式要求
pt-variable-advisor --user=root --password=zhang@123 --source-of-variables my.cnf
以上就是使用percona-toolkit操作MySQL的实用命令小结_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

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.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Building an SQL database involves 10 steps: selecting DBMS; installing DBMS; creating a database; creating a table; inserting data; retrieving data; updating data; deleting data; managing users; backing up the database.
