Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to export mysql/mongo to a local file

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report