能源采矿MySQL

linux下执行mysql的sql脚本文件报错?

source  mysql_test.sqlmysql: [Warning] Using a password on the command line interface can be insecure.'RROR 1102 (42000): Incorrect database name 'bash: show: command not found...: command not found...bash: use: command not found...: command not fou...显示全部

source  mysql_test.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
'RROR 1102 (42000): Incorrect database name '
bash: show: command not found...
: command not found...
bash: use: command not found...
: command not found...
-bash: mysql_test.sql: line 5: syntax error near unexpected token `from'
'bash: mysql_test.sql: line 5: `select host,user,authentication_string from user;

脚本内容:
mysql -u root -pXXXXXXX
show databases;
use mysql;
select host,user,authentication_string from user;
show global variables like '%timeout%';
select version();
show variables;
show status;
show table status \G;
netstat -lnpt | grep 3306
cat .mysql_history
cat /etc/my.cnf

进入mysql后逐条执行可以(后三条在系统下执行)。为什么source  sql文件就报乱七八糟的错误呢?

收起
参与6

返回gm12367的回答

gm12367gm12367DevOpsUniversal

看报错应该是database这个参数错误,从脚本内容来看,你并没有指定database这个参数,可能是由于你的脚本格式问题,句末的换行符被解释器认为是一个错误格式。据以往经验,你可能是在windows下编写的文本,直接上传到Linux下边用,导致换行符变成\r,可以尝试把脚本文件用dos2unix转换一下格式。

一般sql脚本可以直接在终端用mysql命令登录之后,直接在mysql cli下source你的sql文件路径。如果你想在bash下非交互式导入,必须用mysql命令配合管道符导入。

参考Zabbix的数据库结构导入命令:

zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

针对你情况:
cat ./mysql_test.sql | mysql -u root -pXXXXXXX

IT咨询服务 · 2020-04-16
浏览2633
  • use mysql; 这个不就是指定数据库吗? 一句句执行没问题的。 刚开始我也怀疑句末符号的问题,我用vi编辑器重新写了一遍,还是报错呢
    2020-04-17
  • [此评论已删除]
    2020-04-17
  • 那就是我说的第二个问题了,如果需要在shell下导入sql文件,必须把mysql命令写外边,然后用输入流导入sql文件,以下是man帮助的说明,供参考: 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
    2020-04-17

回答者

gm12367
DevOpsUniversal
擅长领域: 云计算容器云容器

gm12367 最近回答过的问题

回答状态

  • 发布时间:2020-04-16
  • 关注会员:2 人
  • 回答浏览:2633
  • X社区推广