mysql spj -uroot -p123456 -e “select * from s;” > C:\backup\s.txt
echo "select * from table into outfile '/tmp/table.csv' fields terminated by ',' escaped by '\\' lines terminated by '\n';"|/usr/bin/mysql -uroot database
outfile
1 2
SELECT * FROM test INTO OUTFILE "E:\\test\\a.csv" FIELDS TERMINATED BY "," ENCLOSED BY '' LINES TERMINATED BY '\n'
SELECT '用户ID', '姓氏', '名字', '电子邮箱' UNION ALL SELECT id, first_name, last_name,email FROM kalacloud_users INTO OUTFILE '/tmp/kalacloud_users_out_c.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n';
SET @table_name = "a"; SET @schema_name = "mydb"; SET @output_name = CONCAT("C://",@table_name,".csv"); SET @cols = NULL; select GROUP_CONCAT(CONCAT("'",COLUMN_NAME,"'") ORDER BY ordinal_position SEPARATOR ",") INTO @cols from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @table_name AND TABLE_SCHEMA = @schema_name; SET @sql = CONCAT(" SELECT * FROM (SELECT ", @cols, " UNION ALL SELECT * FROM ", @table_name, ") as r ", " INTO OUTFILE '", @output_name, "' FIELDS TERMINATED BY '\\t' "); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
# write the table into 1.log file $MYSQL -u root -p****** >1.log << EOF #1.log中是tab分割的sql结果 $statement EOF
#cat the 1.log file and convert it to csv file cat 1.log | while read line do echo $line | tr " " "," #sql结果被转为逗号分割的csv done > $database"_"$table.csv
由于整个过程在shell中执行,可以较好的接受外部入参。
执行sql文件
1 2 3
mysqldump -uroot -p dbname > dbname .sql mysql -u用户名 -p密码 数据库名 < 数据库名.sql mysql -h10.253.29.10 -uroot -p密码 -e"source /opt/rh/test.sql"
或者进行mysql交互终端后执行source
1
mysql>source /home/xxxx/dbname.sql;
导入no header
load data infile
1 2 3 4 5 6
load data infile 'C:\\Users\\UserName\\Desktop\\test.csv' into table `table` fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\n';
echo "load data infile "/tmp/table.csv" replace into table table fields escaped by '\\' terminated by ',' lines terminated by '\n';"|/usr/bin/mysql -uroot database
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/address_book.csv' INTO TABLE address_book FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 ROWS;
load data infile 'student.csv' into table student FIELDS TERMINATED BY ',' -- csv字段分隔符 ENCLOSED BY '"' LINES TERMINATED BY '\n' -- csv行分隔符 IGNORE 1 ROWS -- 去除表头 (@col1,@col2,@col3,@col4) -- @colN 代表csv文件的第N列的值 set name=@col1,class=@col2,description=@col3,score=@col4; -- 每个列分别赋值
LOAD DATA INFILE 'C:\\Users\\Administrator\\Desktop\\ky\\grxx.csv' INTO TABLE `grxx` #要插入的表 CHARACTER SET utf8 #设置表编码 FIELDS TERMINATED BY ',' #字段以,分隔 OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' #字段以"包围 LINES TERMINATED BY '\r\n' #换行符表示一行结束 IGNORE 1 LINES #忽略第一行 (`index`, `ybh`, `idnum`, `name`, `sex`, `birthday`, `companycode`, `companyname`, `phone`, `dept`); #csv文件中数据按此顺序插入表中
不确定第一行列名忽略后,是否按照列次序导入,还是会自动做自动映射
如果导入时需要做数据格式转换则
1 2 3 4 5 6 7 8
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/address_book.csv' INTO TABLE address_book FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 ROWS (Mobile,@Date,Address) SET Date = STR_TO_DATE(@Date, '%m/%d/%Y');