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`;

注意:当你数据有问题时,比如违反了唯一约束,外健问题等,这行命令照样执行,只是有问题的数据的不会导入进去

你可能感兴趣的:(数据库)