首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

命令行导出远程数据库数据

由于线上数据量太大,之前用的navicat直接导出数据的方法分分钟卡死,只好用程序跑,无奈,单线程跑的断了链接,有没有单点续传,整出来一堆问题,

老大给的直接导数据的方法,试了试发现行不通,自己搜了半下午终于找到一个貌似可以导出的方法,先记录下。

windows系统真的和linux存在很大差异,很多命令都可以执行

方法一

链接数据库:

语法格式:mysql -u用户名 -p密码 -h机器(ip) -P端口号 数据库名

eg:

mysql -u用户 -p密码 -h机器eg127.0.0.1 -P 3306 数据库名

运行结果:

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 42846

Server version: 5.6.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

连接成功

#show tables ;

#注意必须以;结尾回车才被执行,否则按照一行处理

尝试第一种方法:

语句

mysql -h机器eg127.0.0.1 -u用户 -p 密码 -A -e "select user_id from 数据表 where create_time > '2018-03-31 00:00:00' and remaining >= 10000 and last_modified_time 数据表_1_li.csv

结果报错:

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

'mysql -u用户 -p密码 -A -e "select user_id from 数据表 wher' at line 1

mysql> -h机器eg127.0.0.1 -u用户 -p密码 -A -e "select user_id from 数据表 where create_time>'2018-03-31 00:00:00'and remaining>10000 and last_modified_timeuser_c

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 '-h机器eg127.0.0.1 -u用户 -p密码 -A -e "select user_id from user_c' at line 1

没看出来语句哪里有问题,不管是自己写或者修改老大给的sql都没有效果

windows总是报错,搜索结果说语句写错了,有保留关键字,替换所有的都一个样子,所以采用方法二

方法二

select * from user_info into outfile './test.csv';

提示mysql设置的权限

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

##查看当前的-secure-file-priv

mysql> show variables like '%secure%';

+------------------+-----------------------+

| Variable_name | Value |

+------------------+-----------------------+

| secure_auth | ON |

| secure_file_priv | /var/lib/mysql-files/ |

+------------------+-----------------------+

2 rows in set (0.01 sec)

修改路径

mysql> select * from user_info into outfile '/var/lib/mysql-files/test.csv';

Query OK, 349 rows affected (0.09 sec)

去对应的路径下看 ,

如果 乱码 指定字符集

可以在语句后面加上CHARACTER SET utf8(gbk)等。

---------------------------------------------

第一种方法继续寻找错误,第二种勉强可以导出数据,

分库分表

表数据多大的时候可以采取这种方式。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180726G1HQ5W00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券