Resolving the MySQL "Command Not Allowed" Error When Loading Data
Encountering the MySQL error "ERROR 1148: The used command is not allowed with this MySQL version" while using LOAD DATA LOCAL
means the LOCAL
keyword is disabled. This is a security measure. To load data locally, you must explicitly enable the local-infile
option.
Client-Side Solution:
The simplest solution is to enable local-infile
when connecting to your MySQL server:
<code class="language-bash">mysql -u myuser -p --local-infile mydatabase</code>
Replace myuser
with your MySQL username, -p
prompts for your password, and mydatabase
with the name of your database.
Important Security Note: Enabling local-infile
introduces a security vulnerability. Only enable it when absolutely necessary and disable it afterward.
Server-Side Solution (More Permanent, but Requires Caution):
For a more permanent solution, enable local-infile
on the MySQL server itself. This requires modifying the MySQL configuration file (my.cnf
on Linux/macOS, my.ini
on Windows). Exercise extreme caution when modifying server configuration files. Incorrect changes can compromise your server's security.
Add or modify the following line in your my.cnf
file:
<code>loose-local-infile = 1</code>
Then, restart your MySQL server for the changes to take effect. After data loading is complete, it's strongly recommended to set loose-local-infile
back to 0 or remove the line to mitigate the security risk.
Alternative Approach (Recommended):
The safest and generally preferred method is to avoid LOAD DATA LOCAL
altogether. Instead, use a method that doesn't involve loading data directly from the client's local file system. This could involve:
mysqlimport
: This command-line tool is a safer alternative for importing data from a file.scp
, sftp
, or other secure file transfer methods, then use LOAD DATA INFILE
(without the LOCAL
keyword) to load the data from the server's file system.Remember to always prioritize security when working with databases. Enabling local-infile
should be a temporary measure, and the alternative approaches are generally safer and recommended for long-term data loading.
The above is the detailed content of How to Fix the 'Command Not Allowed with MySQL Version' Error When Using LOAD DATA LOCAL?. For more information, please follow other related articles on the PHP Chinese website!