有个文本保存了所有的sql语句,一般我的做法都是打开客户端,拷贝粘贴到mysql的客户端,然后执行,现在看来有很多方法:
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 tellmysql to read its input from that file. To do so, create a text filetext_file
that contains the statements you wish to execute. Then invokemysql as shown here:
shell> mysql db_name
< text_file
If you place a USE
statement as the first statement in the file, it is unnecessary to specify the database name on the command line:db_name
shell> mysql < text_file
If you are already running mysql, you can execute an SQL script file using thesource
command or \.
command:
mysql>source
mysql>file_name
\.
file_name
Sometimes you may want your script to display progress information to the user. For this you can insert statements like this:
SELECT '<info_to_display>' AS ' ';
The statement shown outputs <info_to_display>
.
You can also invoke mysql with the --verbose
option, which causes each statement to be displayed before the result that it produces.
As of MySQL 5.0.54, mysql ignores Unicode byte order mark (BOM) characters at the beginning of input files. Previously, it read them and sent them to the server, resulting in a syntax error. Presence of a BOM does not causemysql to change its default character set. To do that, invokemysql with an option such as --default-character-set=utf8
.
For more information about batch mode, see Section 3.5, “Using mysql in Batch Mode”.