Get the App
SLTechnology News&Howtos  ›  Database  › 

How to export mysql/mongo to a local file

Shulou Source: shulou.com Published: 2022-05-31 12:31:25 09月22日 Update

This article is about how to export mysql/mongo to a local file. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

one。 Mongo export to local

#! / bin/bash

Set-e

# variable declaration

Database= ""

Table= ""

Columns= ""

Query= ""

Datafile_path= ""

# function definition

Usage () {

Echo "usage (): $0 [- d database name] [- t table name] [- c column name] [- Q query condition]" 1 > & 2

Exit 1

}

# execution

# Detection parameters assign values to the corresponding variables

While getopts "d:t:c:q:" opt

Do

Case "$opt" in

D) database=$OPTARG

# t) table=$ (echo $OPTARG | tr'[Amurz]'[amerz]')

T) table=$OPTARG

C) columns=$OPTARG

Q) query=$OPTARG

*) usage

Esac

Done

Shift $[$OPTIND-1]

Echo "mongoexport-- host $mongo_ip-- port $mongo_port-u $mongo_user-p $mongo_password-- authenticationDatabase=admin-- db ${database}-- collection ${table}-- readPreference='secondaryPreferred'-f ${columns}-query" {${query}} "--type=csv"

Mongoexport-- host $mongo_ip-- port $mongo_port-u $mongo_user-p $mongo_password-- authenticationDatabase=admin-- db ${database}-- collection ${table}-- readPreference='secondaryPreferred'-f ${columns}-- query "{${query}}"-- type=csv | tail-naught2 | sed 's/ObjectID [(]\ ([0-9aMusi ZZ -]]\ +\)] /\ 1api' > ${table} .csv.

two。 Mysql export to local

#! / bin/bash

Set-e

# variable declaration

Database= ""

Table= ""

Columns= ""

Where= ""

Datafile_path= ""

# function definition

Usage () {

Echo "usage (): $0 [- d database name] [- t table name] [- c column name] [- w filter condition]" 1 > & 2

Exit 1

}

# execution

# Detection parameters assign values to the corresponding variables

While getopts "d:t:c:w:" opt

Do

Case "$opt" in

D) database=$OPTARG

T) table=$ (echo $OPTARG | tr'[Amurz]'[Amurz]')

C) columns=$OPTARG

W) where=$OPTARG

*) usage

Esac

Done

Shift $[$OPTIND-1]

# echo "database=$ {database}, table=$ {table}, columns=$ {columns}, where=$ {where}"

# Database links

Mysql_bin= "mysql-h$mysql_ip-P$mysql_port-u$mysql_user-p$mysql_password-- database=$ {database}"

# generate SQL statement

SQL_STR= ""

If [${where} = = "]; then

SQL_STR= "select" ${columns} "from" ${table} ";"

Else

SQL_STR= "select" ${columns} "from" ${table} "where" ${where} ";"

Fi

Echo ${SQL_STR}

# execute SQL statement to import to local file

$mysql_bin-N-e "${SQL_STR}" > ${table} .csv

Thank you for reading! This is the end of this article on "how to export mysql/mongo to a local file". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Variables files data databases contents functions parameters more conditions articles statements detection good practical articles see knowledge links references help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft macOS Apple Linux