MySQL 官方工具utilities介绍

MySQL 官方工具utilities介绍


MySQL Utilities 是官方提供的MySQL管理工具,功能面面俱到,主要有五个层面的工具:数据库层面(复制、比较、差异、导出、导入)、审核日志层面、服务器层面(实例克隆、实例信息)、系统层面(磁盘使用情况、冗余索引、搜索元数据、进程)、高可用性层面(主从复制、故障转移、主从同步)。此工具让你在mysql的管理上如虎添翼。

介绍

MySQL管理工具MySQL Utilities — 介绍与安装(1) 

MySQL管理工具MySQL Utilities — 如何连接MySQL服务器(2)

数据库层面

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)

MySQL管理工具MySQL Utilities — 找出主所有的从(51)

MySQL管理工具MySQL Utilities — 检查复制数据一致性(52)





    mysql-utilities 是python编写 MySQL官方版的命令行工具集,涵盖主从校验,主从建立,主从切换等等

    https://dev.mysql.com/doc/workbench/en/wb-mysql-utilities.html

    MySQL Utilities工具包概述及安装

    Mysql Utilities


    mysql-utilities 是python编写 MySQL官方版的命令行工具集,涵盖主从校验,主从建立,主从切换等等

    安装

    rpm安装
    注意:不要直接通过yum来安装mysql-utilities,mysql-utilities 1.6.5和mysql-connector-python 8.0.12在一起有问题!

     正确的安装方式如下:

    # rpm -Uvh https://repo.mysql.com//mysql80-community-release-el7.rpm  # linux7
    # rpm -Uvh https://repo.mysql.com//mysql80-community-release-el6.rpm  # linux6
    -- 安装mysql-utilities包,依赖于Python2.7,版本需要对应,否则报错
    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 -y mysql-utilities
    mysqlrplcheck --master=root:lhr@192.168.68.131:3306 --slave=root:lhr@192.168.68.133:3306 -v
    mysqlrplshow --master=root:lhr@192.168.68.131:3306 --discover-slaves-login=root:lhr --verbose
    # report-host=192.168.68.131
    # report-port=3306


    mysql5.7 yum源 还会安装依赖mysql-connector-python 8.0.12-1.el7

    二进制安装

    yum install mysql-connector-python
    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

    通过tar.gz安装和yum安装mysql-utilities出现错误

    可能模块没有在搜索路径中

    export PYTHONPATH=$PYTHONPATH:/root/mysql-utilities-1.6.5/mysql/utilities/common/tools
    试了也不行

    安装mysql5.6 yum源以rpm形式安装的mysql-utilities不会报module找不到

    解决

    卸载原mysql-connector-python8.0版本 
    yum  -y remove  mysql-connector-python
    并下载安装mysql-connector-python-2.1.7
    rpm -Uvh https://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.7-1.el7.x86_64.rpm
     mysqldbcompare --version
    MySQL Utilities mysqldbcompare version 1.6.5 
    License type: GPLv2

    当时还在纠结为什么安装mysql5.7 yum源在以yum安装mysql-utilities和以tar.gz形式来安装mysql-utilites
    两种方式该有的/mysql/utilities/common和tools.py 下的check_python_version都有了,而安装mysql5.6
    yum源不报错,查看 mysql-connector-python版本时才发现两者的版本不一样,在MySQL中 mysql-co
    nnector-python 8.0.12-1.el7 高版本的不兼容低版本的,这是个坑-__-


    Traceback (most recent call last):
      File "/bin/mysqldiff", line 28, in     from mysql.utilities.common.tools import check_python_version
    ImportError: No module named utilities.common.tools

    直接用yum安装的mysqldiff会提示No module named utilities.common.tools

    解决方法:

     remove mysql-connector-python --Uvh http:rpm -Uvh http://repo.mysql.com/yum/mysql-connectors-community/el/6/x86_64/mysql-connector-python-2.1.8-1.el6.x86_64.rpm

    MySQL Utilities 是一组基于python语言编写的python库的命令行实用工具集,依赖于python 2.6。

    该工具提供了MySQL数据库运维工程中常用的一些工具,诸如克隆、复制、比较、差异、导出、导入、安装、配置、索引、磁盘查看等等。有了这个工具包,就好比那些个神医大夫,不管大病小病,先去搞个化验,搞个CT,你也可以当华佗。MySQL Utilities提供了各种平台的软件包,如果没有找到对应自己平台的包,可以通过源码进行编译安装。本文主要描述MySQL Utilities安装以及各个工作功能初步描述。


    一、MySQL Utilities功能及组件

    Binary Log Operations(二进制日志操作) 

            mysqlbinlogmove   二进制日志移动

            mysqlbinlogpurge  二进制日志清理

            mysqlbinlogrotate 二进制日志老化工具    


    Database Operations(数据库操作)

            mysqldbexport     数据导出

            mysqldbimport     数据导入

            mysqldbcopy       库级别数据库复制

            mysqldiff         数据库对象级别比较工具

            mysqldbcompare    数据库库级别比较工具


    General Operations(通用用的操作)     

            mysqldiskusage    磁盘空间查看

            mysqlfrm          恢复故障表.frm文件

            mysqluserclone    用户克隆工具

            mysqluc           Utilities帮助工具 

            mysqlindexcheck   索引检测工具

            mysqlmetagrep     元数据过滤器

            mysqlprocgrep     进程搜索及清理工具


    High Availability Operations(高可用)

        mysqlreplicate   主从复制工具

        mysqlrpladmin    主从复制管理工具

        mysqlrplcheck    主从复制检测工具

        mysqlrplms       主从多元复制工具

        mysqlrplshow     主从复制拓扑图工具

        mysqlrplsync     主从复制同步工具

        mysqlfailover    主从failover工具

        mysqlslavetrx    从库事务跳过工具 


    Server Operations(服务器操作)

            mysqlserverinfo    服务器信息查看工具

            mysqlserverclone   服务器克隆工具    


    Specialized Operations(特殊操作)

            mysqlauditadmin    审计管理工具 

            mysqlauditgrep     审计日志过滤工具



    二、安装需求及下载地址

        需求

                Python 2.6

                MySQL Connector/Python 连接器

        下载地址:

                http://dev.mysql.com/downloads/utilities/


    三、安装示例

    ###本次安装使用1.6.4版本,安装前,需要先安装mysql到python连接器

    # cat /etc/redhat-release 

    CentOS release 6.7 (Final)

    # rpm -Uvh mysql-connector-python-2.1.4-1.el6.x86_64.rpm 

    # rpm -Uvh mysql-utilities-1.6.4-1.el6.noarch.rpm 


    ###如果没有安装连接器,则收到如下错误提示

    # rpm -Uvh mysql-utilities-1.6.4-1.el6.noarch.rpm 

    warning: mysql-utilities-1.6.4-1.el6.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY

    error: Failed dependencies:

            mysql-connector-python >= 2.0.0 is needed by mysql-utilities-1.6.4-1.el6.noarch


    ###查看安装后生成的文件        

    # rpm -ql mysql-utilities-1.6.4-1.el6|grep "/usr/bin"

    /usr/bin/mysqlauditadmin

    /usr/bin/mysqlauditgrep

    /usr/bin/mysqlbinlogmove

    /usr/bin/mysqlbinlogpurge

    /usr/bin/mysqlbinlogrotate

    /usr/bin/mysqldbcompare

    /usr/bin/mysqldbcopy

    /usr/bin/mysqldbexport

    /usr/bin/mysqldbimport

    /usr/bin/mysqldiff

    /usr/bin/mysqldiskusage

    /usr/bin/mysqlfailover

    /usr/bin/mysqlfrm

    /usr/bin/mysqlgrants

    /usr/bin/mysqlindexcheck

    /usr/bin/mysqlmetagrep

    /usr/bin/mysqlprocgrep

    /usr/bin/mysqlreplicate

    /usr/bin/mysqlrpladmin

    /usr/bin/mysqlrplcheck

    /usr/bin/mysqlrplms

    /usr/bin/mysqlrplshow

    /usr/bin/mysqlrplsync

    /usr/bin/mysqlserverclone

    /usr/bin/mysqlserverinfo

    /usr/bin/mysqlslavetrx

    /usr/bin/mysqluc

    /usr/bin/mysqluserclone    


    四、获取帮助

    1、通过mysqluc获取帮助

    ###可以通过调用mysqluc命令行工具来获取这些工具的帮助信息

    ### mysqluc提供一个自带的命令行提示符窗口,在这个窗口下也可以完成相应的命令操作


    [root@node1 ~]# mysqluc

    Launching console ...


    Welcome to the MySQL Utilities Client (mysqluc) version 1.6.4

    Copyright (c) 2010, 2016 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           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 =  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.                         

                     Press ENTER to execute command.                    

                    Press ESCAPE to clear the command entry.           

                      Press DOWN to retrieve the previous command.       

                        Press UP to retrieve the next command in history.  

                       Press TAB for type completion of utility, option,  

                            or variable names.                                 

                  Press TAB twice for list of matching type          

                            completion (context sensitive).                    


    ###查看utilities包中所有的命令行工具

    mysqluc> help utilities 


    Utility            Description                                             

    -----------------  --------------------------------------------------------

    mysqlauditadmin    audit log maintenance utility                           

    mysqlauditgrep     audit log search utility                                

    mysqlbinlogmove    binary log relocate utility                             

    mysqlbinlogpurge   purges unnecessary binary log files                     

    mysqlbinlogrotate  rotates the active binary log file                      

    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                       

    mysqlgrants        display grants per object                               

    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                                       

    mysqlrplms         establish multi-source replication                      

    mysqlrplshow       show slaves attached to a master                        

    mysqlrplsync       replication synchronization checker utility             

    mysqlserverclone   start another instance of a running server              

    mysqlserverinfo    show server information                                 

    mysqlslavetrx      skip transactions on slaves                             

    mysqluserclone     clone a MySQL user account to one or more new users     


    ###也可直接在mysqluc提示符下输入 help command 来获取对应命令的帮助信息,如下

    mysqluc> help mysqlauditadmin 

    Usage: mysqlauditadmin --server=user:pass@host:port --show-options 

    # Author : Leshami

    # Blog   : http://blog.csdn.net/leshami

    mysqlauditadmin - audit log maintenance utility 


    Options:

    Option                     Description                                     

    -------------------------  ------------------------------------------------

    --version                  show program's version number and exit          

    --help                     display this help message and exit              

    --license                  display program's license and exit              

    --server=SERVER            connection information for the server in the    

                               form:                                           

                               [:]@[:][:]  

                               or [:][:] or

                               path>[<[group]>].                               

    --audit-log-name=LOG_NAME  full path and file name for the audit log file. 

                               Used for stats and copy options.                

    --show-options             display the audit log system variables.         

    --remote-login=RLOGIN      user name and host to be used for remote login  

                               for copying log files. Format:                  

                               : Password will be prompted.  

    --file-stats               display the audit log file statistics.          

    --copy-to=COPY_LOCATION    the location to copy the audit log file         

                               specified. The path must be locally accessible  

                               for the current user.                           

    --value=VALUE              value used to set variables based on the command

                               specified. See --help for list per command.     

    --ssl-ca=SSL_CA            path to a file that contains a list of trusted  

                               SSL CAs.                                        

    --ssl-cert=SSL_CERT        name of the SSL certificate file to use for     

                               establishing a secure connection.               

    --ssl-key=SSL_KEY          name of the SSL key file to use for establishing

                               a secure connection.                            

    --ssl=SSL                  specifies if the server connection requires use 

                               of SSL. If an encrypted connection cannot be    

                               established, the connection attempt fails. By   

                               default 0 (SSL not required).                   

    -v, --verbose              control how much information is displayed. e.g.,

                               -v = verbose, -vv = more verbose, -vvv = debug  

                               Available Commands: copy - copy the audit log to

                               a locally accessible path policy - set the audit

                               log policy Values = ALL, NONE, LOGINS, QUERIES, 

                               DEFAULT rotate - perform audit log rotation     

                               rotate_on_size - set the rotate log size limit  

                               for auto rotation Values = 0, 4294967295    


    2、直接在shell提示符下获取命令帮助

    ###如下示例

    [root@node1 ~]# mysqlfailover --help|head

    MySQL Utilities mysqlfailover version 1.6.4 

    License type: GPLv2

    Usage: mysqlfailover --master=root@localhost --discover-slaves-login=root 

                    --candidates=root@host123:3306,root@host456:3306 


    mysqlfailover - automatic replication health monitoring and failover


    Options:

      --version             show program's version number and exit

      --help                display this help message and exit

      --license             display program's license and exit 


    3、基于Linux man获取命令帮助

    ###如下示例

    root@node1 ~]# man mysqlfailover

    MYSQLFAILOVER(1)                MySQL Utilities               MYSQLFAILOVER(1)


    NAME

           mysqlfailover - Automatic replication master failover


    SYNOPSIS

           mysqlfailover [options]



    mysqlreplicate

    主从复制工具

    mysqlreplicate --master=admin:admin@10.186.30.73:3307 \
               --slave=admin:admin@10.186.30.58:3307 --rpl-user=rpl:rpl

    –pedantic选项来确保主和从复制成功,当且仅当两个服务器具有相同的存储引擎,相同的默认存储引擎和相同的InnoDB存储引擎

    mysqlrplcheck

    主从复制检测工具

    mysqlrplcheck --master=admin:admin@10.186.30.73:3307 \
               --slave=admin:admin@10.186.30.58:3307 \
                --show-slave-status -vv

    mysqlrplshow

    主从复制拓扑图工具

    mysqlrplshow --master=admin:admin@10.186.30.73:3307    \
             --discover-slaves-login=admin:admin --verbose --recurse

    mysqlrpladmin

    主从复制管理工具

    mysqlrpladmin --master=admin:admin@10.186.30.73:3307 \
               --slave=admin:admin@10.186.30.58:3307 Health
     
    mysqlrpladmin --master=admin:admin@10.186.30.73:3307 \
               --slave=admin:admin@10.186.30.58:3307 gtid
               
    mysqlrpladmin --master=admin:admin@10.186.30.73:3307 \
              --slave=admin:admin@10.186.30.58:3307 reset
     
    mysqlrpladmin -vv --master=admin:admin@10.186.30.73:3307 \
              --slave=admin:admin@10.186.30.58:3307  \
              --new-master=admin:admin@10.186.30.58:3307 --demote-master switchover
     
    mysqlrpladmin -vv --slave=admin:admin@10.186.30.58:3307 failover
     
    mysqlrpladmin 1.3.6对mysql5.7 会出现错误
    # Checking privileges.
    ERROR: Query failed. 1054 (42S22): Unknown column 'password' in 'field list'

    mysqlfailover

    主从切换工具

    mysqlfailover --master=admin:admin@10.186.30.73:3307 --discover-slaves-login=admin --candidates=admin:admin@10.186.30.58:3307

    mysqldbcompare

    数据库库级别比较工具

    mysqldbcompare --server1=admin:admin@10.186.30.73:3307 --server2=admin:admin@10.186.30.58:3307 --changes- for =server2 --difftype=sql --run-all-tests test

    详细介绍

    Binary Log Operations(二进制日志操作)
             mysqlbinlogmove   二进制日志移动
             mysqlbinlogpurge  二进制日志清理
             mysqlbinlogrotate 二进制日志老化工具   
     
    Database Operations(数据库操作)
             mysqldbexport     数据导出
             mysqldbimport     数据导入
             mysqldbcopy       库级别数据库复制
             mysqldiff         数据库对象级别比较工具
             mysqldbcompare    数据库库级别比较工具
     
    General Operations(通用用的操作)    
             mysqldiskusage    磁盘空间查看
             mysqlfrm          恢复故障表.frm文件
             mysqluserclone    用户克隆工具
             mysqluc           Utilities帮助工具
             mysqlindexcheck   索引检测工具
             mysqlmetagrep     元数据过滤器
             mysqlprocgrep     进程搜索及清理工具
     
    High Availability Operations(高可用)
             mysqlreplicate   主从复制工具
             mysqlrpladmin    主从复制管理工具
             mysqlrplcheck    主从复制检测工具
             mysqlrplms       主从多元复制工具
             mysqlrplshow     主从复制拓扑图工具
             mysqlrplsync     主从复制同步工具
             mysqlfailover    主从failover工具
             mysqlslavetrx    从库事务跳过工具
     
    Server Operations(服务器操作)
             mysqlserverinfo    服务器信息查看工具
             mysqlserverclone   服务器克隆工具   
     
    Specialized Operations(特殊操作)
             mysqlauditadmin    审计管理工具
             mysqlauditgrep     审计日志过滤工具

    参考:

    https://dev.mysql.com/doc/workbench/en/wb-mysql-utilities.html

    MySQL Utilities工具包概述及安装

    Mysql Utilities




    MySQL Utilities是官方提供的MySQL管理工具集
    下载地址: https://downloads.mysql.com/archives/utilities/
    当前版本1.6.5  https://downloads.mysql.com/archives/get/file/mysql-utilities-1.6.5-1.el7.noarch.rpm
    二进制包: https://cdn.mysql.com/archives/mysql-utilities/mysql-utilities-1.6.5.tar.gz
    二进制包: https://downloads.mysql.com/archives/get/file/mysql-utilities-1.6.5.tar.gz

    MySQL Utilities是基于python编写的,不需要安装其他任何工具和库
    提供一组命令行工具用于维护和管理MySQL服务器:
    数据库层面:复制、比较、差异、导出、导入
    审核日志层面:
    服务器层面:实例克隆、实例信息
    系统层面:磁盘使用情况、冗余索引、搜索元数据、进程
    高可用层面:主从复制、故障转移、主从同步

    安装

    尽量不要用 yum 来安装 mysql-utilities 1.6.5版本的mysql-utilites和mysql-connector-python有兼容性问题
    实测,可以使用 yum 安装 1.3.6的mysql-utilites和1.1.6的mysql-connector-python

    # 安装 1.3.6 版本 yum方式
    yum install https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/Packages/m/mysql-connector-python-1.1.6-1.el7.noarch.rpm
    yum install https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/Packages/m/mysql-utilities-1.3.6-1.el7.noarch.rpm
    # 卸载
    yum -y remove mysql-connector-python
    # 安装 1.6.5 版本 二进制方式
    最新版本 1.6.5 建议使用二进制安装(tar包中自带connector无需单独安装)
    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

    连接MySQL服务器

    login-paths(.mylogin.cnf)

    推荐方式,加密的,不会暴露连接信息
    mysql_config_editor set --login-path= mysql_77 --host=192.168.1.77 --user=root --port=3306 --password
    mysql_config_editor是mysql自带工具,执行如上命令后会在家目录生成加密文件.mylogin.cnf
    查看文件内容:
    mysql_config_editor print --login-path= mysql_77
    [mysql_77]
    user = root
    password = *****
    host = 192.168.1.77
    port = 3306
    使用:
    mysqlserverinfo --server= mysql_77 --format=vertical
    mysql --login-path= mysql_77

    使用配置文件

    /etc/my.cnf 中[client]段
    [client]
    port = 3306
    socket = /tmp/mysql.sock
    user=root
    password=root123
    使用:
    mysqlserverinfo --server= /etc/my.cnf[client] --format=vertical

    命令行参数

    该方式最不安全
    [:]@[:][:]
    使用:
    mysqlserverinfo --server= root:root123@192.168.1.77:3306 --format=vertical

    数据库层面

    以下工具工作在数据库级别,用于管理一个或多个服务器的数据库

    mysqldbcompare
    - 比较两个服务器或同个服务器上的数据库
    - 比较定义文件和数据
    - 产生差异报告
    - 生成差异性的转换SQL语句
    mysqldbcopy
    - 服务器之间复制数据库
    - 在同一服务器上克隆数据库
    - 支持重命名
    mysqldbexport
    - 从一个或多个数据库导出元数据或数据
    - 支持格式:SQL CSV TAB Grid Vertical
    mysqldbimport
    - 从一个或多个文件导入元数据或数据
    - 支持mysqldbexport的各种格式
    mysqldiff
    - 比较对象的定义
    - 产生差异报告

    mysqldbcompare

    mysqldbcompare --server1=user:pass@host:port:socket --server2=user:pass@host:port:socket db1:db2

    比较两个数据库对象的差异,包括表、视图、触发器、存储过程、函数、事件
    比较检测的步骤:
    1、数据库定义,确保数据库存在,若不存在,不需要下一步检测
    2、数据库对象,--skip-object-compare跳过
    3、对象定义,比较create语句,--skip-diff跳过
    4、检测表行数,仅检查行数是否一致,--skip-row-count跳过
    5、表数据一致性,先全表checksum校验,然后找出不同 --skip-checksum-table跳过表校验,--skip-data-check跳过数据检查

    --diff-style 定义输出风格:
    unified 默认,统一格式
    context 上下文格式
    differ differ-style格式
    sql 转换语句
    --format 显示行丢失或改变的输出
    grid 默认,网格化
    vertical 类似\G
    csv 、tab
    --changes-for 控制差异报告(默认)还是转换报告(需要--difftype=sql)

    权限:
    对所比较的数据库,select,create temporary tables,insert
    mysql数据库,select
    防止比较操作被写入二进制日志,需要启用  --disable-binary-logging

    mysqldbcompare --server1=mysql_78 --server2=mysql_88 db1:db2 --run-all-tests

    实际上并不好用

    mysqldbcopy

    mysqldbcopy --source=user:pass@host:port:socket --destination=user:pass@host:port:socket orig_db:new_db

    从源服务器上复制一个数据库到另一个目标服务器上,源服务器和目标服务器可以是同一台,数据库名字可相同也可不同
    希望复制的新库或新的服务器改变存储引擎,在这种情况下,使用mysqldbcopy工具非常便捷

    参数:
    -d,--drop-first 若目录中存在,先drop
    --new-storage-engine 目标使用新的引擎
    --default-storage-engine 目标的默认引擎
    --locking 复制过程中的锁级别:no-locks,lock-all,snaphot(默认)
    --rpl-user
    --rpl  直接搭建主从

    权限:
    源数据库,select,show view,event,trigger  同时mysql数据库要有select
    目标数据库,create,alter,select,insert,update,lock tables,drop(--drop-first),super(二进制日志启用),create view,create routine,execute,event,trigger,grant option,

    mysqldbcopy --source=mysql_77 --destination=mysql_77 ecard:new_ecard
    mysqldbcopy --source=mysql_77 --destination=mysql_77 test:new_test -vvv --drop-first --locking=lock-all
    mysqldbcopy --source=mysql_77 --destination=mysql_88 --drop-first --all  #复制整个实例

    mysqldbexport

    mysqldbexport --server=user:pass@host:port:socket db1, db2, db3

    导出数据

    选项:
    --file-per-table 每个表单独保存,格式,db.table.csv
    --rpl 和 --rpl-user 包含主从同步语句 --comment-rpl 注释主从语句

    权限:
    源服务器 select,show view mysql数据库的select

    # 只导出定义语句
    mysqldbexport --server=mysql_77 --format=sql ecard --export=definitions > ecard.sql
    # 只导入数据 批量插入
    mysqldbexport --server=mysql_77 --format=sql ecard --export=data --bulk-insert > ecard.sql
    mysqldbexport --server=mysql_77 --format=sql ecard --export=data --bulk-insert --file-per-table  #一个表一个文件

    若数据库中并不是所有表是innodb引擎,为确保数据一致性,需要在导入 前锁定表,加上 --locking=lock-all
    # 导出结构和数据 并为当前数据库创建一个从服务器
    mysqldbexport --server=mysql_77 --format=sql ecard --export=both --rpl-user=root --rpl=master > ecard.sql

    mysqldbimport

    mysqldbimport --server=user:pass@host:port:socket db1.csv db2.sql db3.grid

    导入数据,若一个对象已经存在于目标服务器上,将先删除再导入

    选项:
    -- format 格式
    -- import data definitions(默认) both
    -- drop - first
    -- no - headers 排除表头,适用于tab csv格式
    -- dryrun   预导入不执行,测试用。测试文件是否有效
    -- table
    --skip - blobs  --skip -rpl  --skip -gtid
    --skip =SKIP_OBJECTS 忽略的对象 
    tables ,  views ,  triggers ,  procedures ,  functions , events ,  grants ,  data ,  create_db

    mysqldbimport --server=mysql_77 --import=definitions --format=csv data.csv
    mysqldbimport --server=mysql_77 --import=data --bulk-insert --format=csv data.csv

    mysqldiff

    mysqldiff --server1=user:pass@host:port:socket --server2=user:pass@host:port:socket db1.object1:db2.object1 db3:db4

    比较对象定义是否相同,不能比较数据是否一致

    mysqldiff --server1=mysql_77 test.mytest:test.t
    mysqldiff --server1=mysql_77 test.mytest:test.t --difftype=sql --show-reverse -vvv

    审核日志层面

    审核日志是MySQL企业版的功能,必需开启审核日志插件

    mysqlauditadmin
    - 监控审计日志
    - 复制 轮换和配置审核日志
    mysqlauditgrep
    - 搜索日志
    - 输出不同格式的结果

    mysqlauditadmin --server=user:pass@host:port --show-options
    mysqlauditgrep [options] AUDIT_LOG_FILE

    服务器层面

    mysqlserverclone
    - 克隆一个新的实例
    mysqlserverinfo
    - 显示服务器信息
    - 搜索主机上运行的服务
    - 访问在线或离线的服务

    mysqlserverclone

    mysqlserverclone --server=user:pass@host:port:socket --new-data=/tmp/data2 --new-port=3310 --new-id=12 --root-password=root

    在同一个主机上创建一个新的服务实例,创建一个新的datadir和socket文件,并启动该实例
    克隆前要先删除datadir目录下文件并配置读写权限

    mysqlserverclone --server=instance_3306 --new-data=/data/tmp/ --new-port=3310 --root-password=3310 --mysqld=--log-bin=mysql-bin-3310 --new-id=3310 --user=mysql -vvv

    mysqlserverinfo

    mysqlserverinfo --server=user:pass@host:port:socket --format=grid

    选项:
    - d ,  -- show - defaults 显示config文件信息
    -- show - servers 若启动了多个实例 可使用该命令查看

    mysqlserverinfo --server=mysql_77 -d --format=grid -vvv --show-defaults  #额外显示defaults信息
    mysqlserverinfo --server=mysql_77 -d --format=grid -vvv --show-defaults --no-headers

    系统层面

    mysqldiskusage
    - 显示数据库磁盘使用情况
    - 生成报表支持 sql csv tab grid vertical
    mysqlfrm
    - 读取 frm 文件
    - 生成表 create 语句
    mysqlindexcheck
    - 读取一个或多个表的索引
    - 检查冗余和重复的索引
    mysqlmetagrep
    - 搜索元数据
    - 正则表达式搜索数据库
    - 搜索查询生成sql语句
    mysqlprocgrep
    - 搜索进程信息
    - 搜索生成 sql 语句
    - kill匹配到的进程
    mysqluserclone
    - 克隆用户
    - 显示用户权限
    mysqluc
    - MySQL Utilities命令行客户端
    - 允许长连接到MySQL服务器
    - 可使用tab完成工具名称和选项
    - 允许使用命令短名称 如 mysqlserverinfo -> serverinfo

    mysqldiskusage

    mysqldiskusage --server=user:pass@host:port:socket db1 --all

    显示一个或多个数据库所使用磁盘空间大小,可显示二进制日志、慢查询日志、错误日志、查询日志、二进制中继日志、innodb表空间大小
    读取文件系统失败,或者服务不在本地,那么将不能确定文件大小

    选项:
    - b ,  -- binlog
    - r ,  -- relaylo
    - l ,  -- logs
    - i ,  -- innodb
    - m ,  -- empty
    - a ,  -- all

    mysqldiskusage --server=mysql_77
    mysqldiskusage --server=mysql_77 --format=g -a -vvv

    mysqlfrm

    mysqlfrm --server=[user[:]@host[:][:]|[:][:]]  [path\tbl1.frm|db:tbl.frm]

    恢复工具,读取frm文件并从中找到 视图定义语句
    注意:不能读取到外键约束和自增长序列

    再生实例模式:
    --basedir或--server选项连接到已经安装的实例。该过程不会改变原始frm文件,指定--port选项给再生实例使用,该端口不能与现有端口冲突,读取完frm文件后,再生实例会关闭,所有临时文件将被删除
    诊断模式:
    指定--diagnostic选项,byte-by-byte读取frm文件,尽可能多的恢复信息,该模式不能校验字符集

    选项:
    --quiet 仅显示create语句和警告、错误信息
    --show-stats 统计frm文件信息

    mysqlfrm --basedir=/usr ttlsa_com:data.frm --port=3333 --user=mysql -vvv
    mysqlfrm --diagnostic /root/data_center  #读取data_center目录下所有frm文件
    mysqlfrm --diagnostic /root/t7_data_center/t7_venue.frm

    利用frm和ibd恢复数据

    在某此情况下,数据库崩溃,无法启动,此时,可利用磁盘中的frm和ibd文件恢复数据,因为数据库存储的数据实际上还是存储在文件里的

    myisam引擎:直接复制这三个文件即可使用,不需要恢复
    xxx.frm 表结构文件
    xxx.MYD 数据文件
    xxx.MYI 索引文件
    innodb引擎innodb某些元数据是存储在 ibdata1文件中的
    xxx.frm 表结构文件  -> 使用mysqlfrm获取
    xxx.ibd 数据和索引文件 -> 覆盖文件

    恢复前提:innodb_file_per_table = 1
    若不能使用mysqlfrm工具时,要获得表结构语句[ 不推荐]:
    1、创建同名表,create table t_access(id int);
    2、使用要恢复表的frm文件替换同名frm文件,修改frm文件权限 chown mysql:mysql xxx.frm
    3、flush tables;
    4、show create table t_access;  #注意,此时数据库日志中会显示字段数量,如
    [Warning] InnoDB: Table tt/t_access contains 1 user defined columns in InnoDB, but  4 columns
    5、重新创建拥有相同字段的同名表
    6、再次用要恢复的表的frm文件替换同名frm文件,修改权限
    7、数据库关机
    8、配置文件设置 innodb_force_recovery=6 重启数据库
    9、得到表结构
    显然,使用mysqlfrm工具更为方便

    利用frm和ibd恢复数据操作流程

    场景:因数据库损坏,ibdata1文件被误删,数据库启动后,所有innodb引擎的表能显示名称,但打开时提示表不存在
    目标:需要恢复其中一张表 t7_system_nodes 可利用的文件为
    /tmp/t7_system_nodes.frm -> 存储了表结构
    /tmp/t7_system_nodes.ibd -> 存储了表数据

    第一步:利用mysqlfrm工具恢复表结构
    mysqlfrm --diagnostic /tmp/t7_system_nodes.frm --quiet
    # WARNING: Cannot generate character set or collation names without the --server option.
    # CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
    CREATE TABLE  tmp. t7_system_nodes (
    id int(10) unsigned NOT NULL AUTO_INCREMENT comment '主键ID', 
    title varchar(150) DEFAULT NULL comment '节点名', 
    action varchar(60) DEFAULT NULL comment '路径名', 
    status tinyint(1) DEFAULT NULL comment '状态(1: 启用, 2: 禁用)', 
    remark varchar(765) DEFAULT NULL comment '备注', 
    sort smallint(4) DEFAULT NULL comment '排序', 
    pid int(11) DEFAULT NULL comment '父节点ID', 
    level tinyint(1) DEFAULT NULL comment '等级', 
    PRIMARY KEY  PRIMARY ( id)
    ) ENGINE=InnoDB COMMENT '系统节点表';
    第二步:在任意数据库创建该表 如test库
    注意,上一步的表名包含库名 tmp. t7_system_nodes 创建时需要把库名去掉或改成 test. t7_system_nodes
    mysql> CREATE TABLE  t7_system_nodes (
    id int(10) unsigned NOT NULL AUTO_INCREMENT comment '主键ID',
    title varchar(150) DEFAULT NULL comment '节点名',
    action varchar(60) DEFAULT NULL comment '路径名',
    status tinyint(1) DEFAULT NULL comment '状态(1: 启用, 2: 禁用)',
    remark varchar(765) DEFAULT NULL comment '备注',
    sort smallint(4) DEFAULT NULL comment '排序',
    pid int(11) DEFAULT NULL comment '父节点ID',
    level tinyint(1) DEFAULT NULL comment '等级',
    PRIMARY KEY  PRIMARY ( id)
    ) ENGINE=InnoDB COMMENT '系统节点表';
    Query OK, 0 rows affected
    第三步:卸载表空间
    mysql> alter table  t7_system_nodes discard tablespace ;
    Query OK, 0 rows affected
    该操作会删除t7_system_nodes.ibd文件
    第四步:复制t7_system_nodes.ibd文件
    将要还原数据的t7_system_nodes.ibd文件复制到数据库目录下,并修改文件属主
    cp /tmp/t7_system_nodes.ibd /data/mysql/test
    chown mysql:mysql /data/mysql/test/t7_system_nodes.ibd
    第五步:导入表空间
    mysql> alter table  t7_system_nodes import tablespace;
    Query OK, 0 rows affected
    第六步:检查数据是否已恢复
    mysql> select count(*) from  t7_system_nodes;
    +----------+
    | count(*) |
    +----------+
    | 75 |
    +----------+
    1 row in set
    数据已经恢复!
    该过程不需要重启数据库实例、不需要修改数据库配置、操作方便,推荐
    mysqlindexcheck

    mysqlindexcheck --server=user:pass@host:port:socket db1.table1 db2 db3.table2

    识别重复的和潜在冗余的表索引
    除了这些库的表mysql, INFORMATION_SCHEMA, performance_schema,可以扫描所有其他库

    选项:
    --show-drops 显示drop语句删除冗余索引
    -- show - indexes 显示每个表的索引

    mysqlindexcheck --server=mysql_77 ecard
    mysqlindexcheck --server=mysql_77 ecard --show-drops --show-indexes --report-indexes

    mysqlmetagrep

    mysqlmetagrep --server=user:pass@host:port:socket [options] pattern

    搜索数据库对象

    mysqlmetagrep --server=mysql_77 --pattern="d_"
    mysqlmetagrep --server=mysql_77 --pattern="%school%"
    mysqlmetagrep --server=mysql_77 -Gb --pattern="ent"  #-Gb正则匹配

    mysqlprocgrep

    mysqlprocgrep --server=user:pass@host:port:socket [options]

    搜索进程,并执行某些操作

    选项:
    --age 指定时间
    --match-xxx 指定匹配条件,xxx可以是 id user host db command info state
    -- kill - connection
    -- kill - query

    mysqlprocgrep --server=mysql_77
    mysqlprocgrep --server=mysql_77 --match-user=root  --kill-connection --match-state=sleep  #kill掉root用户状态为sleep的进程
    mysqlprocgrep --kill-connection --match-state=sleep --print-sql  #生成kill空闲进程的存储过程
    mysqlprocgrep --server=mysql_77 --match-user=lepei --age=5 --kill-query

    kill进程测试并不成功

    mysqluserclone

    mysqluserclone --source=user:pass@host:port:socket --destination=user:pass@host:port:socket joe@localhost sam:secret1@localhost

    以现有数据库上的用户作为模板创作一个或多个具有相同权限的账户

    选项:
    - d ,  -- dump 显示grant语句并不执行
    - l ,  -- list 列出所有用户

    # 显示所有用户
    mysqluserclone --source=mysql_77 --list -vvv
    # 复制admin用户到3308实例,用户名为user1,密码为passwd1,主机为10.%
    mysqluserclone --source=instance_3306 --destination=instance_3308 admin@localhost  user1:passwd1@10.% -vvv
    # 显示用户grant语句
    mysqluserclone --source=mysql_77 --dump --list

    mysqluc

    命令行工具,允许执行用户当前安装mysql工具的任何命令 --utildir用于指定MySQL Utilities安装路径
    支持tab 支持管道

    mysqluc -e "help utilities"
    mysqluc -e "set SRV=mysql_77; mysqldiskusage --server=$SRV" -vvv

    help
    help mysqldiskusage

    高可用性层面

    mysqlfailover
    - 提供对复制结构故障自动转移
    - 使用GTID
    mysqlreplicate
    - 设置复制
    - 从一开始 当前 特定binlog pos复制
    mysqlrplms
    - 提供round-robin multi-source复制
    - 使用GTID
    mysqlrpladmin
    - 管理复制拓扑
    - 允许恢复主
    - 命令包括 select failover gtid health start stop switchover
    mysqlrplcheck
    - 检查复制配置
    - 在主上测试二进制日志
    mysqlrplshow
    - 查看从连接到的主
    - 可递归搜索
    - 显示复制拓扑图或列表
    mysqlrplsync
    - 检查服务器之间数据一致性
    - 使用GTID

    mysqlfailover

    mysqlfailover --master=root@localhost --discover-slaves-login=root --candidates=root@host123:3306,root@host456:3306

    对复制进行健康检测和实现故障自动转移
    需要gtid_mode=ON
    所有从必需使用--report-host 和 --report-port 启动参数
    许用户指定外部脚本在切换和故障转移命令之前或之后执行

    --failover-mode
    auto 执行故障自动转移到第一候选人
    elect 与auto一样,但若在指定候选从列表中没有可行的,不检查剩余从,产生错误并退出
    fail 产生一个错误,不进行故障转移

    mysqlreplicate

    mysqlreplicate --master=root@localhost:3306 --slave=root@localhost:3310 --rpl-user=rpl:passwd

    在两台服务器间设置和启动复制

    mysqlrplms

    mysqlrplms --slave=root@localhost:3306 --masters=root@localhost:3310,root@localhost:3311 --rpl-user=rpl:passwd

    设置多主单从的复制,即从多个主复制

    mysqlrpladmin

    mysqlrpladmin --slaves=root@localhost:3306

    MySQL复制的管理工具

    mysqlrplcheck

    mysqlrplcheck --master=root@localhost:3306 --slave=root@localhost:3310

    检查复制的先决条件

    mysqlrplshow

    mysqlrplshow --master=root@localhost:3306

    显示主从复制关系,并绘制主的图形结构,标注每个主机名和端口

    mysqlrplsync

    mysqlrplsync --master=user:pass@host:port --slaves=user:pass@host:port [[.]]

    对复制同步进行检查,检查主从或从从之间的数据是否一致,并报告丢失的对象以及数据

     

    参考:https://blog.csdn.net/anzhen0429/article/details/78007341









    About Me

    ........................................................................................................................

    ● 本文作者:小麦苗,部分内容整理自网络,若有侵权请联系小麦苗删除

    ● 本文在itpub、博客园、CSDN和个人微 信公众号( DB宝)上有同步更新

    ● 本文itpub地址: http://blog.itpub.net/26736162

    ● 本文博客园地址: http://www.cnblogs.com/lhrbest

    ● 本文CSDN地址: https://blog.csdn.net/lihuarongaini

    ● 本文pdf版、个人简介及小麦苗云盘地址: http://blog.itpub.net/26736162/viewspace-1624453/

    ● 数据库笔试面试题库及解答: http://blog.itpub.net/26736162/viewspace-2134706/

    ● DBA宝典今日头条号地址: http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826

    ........................................................................................................................

    ● QQ群号: 230161599 、618766405

    ● 微 信群:可加我微 信,我拉大家进群,非诚勿扰

    ● 联系我请加QQ好友 646634621 ,注明添加缘由

    ● 于 2020-05-01 06:00 ~ 2020-05-30 24:00 在西安完成

    ● 最新修改时间:2020-11-19

    ● 文章内容来源于小麦苗的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解

    ● 版权所有,欢迎分享本文,转载请保留出处

    ........................................................................................................................

    小麦苗的微店https://weidian.com/s/793741433?wfr=c&ifr=shopdetail

    小麦苗出版的数据库类丛书http://blog.itpub.net/26736162/viewspace-2142121/

    小麦苗OCP、OCM、高可用网络班http://blog.itpub.net/26736162/viewspace-2148098/

    小麦苗腾讯课堂主页https://lhr.ke.qq.com/

    ........................................................................................................................

    使用 微 信客户端扫描下面的二维码来关注小麦苗的微 信公众号( DB宝)及QQ群(DBA宝典)、添加小麦苗微 信, 学习最实用的数据库技术。

    ........................................................................................................................

    欢迎与我联系

     

     



    请使用浏览器的分享功能分享到微信等