简体   繁体   中英

How could I import an sql dump into all databases in MySQL?

Is there a way to execute an import on all present databases instead of specifying each like mysql -uusername -ppw db_name < dump.sql ? In specific, I want to transfer all tables from one database to every single other database , so I dumped them, but since I have a ton of databases here, should I create a script or is there a workaround? I'd like to avoid making scripts as database names are encrypted so they resemble pseudorandom strings. Thanks in advance

There's nothing built-in like this, it's a fairly specific situation, and easily solved. This can be a one-liner in shell/bash. Retrieve your list of databases and feed them to a loop to load them.

  • Set default user with:
  • mysql_config_editor set -u username -p
  • enter your password

One liner:

mysql -NBe "select schema_name from information_schema.schemata where schema_name not in ('mysql','performance_schema','sys','information_schema','others...')" | while read dbname ; do date; echo "loading $dbname"; mysql $dbname < dump.sql"; done 

And block formatted for read-ability:

mysql -NBe "select schema_name from information_schema.schemata
where schema_name not in ('mysql','performance_schema','sys', 'information_schema','others...')" | while read dbname ; do
date;
echo "loading $dbname";
mysql $dbname < dump.sql";
done

Depending on exactly how many "a ton" of databases is, and your available resources, split the list in 2, and run 2 concurrent sessions.

You can write a script in Java or php where you keep varying the db name and script will import the sql file in all db. You can take a look into below question - Importing .sql file into MySQL using Java code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM