linux 执行mysql脚本

4.5.1.4. Executing SQL Statements from a Text File

The mysql client typically is used interactively, like this:

shell> mysql db_name




However, it is also possible to put your SQL statements in a file and then tell mysql to read its input from that file. To do so, create a text file text_file that contains the statements you wish to execute. Then invoke mysql as shown here:

shell> mysql db_name

 < text_file




If you place a USE db_name statement as the first statement in the file, it is unnecessary to specify the database name on the command line:

shell> mysql < text_file


If you are already running mysql , you can execute an SQL script file using the source command or /. command:

mysql> source file_name




mysql> /. file_name




在shell命令行同时输入 数据库连接选项和sql脚本及输出信息
[...]# mysql -h <host> -u <username> -D <database name> --password=<password> < /tmp/query.sql >>
/tmp/results.sql
 

3.5. Using mysql in Batch Mode

In the previous sections, you used mysql interactively to enter queries and view the results. You can also run mysql in batch mode. To do this, put the 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 running mysql 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 use mysql this way, you are creating a 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 the --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 developing a query, particularly 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 the error, then tell mysql 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 from a cron 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 output of SELECT 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, use mysql -t . To echo to the output the commands that are executed, use mysql -vvv .

You can also use scripts from the mysql prompt by using the source command or /. command:

mysql> source filename ; mysql> /. filename

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值