Run the MySQL script in Linux.

Source: Internet
Author: User
4.5.1.4. Executing SQL statements from a text file









TheMySQL


Client typically is used
Interactively, like this:

shell> mysql db_name

However, it is also possible to put your SQL statements in
File and then tellMySQL


To read its input
From that file. To do so, create a text file
text_file

That contains
Statements you wish to execute. then invoke
MySQL


As shown here:

shell> mysql db_name < text_file

If you placeUSE
db_name


Statement as
First Statement in the file, it is unnecessary to specify
Database Name on the command line:

shell> mysql < text_file

If you are already runningMySQL


, You can
Execute an SQL script file usingsource
Command or/.
Command:

Mysql>source file_nameMysql>/. file_name
Enter the database connection options, SQL scripts, and output information at the same time in the shell command line.
[...] # Mysql-H /Tmp/results. SQL
 
3.5. Using MySQL

In batch mode






In the previous sections, you usedMySQL


Interactively to enter queries and view the results. You can also
RunMySQL


In batch mode. To do this, put
Commands you want to run in a file, then tell
MySQL


To read its input from the file:

Shell>mysql < batch-file


If you are runningMySQL


Under Windows and have
Some special characters in the file that cause problems, you can
Do this:

C:/>mysql -e "source batch-file

"

If you need to specify connection parameters on the command line,
The command might look like this:

Shell>mysql -h host

-u user

-p < batch-file



Enter password:********

When you useMySQL


This way, you are creating
Script file, then executing the script.

If you want the script to continue even if some of the statements
In it produce errors, you should use
--force

Command-line option.

Why use a script? Here are a few reasons:

  • If you run a query repeatedly (say, every day or every week ),
    Making it a script allows you to avoid retyping it each time
    You execute it.

  • You can generate new queries from existing ones that are
    Similar by copying and editing script files.

  • Batch mode can also be useful while you're re developing a query,
    Participant ularly for multiple-line commands or multiple-Statement
    Sequences of commands. If you make a mistake, you don't have
    To retype everything. Just edit your script to correct
    Error, then tellMySQL


    To execute it again.

  • If you have a query that produces a lot of output, you can run
    The output through a pager rather than watching it scroll off
    The top of your screen:

    shell> mysql < batch-file | more
  • You can catch the output in a file for further processing:

    shell> mysql < batch-file > mysql.out
  • You can distribute your script to other people so that they
    Can also run the commands.

  • Some situations do not allow for interactive use, for example,
    When you run a query fromCron

    Job. In
    This case, you must use batch mode.

The default output format is different (more concise) when you run
MySQL


In batch mode than when you use it
Interactively. For example, the outputSELECT DISTINCT
species FROM pet

Looks like this when
MySQL


Is run interactively:

+ --------- +
| Species |
+ --------- +
| Bird |
| Cat |
| Dog |
| Hamster |
| Snake |
+ --------- +

In batch mode, the output looks like this instead:

Species
Bird
Cat
Dog
Hamster
Snake

If you want to get the interactive output format in batch mode,
Usemysql -t
. To echo to the output
Commands that are executed, usemysql -vvv
.




You can also use scripts fromMySQL


Prompt
By usingsource
Command or
/.
Command:

Mysql>source filename

;


Mysql>/. filename


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.