CSV 형식의 mysqldump
MySQL의 모든 테이블을 CSV 형식 으로 덤프해야 합니다.
사용하여 명령 거기 mysqldump
에 단지 CSV 형식의 모든 테이블에 대한 출력 모든 행은?
먼저 하나의 테이블에 대한 답을 드릴 수 있습니다.
이 모든 INTO OUTFILE
또는 --tab=tmpfile
(및 -T/path/to/directory
) 답변 의 문제 는 MySQL 서버 와 동일한 서버 에서 mysqldump 를 실행 하고 해당 액세스 권한 이 있어야한다는 것 입니다.
내 솔루션은 매개 변수 와 함께 mysql
(NOT mysqldump
) 를 사용 -B
하고 SELECT 문을으로 인라인 -e
한 다음 ASCII 출력을로 마사지하고 sed
헤더 필드 행을 포함하는 CSV로 마무리하는 것입니다.
예:
mysql -B -u username -p password database -h dbhost -e "SELECT * FROM accounts;" \
| sed "s/\"/\"\"/g;s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g"
"id", "login", "password", "folder", "email" "8", "mariana", "xxxxxxxxxx", "mariana", "" "3", "squaredesign", "xxxxxxxxxxxxxxxxx", " squaredesign ","mkobylecki@squaredesign.com ""4 ","miedziak ","xxxxxxxxxx ","miedziak ","miedziak@mail.com ""5 ","Sarko ","xxxxxxxxx ","Sarko "," "" "6", "Logitrans Poland", "xxxxxxxxxxxxxx", "LogitransPoland", "" "7", "Amos", "xxxxxxxxxxxxxxxxxxxx", "Amos", "" "9", "Annabelle", "xxxxxxxxxxxxxxxx", "애나벨", "" "11", "Brandfathers and Sons ","xxxxxxxxxxxxxxxxx ","BrandfathersAndSons "," ""12 ","Imagine Group ","xxxxxxxxxxxxxxxx ","ImagineGroup "," ""13 ","EduSquare.pl ","xxxxxxxxxxxxxxxxx ","EduSquare .pl "," ""101 ","tmp ","xxxxxxxxxxxxxxxxxxxxx ","_ ","WOBC-14.squaredesign.atlassian.net@yoMama.com "atlassian.net@yoMama.com "atlassian.net@yoMama.com "
> outfile.csv
해당 테이블에 대한 CSV 파일을 얻으려면 해당 한 줄 끝에를 추가하십시오 .
다음으로 모든 테이블 목록을 가져옵니다.
mysql -u username -ppassword dbname -sN -e "SHOW TABLES;"
거기에서 루프를 만드는 것은 단 한 단계뿐입니다. 에서 bash
해당 테이블을 반복 쉘 :
for tb in $(mysql -u username -ppassword dbname -sN -e "SHOW TABLES;"); do
echo .....;
done
사이 do
와 ; done
나는 위의 제 1 부에 쓴, 그러나 당신의 TABLENAME을 대체 긴 명령을 삽입 $tb
하는 대신.
이 명령은 / path / to / directory table_name.sql 및 table_name.txt에 2 개의 파일을 생성합니다.
SQL 파일에는 테이블 생성 스키마가 포함되고 txt 파일에는 쉼표로 구분 된 필드가있는 mytable 테이블의 레코드가 포함됩니다.
mysqldump -u username -p -t -T/path/to/directory dbname table_name --fields-terminated-by=','
mysqldump에는 CSV 형식화 옵션이 있습니다.
--fields-terminated-by=name
Fields in the output file are terminated by the given
--lines-terminated-by=name
Lines in the output file are terminated by the given
는 name
다음 중 하나를 포함해야
`--fields-terminated-by`
\t
또는 "\""
`--fields-enclosed-by=name`
출력 파일의 필드는 주어진
과
--lines-terminated-by
\r
\n
\r\n
당연히 각 테이블을 개별적으로 mysqldump해야합니다.
모든 테이블 이름을 텍스트 파일에 모으는 것이 좋습니다. 그런 다음 mysqldump를 실행하는 모든 테이블을 반복합니다. 다음은 한 번에 10 개의 테이블을 덤프하고 gzip하는 스크립트입니다.
MYSQL_USER=root
MYSQL_PASS=rootpassword
MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
SQLSTMT="SELECT CONCAT(table_schema,'.',table_name)"
SQLSTMT="${SQLSTMT} FROM information_schema.tables WHERE table_schema NOT IN "
SQLSTMT="${SQLSTMT} ('information_schema','performance_schema','mysql')"
mysql ${MYSQL_CONN} -ANe"${SQLSTMT}" > /tmp/DBTB.txt
COMMIT_COUNT=0
COMMIT_LIMIT=10
TARGET_FOLDER=/path/to/csv/files
for DBTB in `cat /tmp/DBTB.txt`
do
DB=`echo "${DBTB}" | sed 's/\./ /g' | awk '{print $1}'`
TB=`echo "${DBTB}" | sed 's/\./ /g' | awk '{print $2}'`
DUMPFILE=${DB}-${TB}.csv.gz
mysqldump ${MYSQL_CONN} -T ${TARGET_FOLDER} --fields-terminated-by="," --fields-enclosed-by="\"" --lines-terminated-by="\r\n" ${DB} ${TB} | gzip > ${DUMPFILE}
(( COMMIT_COUNT++ ))
if [ ${COMMIT_COUNT} -eq ${COMMIT_LIMIT} ]
then
COMMIT_COUNT=0
wait
fi
done
if [ ${COMMIT_COUNT} -gt 0 ]
then
wait
fi
시도 해봐 !!!
MySQL 또는 MariaDB를 사용하는 경우 단일 테이블에 대한 CSV 덤프를 가장 쉽고 효율적으로 수행하는 방법은 다음과 같습니다.
SELECT customer_id, firstname, surname INTO OUTFILE '/exportdata/customers.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM customers;
이제 다른 기술을 사용하여 여러 테이블에 대해이 명령을 반복 할 수 있습니다. 여기에서 자세한 내용을 참조하십시오.
- https://mariadb.com/kb/en/the-mariadb-library/select-into-outfile/
- https://dev.mysql.com/doc/refman/5.7/en/select-into.html
다른 사람들도이 문제를 겪은 것처럼 보입니다. 이제 mysqldump의 출력을 csv 파일로 변환 하는 간단한 python 스크립트가 있습니다.
wget https://raw.githubusercontent.com/jamesmishra/mysqldump-to-csv/master/mysqldump_to_csv.py
mysqldump -u username -p --host=rdshostname database table | python mysqldump_to_csv.py > table.csv
이것은 나를 위해 잘 작동했습니다.
mysqldump DBNAME --fields-terminated-by ',' \
--fields-enclosed-by '"' --fields-escaped-by '\' \
--no-create-info --tab /var/lib/mysql-files/
또는 TABLENAME 만 덤프하려는 경우 :
mysqldump DBNAME TABLENAME --fields-terminated-by ',' \
--fields-enclosed-by '"' --fields-escaped-by '\' \
--no-create-info --tab /var/lib/mysql-files/
/var/lib/mysql-files/
이 오류를 피하기 위해 덤프 중입니다 .
mysqldump: Got error: 1290: The MySQL server is running with the --secure-file-priv option so it cannot execute this statement when executing 'SELECT INTO OUTFILE'
You also can do it using Data Export tool in dbForge Studio for MySQL.
It will allow you to select some or all tables and export them into CSV format.
Thats a lot of messing around just to export as csv, why dont you just install Dbeaver from enter link description here
It covers most of the popular databases except nosql,and can export in a variety of formats including csv and sql.
You could using the easy more method, csvdump tool to print remote csv file from mysql: https://github.com/bobby96333/csvdump
참고URL : https://stackoverflow.com/questions/12040816/mysqldump-in-csv-format
'Development Tip' 카테고리의 다른 글
Win7 64 비트, Python 2.6.4에 PIL (Python Imaging Library) 설치 (0) | 2020.11.19 |
---|---|
부분보기에 매개 변수 전달 (0) | 2020.11.19 |
인라인 블록으로 새 줄을 끊으셨습니까? (0) | 2020.11.19 |
Swift Playground는 UIKit을 지원합니까? (0) | 2020.11.19 |
NSFileManager fileExistsAtPath : isDirectory 및 swift (0) | 2020.11.19 |