简体   繁体   中英

How to create a MySQL database from dump file in expect/TCL script?

I just wanted to execute this bash command in expect script:

mysql -u root -h localhost -proot dbTest < temp.sql

I added spawn to the beginning but it is not working. I think "<" symbol means nothing in expect!

Can anyone help me to solve this issue?

spawn does not support the < direction but you can do like this:

spawn sh -c "mysql -u root -h localhost -proot dbTest < temp.sql"

Seems like you want to run mysql in the non-interactive way so you can also use Expect 's system command:

system "mysql -u root -h localhost -proot dbTest < temp.sql"

or Tcl 's exec command:

exec mysql -u root -h localhost -proot dbTest < temp.sql >@ stdout 2>@ stderr

You may need to put the whole system or exec command in a catch block in case the mysql fails:

catch {system "mysql ..."} catched
# or
catch {exec mysql ...} catched

People tend to use sqldump and mysqldump. I like the quote in the below link saying rubbish with phpmyadmin:

how to import a very large query over phpmyadmin?

Concerning cron or expect ...

I know cron has trouble say with dates and often what works at a command line has to be shoved in a bash script for easier work. Then cron runs the .sh script versus embedded string.

Also it is a duplicate question of yours from 2 hours ago.

I have now found a solution for this problem. I avoided the "<" symbol, so we can use this command instead:

spawn mysql -u root -h localhost -proot dbTest -Bse "source temp.sql"

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