• MySQL之mysql客户端工作的批处理一些使用手法


    通常我们会用mysql这个客户端程序来连接mysql库、这个通常是工作在交互式模式下的、如我们连接上mysql并执行如下操作:

    mysql -uroot -h127.0.0.1 -P3306
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 96
    Server version: 5.7.17-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)
    
    Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    mysql> 

    这个时候我们就可以通过sql语句来完成与数据库的交互操作了如:

    use tempdb;
    Database changed
    mysql> desc person;
    +-------+------------+------+-----+---------+----------------+
    | Field | Type       | Null | Key | Default | Extra          |
    +-------+------------+------+-----+---------+----------------+
    | id    | int(11)    | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(8) | YES  | UNI | NULL    |                |
    +-------+------------+------+-----+---------+----------------+
    2 rows in set (0.01 sec)
    
    mysql> select * from person;
    +----+------+
    | id | name |
    +----+------+
    |  1 | a    |
    |  2 | b    |
    |  3 | c    |
    +----+------+
    3 rows in set (0.00 sec)
    
    mysql> 

    说了这么多只是为了把今天的主角引出来、这个就是mysql批处理模式

    1、批处理方式执行单条语句

    mysql -uroot -h127.0.0.1 -P3306 -e"select * from tempdb.person"
    +----+------+
    | id | name |
    +----+------+
    |  1 | a    |
    |  2 | b    |
    |  3 | c    |
    +----+------+

    2、以输入重定向的方式执行一个sql脚本文件

    cat /tmp/script.sql 
    select * from tempdb.person;
    
    
    mysql -h127.0.0.1 -uroot </tmp/script.sql 
    id    name
    1    a
    2    b
    3    c

    3、以source 语句的方式执行一个sql脚本文件

    mysql -h127.0.0.1 -uroot -e"source /tmp/script.sql"
    +----+------+
    | id | name |
    +----+------+
    |  1 | a    |
    |  2 | b    |
    |  3 | c    |
    +----+------+

    4、不知道你有没有发现不同的执行方式对应的输出格式会有不同、如例2中的输出就没有格式化成表格、这里我们要讲一下

    mysql批处理模式下对输出格式的定制 -t 以表格方式输出 -v 输出的同时打印所执行的sql语句

    mysql -h127.0.0.1 -uroot </tmp/script.sql -t
    +----+------+
    | id | name |
    +----+------+
    |  1 | a    |
    |  2 | b    |
    |  3 | c    |
    +----+------+
    
    
    mysql -h127.0.0.1 -uroot </tmp/script.sql -v
    --------------
    select * from tempdb.person
    --------------
    
    id    name
    1    a
    2    b
    3    c
    
    
    mysql -h127.0.0.1 -uroot </tmp/script.sql -tv
    --------------
    select * from tempdb.person
    --------------
    
    +----+------+
    | id | name |
    +----+------+
    |  1 | a    |
    |  2 | b    |
    |  3 | c    |
    +----+------+

    ------

  • 相关阅读:
    git 基本使用
    docker下rabbitMQ高可用集群部署
    成长路上破局思维:工具化时间管理
    图解Elasticsearch的核心概念
    先森林后树木:Elasticsearch各版本升级核心内容必看
    JRebel 破解最简单的使用
    POA理论:不要被你的目标欺骗了你
    读了《跃迁-成为高手的技术》我的工资翻倍了
    微信头像地址失效踩坑记附带方案
    如何做程序员喜欢的测试妹子?
  • 原文地址:https://www.cnblogs.com/JiangLe/p/6604267.html
Copyright © 2020-2023  润新知