找到你要的答案

Q:shell save MySQL query result to a file

Q:壳牌节省MySQL查询结果文件

I have a script which runs a MySQL query, something like this:

#!/bin/sh

user="root"
pwd="test"
database="mydb"
command="long... 
         long... query in
         multiple lines" 

mysql -u $user -p$pwd << EOF 
use $database;
$command
EOF

This query does a backup from a table to another. Is it possible to save the query result in a file without using mysql INTO OUTFILE? I only want to know if the query failed or succeeded.

If it succeeded something like 1 row(s) affected or if it failed Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ...

Update

Solution 1: () act as a chain of commands, so wrapping them in a variable gets the result of those commands. Then simply output the result of that variable in a file.

output=$( mysql -u $user -p$pwd << EOF 
use $database;
$command
EOF
)
echo "$output" >> /events/mysql.log

Solution 2: use tee as system command to send the result of the commands into a file, but this needs to be done from the crontab, like this:

*/1 * * * * root sh /events/mysql.sh |tee -a /events/mysql.log

http://forums.mysql.com/read.php?10,391070,391983#msg-391983

My working solution:

user="root"
pwd="root12345"
database="mydb"
command="long ...long query"

mysql -u $user -p$pwd << EOF >> /events/mysql.log 2>&1
use $database;
$command;
EOF

我有一个脚本运行MySQL查询,这样的事情:

#!/bin/sh

user="root"
pwd="test"
database="mydb"
command="long... 
         long... query in
         multiple lines" 

mysql -u $user -p$pwd << EOF 
use $database;
$command
EOF

此查询从表中备份到另一个表。可以保存查询结果文件中没有使用MySQL为导出的文件吗?我只想知道查询失败或成功。

如果它成功了一些像1行(s)受影响或如果它失败了错误代码:1064。你有在您的SQL语法错误;检查对应于你的MySQL服务器版本正确的语法手册…

更新

解决方案1:()充当命令链,所以将它们封装在一个变量中会得到这些命令的结果。然后只需在文件中输出该变量的结果。

output=$( mysql -u $user -p$pwd << EOF 
use $database;
$command
EOF
)
echo "$output" >> /events/mysql.log

解决方案2:使用三通系统命令发送的命令的结果为一个文件,但这需要从crontab做的,像这样:

*/1 * * * * root sh /events/mysql.sh |tee -a /events/mysql.log

http://forums.mysql.com/read.php?10391070391983 # msg-391983

我的工作解决方案:

user="root"
pwd="root12345"
database="mydb"
command="long ...long query"

mysql -u $user -p$pwd << EOF >> /events/mysql.log 2>&1
use $database;
$command;
EOF
answer1: 回答1:

This should work:

output=$( mysql -u $user -p$pwd << EOF 
use $database;
$command
EOF
)
echo "$output" >> outfile

这应该工作:

output=$( mysql -u $user -p$pwd << EOF 
use $database;
$command
EOF
)
echo "$output" >> outfile
answer2: 回答2:

you can also do it like this:

    #!/bin/sh

    user="root"
    pwd="test"
    database="mydb"
    command="long... 
     long... query in
     multiple lines" 
    mysql -u$user -p$pwd -D$database -e "$command" > file

你也可以这样做:

    #!/bin/sh

    user="root"
    pwd="test"
    database="mydb"
    command="long... 
     long... query in
     multiple lines" 
    mysql -u$user -p$pwd -D$database -e "$command" > file
answer3: 回答3:

It's easier to use the MySQL tee command to send output.

To get the logging process started, just use the tee command,

tee /tmp/my.out;

#!/bin/sh

user="root"
pwd="test"
database="mydb"
$pathToFile = "/tmp/my.out"
command="long... 
         long... query in
         multiple lines" 

mysql -u $user -p$pwd << EOF 
tee $pathToFile
use $database;
$command
EOF

EDIT Since tee is reportedly not working inside script, you could also log output using Tee directly when running the script.

mysql_bash.sh > >(tee -a script.log)

这是使用mysql命令发送输出更容易通。

要获取日志记录进程,只需使用,

发球/甲氧苄啶/ my.out;

#!/bin/sh

user="root"
pwd="test"
database="mydb"
$pathToFile = "/tmp/my.out"
command="long... 
         long... query in
         multiple lines" 

mysql -u $user -p$pwd << EOF 
tee $pathToFile
use $database;
$command
EOF

EDIT Since tee is reportedly not working inside script, you could also log output using Tee directly when running the script.

mysql_bash.sh > >(tee -a script.log)
answer4: 回答4:

The correct way to handle error-messages is to through stderr. Use 2>&1 to catch the error.

So, add this to the end of your script:

>> install.log 2>&1

正确的方法是通过内部处理错误信息。使用2 >;&;1捕捉错误。

所以,把这个添加到脚本的结尾:

>;>;install.log 2 >;&;1

linux  shell