How should I tackle --secure-file-priv in MySQL?

前端 未结 21 1086
攒了一身酷
攒了一身酷 2020-11-22 06:04

I am learning MySQL and tried using a LOAD DATA clause. When I used it as below:

LOAD DATA INFILE \"text.txt\" INTO table mytable;
相关标签:
21条回答
  • 2020-11-22 06:17

    MySQL use this system variable to control where you can import you files

    mysql> SHOW VARIABLES LIKE "secure_file_priv";
    +------------------+-------+
    | Variable_name    | Value |
    +------------------+-------+
    | secure_file_priv | NULL  |
    +------------------+-------+
    

    So problem is how to change system variables such as secure_file_priv.

    1. shutdown mysqld
    2. sudo mysqld_safe --secure_file_priv=""

    now you may see like this:

    mysql> SHOW VARIABLES LIKE "secure_file_priv";
    +------------------+-------+
    | Variable_name    | Value |
    +------------------+-------+
    | secure_file_priv |       |
    +------------------+-------+
    
    0 讨论(0)
  • 2020-11-22 06:18

    This worked for me (had the additional problem of not being able to use LOCAL with my current MySQL version in the statement LOAD DATE INFILE ... )

    sudo /usr/local/mysql/support-files/mysql.server start --secure-file-priv='' --local-infile
    

    The above works for that given path on my machine; you may have to adjust your path.

    Then use:

    mysql -u root -p
    

    One important point is that you should have the CSV in the MySQL data folder. In my machine it is located at: /usr/local/mysql-8.0.18-macos10.14-x86_64/data

    You can change the folder permission if needed to drop a CSV in the data folder.

    Setup:
    macOS Catalina version 10.15.5
    MySQL version 8.0.18

    0 讨论(0)
  • 2020-11-22 06:20

    If the file is local to your machine use the LOCAL in your command

    LOAD DATA LOCAL INFILE "text.txt" INTO table mytable;
    
    0 讨论(0)
  • 2020-11-22 06:20

    @vhu I did the SHOW VARIABLES LIKE "secure_file_priv"; and it returned C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\ so when I plugged that in, it still didn't work.

    When I went to the my.ini file directly I discovered that the path is formatted a bit differently:

    C:/ProgramData/MySQL/MySQL Server 8.0/Uploads
    

    Then when I ran it with that, it worked. The only difference was the direction of the slashes.

    0 讨论(0)
  • 2020-11-22 06:21

    in Linux you have to edit my.cnf file in

    /etc/mysql/my.cnf
    

    and change 26 line number param like this :

    secure-file-priv= <your data path directory like /home/user/data>
    

    then restart your MySQL and try again.

    in docker you have to mount your my.cnf file with my.cnf file in your container with this command in docker-compose or add manually :

    volumes:
      - ./persistent:/var/lib/mysql
      - ./conf/my.cnf:/etc/mysql/my.cnf
    

    next change /conf/my.cnf in your host and config secure-file-priv param like the upper approach, in addition, you have to mount your data in mysql container and set that path for secure-file-priv param and restart your services and finally, you can load your data.

    you can check your config with this command :

    SHOW VARIABLES LIKE "secure_file_priv";
    
    0 讨论(0)
  • 2020-11-22 06:25

    I had this problem on windows 10. "--secure-file-priv in MySQL" To solve this I did the following.

    1. In windows search (bottom left) I typed "powershell".
    2. Right clicked on powershell and ran as admin.
    3. Navigated to the server bin file. (C:\Program Files\MySQL\MySQL Server 5.6\bin);
    4. Typed ./mysqld
    5. Hit "enter"

    The server started up as expected.

    0 讨论(0)
提交回复
热议问题