简体   繁体   English

命令行MySQL脚本

[英]Command Line MySQL Script

Been trying to figure out a few ways around this, and back to square 1. 一直试图找出一些解决方法,然后回到第1方块。

My hosting package only apparently has supporting software for PHP, SendMail and Perl from the command line - but I need to figure out the best way to run an SQL script, whether it be from the command line as a MySQL script (if that's possible), or within a PHP or Perl script. 我的托管软件包显然仅从命令行提供了针对PHP,SendMail和Perl的支持软件-但是我需要找出运行SQL脚本的最佳方法,无论它是从命令行作为MySQL脚本(如果可能) ,或者在PHP或Perl脚本中。

I have tried the following script within a PHP script, but the LOAD DATA LOCAL INFILE prevents it from running properly, so I'm stumped as to an easier solution... but at this stage I'll take any! 我已经在PHP脚本中尝试了以下脚本,但是LOAD DATA LOCAL INFILE阻止其正常运行,因此我为一个更简单的解决方案而烦恼...但是在这个阶段,我会采取任何措施!

<?php
$con = mysqli_connect('xxx.xxx.xxx.xx', 'xxxx-xxxxxxx', 'xxxxxxxxx', 'xxxx-xxxxxxx');
mysqli_query($con, "LOAD DATA LOCAL INFILE 'http://www.mydomain.co.uk/datafeed.csv' REPLACE INTO TABLE product_feed FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS");
mysqli_close($con);

Command lines are very new to me, and to be honest I only need to know as it's how I construct a schedule task/cron job for an update script. 命令行对我来说是很新的,老实说,我只需要知道,因为它是我为更新脚本构造计划任务/计划任务的方式。

Can anyone help me out 谁能帮我吗

Where are you running this from? 您是从哪里运行的? I presume www.mydomain.co.uk is your server, and you're accessing it from an external client? 我假设www.mydomain.co.uk是您的服务器,并且您正在从外部客户端访问它?

The main difference between LOAD DATA and LOAD DATA LOCAL is that the latter copies a file from the client to a temporary file in the server before continuing as an ordinary LOAD DATA command using the copied file; LOAD DATALOAD DATA LOCAL之间的主要区别在于,后者在使用复制的文件作为普通LOAD DATA命令继续之前,将文件从客户端复制到服务器中的临时文件。 but both the MySQL client and the server must have been built explicitly to enable it . 但是MySQL客户端和服务器都必须显式构建以使其启用 Since it is a potential security hole I would doubt very much if the server has been built that way. 由于这是一个潜在的安全漏洞,因此我非常怀疑服务器是否采用这种方式构建。

Neither will accept an HTTP URL -- you need to specify a path . 两者都不接受HTTP URL-您需要指定路径 If you use LOAD DATA specifying just a bare filename then the server will look for it in the default database directory ; 如果使用LOAD DATA仅指定裸文件名,则服务器将在默认数据库目录中查找该文件名; or if it is a relative path with a directory component then the server treats it as being relative to the data directory ; 或者如果它是具有目录组件的相对路径,则服务器将其视为相对于数据目录相对路径; absolute paths are, of course, used literally. 当然,绝对路径是按字面意义使用的。

How you get this to work depends on which server directories you can write to that the MySQL server can also read. 如何使它起作用,取决于您可以向哪些服务器目录写入,而MySQL服务器也可以读取该服务器目录。 There is little to be gained from LOAD DATA LOCAL as you can copy the CSV file yourself and have more control over it. LOAD DATA LOCAL几乎LOAD DATA LOCAL因为您可以自己复制CSV文件并对其进行更多控制。 The most likely candidate is the data directory, and you can discover where that is using 最可能的候选对象是数据目录,您可以发现正在使用的目录

SHOW VARIABLES WHERE Variable_Name = "datadir"

Then, if you put your file myfile.csv into this directory, you can use 然后,如果将文件myfile.csv放入此目录,则可以使用

LOAD DATA INFILE "./myfile.csv" ...

to get the server to load the table from your file. 使服务器从文件中加载表。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM