今天在學習MySQL時候,想要將文本文件的數據導入到數據庫中,卻發現一直報錯,換了導入文本的路徑也還是同樣的錯誤,錯誤顯示ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement。
select ......into outfile 是一種邏輯備份方法,它的恢復速度非常之快,比insert的插入速度還要快。它只能備份表中的數據,並不能包含表的結構。
然后在網上找解決辦法,找的方法在Linux 下也不怎么好用,最后找到了解決Linux下MySQL文件導入出錯的方法
出錯的原因是因為在MySQL 5.7.6版本之后,導入文件只能在secure_file_priv指定的文件夾下(也有原因是因為權限不夠)
方法一:
我們可以用show variables like '%secure%';命令顯示文件目錄
這樣將導入文件放在 /var/lib/mysql-files/文件夾下,之后再從這里導入就可以了
導出文件時候,也是將 文件導出到這個文件夾里。
root@localhost:mysql3306.sock [(none)]>show global variables like '%secure%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| require_secure_transport | OFF |
| secure_auth | ON |
| secure_file_priv | /data/mysql/mysql3306/tmp/ |
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
root@localhost:mysql3306.sock [(none)]>select * from qt into outfile '/data/mysql/mysql3306/tmp/qt.sql';
ERROR 1046 (3D000): No database selected
root@localhost:mysql3306.sock [(none)]>select * from qq.qt into outfile '/data/mysql/mysql3306/tmp/qt.sql'; //備份表
Query OK, 8 rows affected (0.01 sec)
[root@node1 ~]# cd /data/mysql/mysql3306/tmp/
[root@node1 tmp]# ll
total 4
-rw-rw-rw- 1 mysql mysql 56 Aug 13 06:06 qt.sql
[root@node1 tmp]# pwd
/data/mysql/mysql3306/tmp
恢復:
root@localhost:mysql3306.sock [(none)]>use qq;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
root@localhost:mysql3306.sock [qq]>select * from qt;
Empty set (0.01 sec)
root@localhost:mysql3306.sock [qq]>LOAD DATA INFILE '/data/mysql/mysql3306/tmp/qt.sql' into table qq qt;
ERROR 1064 (42000): 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 'qt' at line 1
root@localhost:mysql3306.sock [qq]>LOAD DATA INFILE '/data/mysql/mysql3306/tmp/qt.sql' into table qq.qt;
Query OK, 8 rows affected (0.00 sec)
Records: 8 Deleted: 0 Skipped: 0 Warnings: 0
root@localhost:mysql3306.sock [qq]>
如果顯示ERROR 1261 (01000): Row 1 doesn't contain data for all columns
這個錯誤,是因為數據行不匹配,默認不能有空,用下列命令解決set sql_modul = 0;
