본문 바로가기

Database/MySQL

MySQL shell exportTable() 사용하기

tablelist="
T1
T2
"




file_path=/home/chlee/data
mysqlsh_path=$HOME/mysqlsh/bin

HOST=127.0.0.1
PASSWD=pass
DATABASE=test
USER=root

CMD="$mysqlsh_path/mysqlsh -h $HOST -u$USER -p$PASSWD --mysql"

function exportAll()
{
   for table in $tablelist
   do
      exportTable ${table%.*}
   done
}

function exportTable()
{
   table=$1
   echo "export $table"  
   time $CMD -e "util.exportTable('${DATABASE}.$table' , 'file://$file_path/${table}.csv' , {dialect: 'csv-unix'})"
}

if [ "$#" = 0 ];
then
   exportAll
elif [ "$#" = 1 ];
then
   exportTable $1
fi