1. 程式人生 > >mysql 命令匯出,匯入 查詢結果

mysql 命令匯出,匯入 查詢結果

需求:某些特殊的資料要同步到其他伺服器

匯出

命令結構: select … into outfile ‘儲存路徑+檔名’;

SELECT * FROM user where id between 2 and 20 into outfile '/opt/user.sql';

問題
LZ,執行這條命令時,報如下異常

ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

解決
執行如下命令

 show variables like
'%secure%';

結果如下

+--------------------------+-----------------------+
| Variable_name            | Value                 |
+--------------------------+-----------------------+
| require_secure_transport | OFF                   |
| secure_auth              | ON                    |
| secure_file_priv         |
/var/lib/mysql-files/ | +--------------------------+-----------------------+

找到secure_file_priv所對應的路徑,改掉即可

SELECT * FROM user where id between 2 and 20 into outfile '/var/lib/mysql-files/user.sql';

匯入

命令結構:load data local infile ‘檔案路徑’ into table 表名 character set utf8;

load data local infile "/opt/user.sql"
into table `user`;

注意:當你資料有問題時,比如違反了唯一約束,外健問題等,這行命令照樣執行,只是有問題的資料的不會匯入進去