r - 为什么加载 mysql 在命令行中有效,但在 R 中使用 system() 则无效?

标签 r system

我正在尝试将一些 sql 文件加载到 mySql 中。在命令行中它可以工作:

C:\SomeDirectory\R>mysql -u root -p1234 hat < hat.
2015-10-16T01-10-03
mysql: [Warning] Using a password on the command line interface can be insecure.

C:\SomeDirectory\R>

在 R 中,我使用 system 在同一工作目录中调用相同的命令,但它不起作用:(7zip 有效)

> command1
[1] "7z e hat.2015-10-16T01-10-03.gz"
> system(command1, intern=T)
 [1] ""                                                                        
 [2] "7-Zip [64] 15.06 beta : Copyright (c) 1999-2015 Igor Pavlov : 2015-08-09"
 [3] ""                                                                        
 [4] "Scanning the drive for archives:"                                        
 [5] "1 file, 3518447 bytes (3436 KiB)"                                        
 [6] ""                                                                        
 [7] "Extracting archive: hat.2015-10-16T01-10-03.gz"                          
 [8] "--"                                                                      
 [9] "Path = hat.2015-10-16T01-10-03.gz"                                       
[10] "Type = gzip"                                                             
[11] "Headers Size = 10"                                                       
[12] ""                                                                        
[13] "Everything is Ok"                                                        
[14] ""                                                                        
[15] "Size:       13868282"                                                    
[16] "Compressed: 3518447"



> command1
[1] "mysql -u root -p1234 hat < hat.2015-10-16T01-10-03"
> system(command1, intern=T)
  [1] "mysql: [Warning] Using a password on the command line interface can be insecure."                                                              
  [2] "C:\\PROGRA~1\\MySQL\\MYSQLS~1.7\\bin\\mysql.exe  Ver 14.14 Distrib 5.7.7-rc, for Win64 (x86_64)"                                               
  [3] "Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved."                                                                  
  [4] ""                                                                                                                                              
  [5] "Oracle is a registered trademark of Oracle Corporation and/or its"                                                                             
  [6] "affiliates. Other names may be trademarks of their respective"                                                                                 
  [7] "owners."                                                                                                                                       
  [8] ""                                                                                                                                              
  [9] "Usage: C:\\PROGRA~1\\MySQL\\MYSQLS~1.7\\bin\\mysql.exe [OPTIONS] [database]"                                                                   
 [10] "  -?, --help          Display this help and exit."                                                                           
....                
SEEMS TO BE BIG CHUNK OF MYSQL MANUAL
....


[148] "  --connect-expired-password "                                                                                                                 
[149] "                      Notify the server that this client is prepared to handle"                                                                
[150] "                      expired password sandbox mode."                                                                                          
[151] ""                                                                                                                                              
[152] "Default options are read from the following files in the given order:"                                                                         
[153] "C:\\Windows\\my.ini C:\\Windows\\my.cnf C:\\my.ini C:\\my.cnf C:\\PROGRA~1\\MySQL\\MYSQLS~1.7\\my.ini C:\\PROGRA~1\\MySQL\\MYSQLS~1.7\\my.cnf "
[154] "The following groups are read: mysql client"                                                                                                   
[155] "The following options may be given as the first argument:"                                                                                     
[156] "--print-defaults        Print the program argument list and exit."                                                                             
[157] "--no-defaults           Don't read default options from any option file,"                                                                      
[158] "                        except for login file."                                                                                                
[159] "--defaults-file=#       Only read default options from the given file #."                                                                      
[160] "--defaults-extra-file=# Read this file after the global files are read."                                                                       
[161] "--defaults-group-suffix=#"                                                                                                                     
[162] "                        Also read groups with concat(group, suffix)"                                                                           
[163] "--login-path=#          Read this path from the login file."                                                                                   
[164] ""                                                                                                                                              
[165] "Variables (--variable-name=value)"                                                                                                             
[166] "and boolean options {FALSE|TRUE}  Value (after reading options)"                                                                               
[167] "--------------------------------- ----------------------------------------"                                                                    
[168] "auto-rehash                       TRUE"                                                                                                        
[169] "auto-vertical-output              FALSE"                                                                                                       
[170] "bind-address                      (No default value)"                                                                                          
[171] "character-sets-dir                (No default value)"                                                                                          
[172] "column-type-info                  FALSE"                                                                                                       
[173] "comments                          FALSE"                                                                                                       
[174] "compress                          FALSE"                                                                                                       
[175] "database                          (No default value)"                                                                                          
[176] "default-character-set             auto"                                                                                                        
[177] "delimiter                         ;"                                                                                                           
[178] "enable-cleartext-plugin           FALSE"                                                                                                       
[179] "vertical                          FALSE"                                                                                                       
[180] "force                             FALSE"                                                                                                       
[181] "histignore                        (No default value)"                                                                                          
[182] "named-commands                    FALSE"                                                                                                       
[183] "ignore-spaces                     FALSE"                                                                                                       
[184] "init-command                      (No default value)"                                                                                          
[185] "local-infile                      FALSE"                                                                                                       
[186] "no-beep                           FALSE"                                                                                                       
[187] "host                              (No default value)"                                                                                          
[188] "html                              FALSE"                                                                                                       
[189] "xml                               FALSE"                                                                                                       
[190] "line-numbers                      TRUE"                                                                                                        
[191] "unbuffered                        FALSE"                                                                                                       
[192] "column-names                      TRUE"                                                                                                        
[193] "sigint-ignore                     FALSE"                                                                                                       
[194] "port                              0"                                                                                                           
[195] "prompt                            mysql> "                                                                                                     
[196] "quick                             FALSE"                                                                                                       
[197] "raw                               FALSE"                                                                                                       
[198] "reconnect                         FALSE"                                                                                                       
[199] "shared-memory-base-name           (No default value)"                                                                                          
[200] "socket                            (No default value)"                                                                                          
[201] "ssl                               TRUE"                                                                                                        
[202] "ssl-ca                            (No default value)"                                                                                          
[203] "ssl-capath                        (No default value)"                                                                                          
[204] "ssl-cert                          (No default value)"                                                                                          
[205] "ssl-cipher                        (No default value)"                                                                                          
[206] "ssl-key                           (No default value)"                                                                                          
[207] "ssl-crl                           (No default value)"                                                                                          
[208] "ssl-crlpath                       (No default value)"                                                                                          
[209] "ssl-verify-server-cert            FALSE"                                                                                                       
[210] "table                             FALSE"                                                                                                       
[211] "user                              root"                                                                                                        
[212] "safe-updates                      FALSE"                                                                                                       
[213] "i-am-a-dummy                      FALSE"                                                                                                       
[214] "connect-timeout                   0"                                                                                                           
[215] "max-allowed-packet                16777216"                                                                                                    
[216] "net-buffer-length                 16384"                                                                                                       
[217] "select-limit                      1000"                                                                                                        
[218] "max-join-size                     1000000"                                                                                                     
[219] "secure-auth                       TRUE"                                                                                                        
[220] "show-warnings                     FALSE"                                                                                                       
[221] "plugin-dir                        (No default value)"                                                                                          
[222] "default-auth                      (No default value)"                                                                                          
[223] "binary-mode                       FALSE"                                                                                                       
[224] "connect-expired-password          FALSE"                                                                                                       
attr(,"status")
[1] 1
Warning message:
running command 'mysql -u root -p1234 hat < hat.2015-10-16T01-10-03' had status 1 

Mysql在系统PATH中:

> Sys.getenv('PATH')
[1] "C:\\Program Files\\R\\R-3.2.1\\bin\\x64;C:\\ProgramData\\Oracle\\Java\\javapath;C:\\Program Files (x86)\\Intel\\iCLS Client\\;C:\\Program Files\\Intel\\iCLS Client\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;C:\\Program Files\\SASHome\\Secure\\ccme4;C:\\Program Files\\SASHome\\x86\\Secure\\ccme4;C:\\Windows\\system32\\config\\systemprofile\\.dnx\\bin;C:\\Program Files\\Microsoft DNX\\Dnvm\\;C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin;C:\\Program Files (x86)\\Skype\\Phone\\;C:\\Program Files\\7-Zip"

有人现在有什么问题吗?任何帮助表示赞赏。谢谢!


更新: shell 工作了

> command1
[1] "mysql -u root -p1234 hat < hat.2015-10-16T01-10-03"
> shell(command1)
mysql: [Warning] Using a password on the command line interface can be insecure.

system2 还不能工作,b/c 我不知道命令的哪一部分属于 argument

> system2('mysql -u root -p1234 hat', args = "hat.2015-10-16T01-10-03")
Warning message:
running command '"mysql -u root -p1234 hat" hat.2015-10-16T01-10-03' had status 127 
>         system2('mysql -u root -p1234', args = 'hat < hat.2015-10-16T01-10-03')
Warning message:
running command '"mysql -u root -p1234" hat < hat.2015-10-16T01-10-03' had status 127 

最佳答案

由于您使用的是 Windows,因此您无法像在控制台上那样通过 system 调用 shell 命令。

阅读文档了解详细信息。它特别指出:

This means that it cannot be assumed that redirection or piping will work in system (redirection sometimes does, but we have seen cases where it stopped working after a Windows security patch), and system2 (or shell) must be used on Windows.

因此,简而言之,解决方案是:使用 system2,或者不使用重定向(您可以将标准输入和输出流指定为 system 的单独命令)相反)。

关于r - 为什么加载 mysql 在命令行中有效,但在 R 中使用 system() 则无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33186377/

相关文章:

c# - 系统提示音是什么原因?

r - Vim:下划线(_)自动转换为(<-)

linux - 如何为用户空间应用程序交叉编译系统tap脚本

arrays - 数组初始化中的 Perl qx 执行顺序

r - 按不同 id 合并数据框列表

linux - 我用过的内存去哪儿了?

php - 可以使用 PHP 重新启动 linux 服务器或重新启动 dhcp 吗?

r - 如何使用 MUTATE : growth rate? 添加新变量

r - 在 R markdown 中批量创建文档

r - 在 ggplot2 中使用颜色转换时,更改图例渐变而不是图例中断位置