假如SQL文件位于D:\java\opensource\mysql\pet.sql;内容简单的为use menagerie;select * from pet;

    当连接mysql服务是:

    1. mysql> source D:/java/opensource/mysql/pet.sql;
    2. +----------+--------+---------+------+------------+------------+
    3. | name | owner | species | sex | birth | death |
    4. +----------+--------+---------+------+------------+------------+
    5. | Fluffy | Harold | cat | f | 1993-02-04 | NULL |
    6. | Claws | Gwen | cat | m | 1994-03-17 | NULL |
    7. | Buffy | Harold | dog | f | 1989-05-13 | NULL |
    8. | Fang | Benny | dog | m | 1990-08-27 | NULL |
    9. | Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
    10. | Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
    11. | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
    12. | Slim | Benny | snake | m | 1996-04-29 | NULL |
    13. | Puffball | Diane | hamster | f | 1999-03-30 | NULL |
    14. +----------+--------+---------+------+------------+------------+
    15. 9 rows in set (0.00 sec)

    未连接mysql服务是:

    C:\Users\Zk_Fo>mysql -uroot -p < D:\java\opensource\mysql\pet.sql
    Enter password: ****
    name    owner   species sex     birth   death
    Fluffy  Harold  cat     f       1993-02-04      NULL
    Claws   Gwen    cat     m       1994-03-17      NULL
    Buffy   Harold  dog     f       1989-05-13      NULL
    Fang    Benny   dog     m       1990-08-27      NULL
    Bowser  Diane   dog     m       1979-08-31      1995-07-29
    Chirpy  Gwen    bird    f       1998-09-11      NULL
    Whistler        Gwen    bird    NULL    1997-12-09      NULL
    Slim    Benny   snake   m       1996-04-29      NULL
    Puffball        Diane   hamster f       1999-03-30      NULL
    
    C:\Users\Zk_Fo>
    

    也可以将结果定向输出到文本文件中

    C:\Users\Zk_Fo>mysql -uroot -p < D:\java\opensource\mysql\pet.sql >d:\java\opensource\mysql\pet_result.txt
    Enter password: ****
    
    C:\Users\Zk_Fo>