mysql-utilities工具介绍
Tags: MySQLmysql-utilities
简介
子曰:工欲善其事必先利其器。一款好的利器能大大的提高效率。MySQL作为一款非常流行的开源数据库,支持它的工具日益繁多,提供的功能大大增强,对MySQL数据库管理员提供诸多方便,减轻工作量和维护难度。
MySQL Utilities 是官方提供的MySQL管理工具,功能面面俱到,主要有五个层面的工具:数据库层面(复制、比较、差异、导出、导入)、审核日志层面、服务器层面(实例克隆、实例信息)、系统层面(磁盘使用情况、冗余索引、搜索元数据、进程)、高可用性层面(主从复制、故障转移、主从同步)。此工具让你在mysql的管理上如虎添翼。
MySQL Utilities 提供一组命令行工具用于维护和管理 MySQL 服务器,包括:
- 管理工具 (克隆、复制、比较、差异、导出、导入)
- 复制工具 (安装、配置)
- 一般工具 (磁盘使用情况、冗余索引、搜索元数据)
MySQL Utilities是一系列的命令行工具以及Python库更容易完成管理的任务。库是用Python语言写的,这就意味着不需要安装其他任何工具和库。当前是基于Python2.6版本设计的,不支持Python3.1版本。
MySQL Utilities提供了各种平台的软件包,如果没有找到对应自己平台的包,可以通过源码进行编译安装。
安装
最新的MySQL Utilities可以在此处下载:http://dev.mysql.com/downloads/utilities/
MySQL Utilities is now covered under Oracle Lifetime Sustaining Support
Per Oracle's Lifetime Support policy, as of May 30, 2018, MySQL Utilities is covered under Oracle Sustaining Support. Some features of Utilities are on the roadmap for Shell, users are encouraged to migrate to MySQL Shell.
MySQL Utilities提供了各种平台的软件包,如果没有找到对应自己平台的包,可以通过源码进行编译安装。
MySQL Utilities可以在Linux或Windows上安装使用,不过该工具已经停止更新。
MySQL Utilities依赖环境
MySQL Utilities需要Python2.6版本,所有的代码都是基于该版本编写的。同时,还需要连接驱动MySQL Connector/Python通用版本(高于1.0.8)。
MySQL Connector/Python下载地址:http://dev.mysql.com/downloads/connector/python/
源码安装(推荐)
1 2 3 4 5 6 7 8 | yum -y install python-devel openssl openssl-devel gcc sqlite sqlite-devel mysql-devel libxml2-devel libxslt-devel wget https://cdn.mysql.com/archives/mysql-utilities/mysql-utilities-1.6.5.tar.gz tar -zxf mysql-utilities-1.6.5.tar.gz cd mysql-utilities-1.6.5 python ./setup.py build python ./setup.py install |
可以会要求你先安装驱动程序Connector/Python
yum源安装
若配置了MySQL YUM源,可以使用yum安装,但是yum安装的版本是1.3.6版本,而最新版本为1.6.5版本,建议下载安装最新的1.6.5的rpm包进行安装。
1 2 3 4 5 6 7 8 | rpm -e mysql-connector-python-2.1.8-1.el7.x86_64 --nodeps #centos 7 rpm -Uvh http://repo.mysql.com/yum/mysql-connectors-community/el/7/x86_64/mysql-connector-python-1.1.6-1.el7.noarch.rpm #centos 6 rpm -Uvh http://repo.mysql.com/yum/mysql-connectors-community/el/6/x86_64/mysql-connector-python-1.1.6-1.el6.noarch.rpm yum install mysql-utilities.noarch |
yum安装默认会安装如下2个包:
mysql-connector-python-1.1.6-1.el7.noarch.rpm
mysql-utilities-1.3.6-1.el7.noarch.rpm
安装最新版:
1 2 3 4 5 6 7 8 9 | yum -y install python-devel openssl openssl-devel gcc sqlite sqlite-devel mysql-devel libxml2-devel libxslt-devel rpm -e mysql-connector-python-1.1.6-1.el7.noarch --nodeps rpm -e mysql-utilities-1.3.6-1.el7.noarch --nodeps rpm -Uvh http://repo.mysql.com/yum/mysql-connectors-community/el/7/x86_64/mysql-connector-python-2.1.8-1.el7.x86_64.rpm wget https://cdn.mysql.com/archives/mysql-utilities/mysql-utilities-1.6.5-1.el7.noarch.rpm rpm -ivh mysql-connector-python-2.1.8-1.el7.x86_64.rpm rpm -ivh mysql-utilities-1.6.5-1.el7.noarch.rpm |
windows
x86 32位:https://cdn.mysql.com/archives/mysql-utilities/mysql-utilities-1.6.5-win32.msi
x86 64位:https://cdn.mysql.com/archives/mysql-utilities/mysql-utilities-1.6.5-winx64.msi
下一步下一步没啥好说的了。
所有工具
mysqldbcompare
- 比较两个服务器或同个服务器上的数据库
- 比较定义文件和数据
- 产生差异报告
- 生成差异性的转换SQL语句
mysqldbcopy
- 服务器之间复制数据库
- 在同一服务器上克隆数据库
- 支持重命名
mysqldbexport
- 从一个或多个数据库导出元数据或数据
- 支持格式:SQL CSV TAB Grid Vertical
mysqldbimport
- 从一个或多个文件导入元数据或数据
- 支持mysqldbexport的各种格式
mysqldiff
- 比较对象的定义
- 产生差异报告
mysqldiskusage
- 显示数据库磁盘使用情况
- 生成报表支持sql csv tab grid vertical
mysqlfrm
- 读取frm文件
- 生成表create语句
mysqlindexcheck
- 读取一个或多个表的索引
- 检查冗余和重复的索引
mysqlmetagrep
- 搜索元数据
- 正则表达式搜索数据库
- 搜索查询生成sql语句
mysqlprocgrep
- 搜索进程信息
- 搜索生成sql语句
- kill匹配到的进程
mysqluserclone
- 克隆用户
- 显示用户权限
mysqluc
- MySQL Utilities命令行客户端
- 允许长连接到MySQL服务器
- 可使用tab完成工具名称和选项
- 允许使用命令短名称如mysqlserverinfo- >serverinfo
MySQL Utilities连接MySQL
本节将描述下如何通过MySQL Utilities或者通过MySQL Utilities库方法连接到MySQL服务器。
连接参数
连接到一个服务器,必须指定连接参数,如用户名,主机名称,密码,端口号,socket。MySQL Utilities提供了三种提供这些参数的方法,这些方法都需要通过命令行指定。
- 使用.mylogin.cnf文件(加密的,不可见的)。如:
<login-path>[:<port>][:]
- 使用配置文件(未加密的,不可见的)。只适用于1.5.0以上版本。如:
<configuration-file-path>[:<section>]
- 在命令行指定(未加密的,可见的)。如:
<user>[:<passwd>]@<host>[:<port>][:]
使用login-paths(.mylogin.cnf)
使用该方法连接数据库是最好的。不仅是文件加密的,而且任何执行的记录不会暴露连接信息。因此,日志中用户名、密码、端口等信息是不可见的。这是使用MySQL Utilities工具连接数据库的首选方法。
注意:MySQL Utilities1.2.1以及以后版本支持login-paths方法。
连接的字符串格式为: login-path-name[:port][:socket]。其中port、socket是可选的参数。如果指定,将覆盖login-path中定义的。
当使用login-paths时,除了Posix系统没有默认值,当指定套接字时。在这种情况下,host选项默认值是localhost。这就意味着,结合login-path指定值有两个可选值port和socket选择,至少需要指定一个用户名、一个主机名、一个port或一个socket。
使用mysql_config_editor工具添加如下连接信息:
1 2 3 | cd /usr/local/mysql5.6/ # ./bin/mysql_config_editor set --login-path=instance_3366 --host=localhost --user=root --port=3366 --password Enter password: |
将会在家目录下创建一个隐藏的加密文件.mylogin.cnf。
查看.mylogin.cnf 内容:
一旦配置了.mylogin.cnf文件,就只需要指定.mylogin.cnf文件中的服务器段进行连接。如,在前面的示例中创建了“instance_3366”服务段。因此,我们可以使用--server=instance_3366。如下所示指定部分的登录路径:
1 2 3 4 5 6 | ./bin/mysql_config_editor print --login-path=instance_3366 [instance_3366] user = root password = ***** host = localhost port = 3366 |
也可以自定义,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ysqlserverinfo --server=root:@localhost:3366:/tmp/mysql3366.sock --format=vertical WARNING: Using a password on the command line interface can be insecure. # Source on localhost: ... connected. ************************* 1. row ************************* server: localhost:3366 config_file: /usr/local/mysql5.6/my.cnf, ./my.cnf binary_log: master-bin.000003 binary_log_pos: 151 relay_log: relay_log_pos: version: 5.6.16-log datadir: /usr/local/mysql5.6/data/ basedir: /usr/local/mysql5.6 plugin_dir: /usr/local/mysql5.6/lib/plugin/ general_log: OFF general_log_file: general_log_file_size: log_error: /usr/local/mysql5.6/data/localhost.err log_error_file_size: 2238 bytes slow_query_log: OFF slow_query_log_file: slow_query_log_file_size: 1 row. #...done. |
使用配置文件
MySQL Utilities也可以接受服务器连接数据的配置路径和段。这就允许你存储的一个或多个部分的连接信息。将数据保存在配置文件中比在命令行指定安全,但是,该文件是个文本文件,只有能访问到该文件,任何人可读的。
my.cnf连接段如下所示:
1 2 3 4 5 | vim my.cnf [client] port = 3366 socket = /tmp/mysql3366.sock user=root |
使用配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ysqlserverinfo --server=/usr/local/mysql5.6/my.cnf[client] --format=vertical # Source on localhost: ... connected. ************************* 1. row ************************* server: localhost:3366 config_file: /usr/local/mysql5.6/my.cnf, ./my.cnf binary_log: master-bin.000003 binary_log_pos: 151 relay_log: relay_log_pos: version: 5.6.16-log datadir: /usr/local/mysql5.6/data/ basedir: /usr/local/mysql5.6 plugin_dir: /usr/local/mysql5.6/lib/plugin/ general_log: OFF general_log_file: general_log_file_size: log_error: /usr/local/mysql5.6/data/localhost.err log_error_file_size: 2238 bytes slow_query_log: OFF slow_query_log_file: slow_query_log_file_size: 1 row. #...done. |
使用命令行
通过命令行参数指定连接服务器信息,这种方式是最不安全的,数据在命令行可见,在日志文件中亦是可见的。
这种方式下,指定参数的顺序为:<user>[:<passwd>]@<host>[:<port>][:]
。方括号代表是可选的参数。
实例参见上面的。
在Python库中定义
如果使用MySQL Utilities库文件来创建自己的工具,你会遇到各种各样的连接方式。
可选的方法有:
- 一个含有连接参数的Python字典.
- 一个含有连接参数的字符串.
- 指定一个服务器实例.
字典格式如下:
1 2 3 4 5 6 7 8 | Set connection values dest_values = { "user" : "root", "passwd" : "secret", "host" : "localhost", "port" : 3308, "unix_socket" : None, } |
字符串格式为:
<user>[:<passwd>]@<host>[:<port>][:]
中括号是可选参数。
该连接字符串通过使用options.parse_connection函数进行解析。
也可以指定服务器类的一个实例,在这种情况下,新类将复制这些连接信息。
数据库层面工具
MySQL Utilities提供了一系列MySQL服务器和数据库的管理工具。完全支持MySQL5.1及以上版本,也兼容MySQL5.0版本,不过有些特性不支持。不支持MySQL4.0版本。
在下面的章节中,将介绍描述一些需求、目标、执行方法、具体方案和说明等管理任务以及所需的特定权限。
下面这些工具工作在数据库级别,可以用来管理一个或多个服务器的数据库。
1. mysqldbcompare
- 比较两个服务器或同个服务器上的数据库
- 比较定义文件和数据
- 产生差异报告
- 生成差异性的转换SQL语句
2. mysqldbcopy
- 服务器之间复制数据库
- 在同一台服务器上克隆数据库
- 支持重命名
3. mysqldbexport
- 从一个或多个数据库导出元数据和或数据
- 支持的格式: SQL, CSV, TAB, Grid, Vertical
4. mysqldbimport
- 从一个或多个文件导入元数据和数据
- 支持mysqldbexport各种格式
5. mysqldiff
- 比较对象的定义
- 产生差异的报告
mysqluc
可以通过调用mysqluc命令行工具来获取这些工具的帮助信息
mysqluc提供一个自带的命令行提示符窗口,在这个窗口下也可以完成相应的命令操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | [root@lhrcentos76 /]# mysqlu mysqluc mysqluserclone [root@lhrcentos76 /]# mysqluc Launching console ... Welcome to the MySQL Utilities Client (mysqluc) version 1.3.6 Copyright (c) 2010, 2013 Oracle and/or its affiliates. All rights reserved. This is a release of dual licensed MySQL Utilities. For the avoidance of doubt, this particular copy of the software is released under the version 2 of the GNU General Public License. MySQL Utilities is brought to you by Oracle. Type 'help' for a list of commands or press TAB twice for list of utilities. mysqluc> help Command Description ---------------------- --------------------------------------------------- help utilities Display list of all utilities supported. help <utility> Display help for a specific utility. show errors Display errors captured during the execution of the utilities. clear errors clear captured errors. show last error Display the last error captured during the execution of the utilities help | help commands Show this list. exit | quit Exit the console. set <variable>=<value> Store a variable for recall in commands. show options Display list of options specified by the user on launch. show variables Display list of variables. <ENTER> Press ENTER to execute command. <ESCAPE> Press ESCAPE to clear the command entry. <DOWN> Press DOWN to retrieve the previous command. <UP> Press UP to retrieve the next command in history. <TAB> Press TAB for type completion of utility, option, or variable names. <TAB><TAB> Press TAB twice for list of matching type completion (context sensitive). mysqluc> help utilities Utility Description ---------------- --------------------------------------------------------- mysqlauditadmin audit log maintenance utility mysqlauditgrep audit log search utility mysqldbcompare compare databases for consistency mysqldbcopy copy databases from one server to another mysqldbexport export metadata and data from databases mysqldbimport import metadata and data from files mysqldiff compare object definitions among objects where the difference is how db1.obj1 differs from db2.obj2 mysqldiskusage show disk usage for databases mysqlfailover automatic replication health monitoring and failover mysqlfrm show CREATE TABLE from .frm files mysqlindexcheck check for duplicate or redundant indexes mysqlmetagrep search metadata mysqlprocgrep search process information mysqlreplicate establish replication with a master mysqlrpladmin administration utility for MySQL replication mysqlrplcheck check replication mysqlrplshow show slaves attached to a master mysqlserverclone start another instance of a running server mysqlserverinfo show server information mysqluserclone clone a MySQL user account to one or more new users |
参考
http://yunwei.why8.cn/mysql/mysql-manager-tools-mysql-utilities-tutorial/
http://blog.itpub.net/26736162/viewspace-2693273/
数据库层面
MySQL管理工具MySQL Utilities — 数据库操作管理(3)
MySQL管理工具MySQL Utilities — mysqldbcompare (4)
MySQL管理工具MySQL Utilities — 生成差异SQL语句来同步表内容 (5)
MySQL管理工具MySQL Utilities — mysqldbcopy (6)
MySQL管理工具MySQL Utilities — 如何复制数据库(7)
MySQL管理工具MySQL Utilities — mysqldbexport(8)
MySQL管理工具MySQL Utilities — mysqldbimport(9)
MySQL管理工具MySQL Utilities — 使用mysqldbexport与mysqldbimport创建新的从库(10)
MySQL管理工具MySQL Utilities — mysqldiff(11)
MySQL管理工具MySQL Utilities — mysqldbcopy改变存储引擎(12)
审核日志层面
MySQL管理工具MySQL Utilities — 针对日志的操作管理(13)
MySQL管理工具MySQL Utilities — mysqlauditadmin(14)
MySQL管理工具MySQL Utilities — mysqlauditgrep(15)
MySQL管理工具MySQL Utilities — 记录登录事件(16)
MySQL管理工具MySQL Utilities — 复制移动审计日志(17)
MySQL管理工具MySQL Utilities — 查询更新失败的记录(18)
MySQL管理工具MySQL Utilities — 查询某个连接用户的信息(19)
服务器层面
MySQL管理工具MySQL Utilities — 服务器操作管理(20)
MySQL管理工具MySQL Utilities — mysqlserverclone(21)
MySQL管理工具MySQL Utilities — mysqlserverinfo(22)
MySQL管理工具MySQL Utilities — 复制在线测试用例数据库(23)
系统层面
MySQL管理工具MySQL Utilities — 基础操作管理 (24)
MySQL管理工具MySQL Utilities — mysqldiskusage (25)
MySQL管理工具MySQL Utilities — mysqlfrm (26)
MySQL管理工具MySQL Utilities — mysqlindexcheck (27)
MySQL管理工具MySQL Utilities — mysqlmetagrep (28)
MySQL管理工具MySQL Utilities — mysqlprocgrep (29)
MySQL管理工具MySQL Utilities — mysqluserclone (30)
MySQL管理工具MySQL Utilities — mysqluc (31)
MySQL管理工具MySQL Utilities — 查询MySQL使用的空间大小(32)
MySQL管理工具MySQL Utilities — 从崩溃的数据库中恢复表结构(33)
MySQL管理工具MySQL Utilities — 创建具有相同权限的新用户(34)
MySQL管理工具MySQL Utilities — MySQL用户控制台(35)
MySQL管理工具MySQL Utilities — 优化重复冗余索引(36)
MySQL管理工具MySQL Utilities — mysqlmetagrep实际应用(37)
MySQL管理工具MySQL Utilities — mysqlprocgrep 实际应用(38)
高可用性层面
MySQL管理工具MySQL Utilities — 高可用性操作(39)
MySQL管理工具MySQL Utilities — mysqlfailover(40)
MySQL管理工具MySQL Utilities — mysqlreplicate(41)
MySQL管理工具MySQL Utilities — mysqlrplms(42)
MySQL管理工具MySQL Utilities — mysqlrpladmin(43)
MySQL管理工具MySQL Utilities — mysqlrplcheck(44)
MySQL管理工具MySQL Utilities — mysqlrplshow(45)
MySQL管理工具MySQL Utilities — mysqlrplsync(46)
MySQL管理工具MySQL Utilities — 使用mysqlreplicate创建复制(47)
MySQL管理工具MySQL Utilities — 添加新服务器和更改主角色(48)
MySQL管理工具MySQL Utilities — 自动故障转移(49)
MySQL管理工具MySQL Utilities — 恢复有故障的主(50)