7

I have a large-ish (several GB) mysql DB that I'm planning to import via

mysql -u root -p mydb < mydb.sql

in a SSH session.

What would happen if my SSH connection drops?

Is it safer to run something like

nohup mysql -u root -p mydb < mydb.sql

Or does mysql not care?

Using the following

  • MySQL 5.1.45
  • CentOS 5.x

2 Answers 2

4

If the SSH drops without putting the mysql session into the background (along with nohup), then the DB connection is at the SSH session's mercy. So, yes, mysql would care about the lifecycle of the SSH session.

You should launch it with & at the end as follows:

nohup mysql -u root -p mydb < mydb.sql &

Then, you can walk away feeling OK (you could actually logout on purpose at this point) because process 1 (init) will pick up the process ID that is running the mysql session upon the SSH session termination (voluntary or involuntary) because it was suddenly orphaned (What a great dad !!!).

4
  • Thanks. As a follow-up question, how would I know if/when the db import is complete?
    – Mike B
    Commented Jun 7, 2016 at 0:57
  • There should be a nohup.out file in the folder you launched this. If the file is 0 bytes, then it finished without any issues. Commented Jun 7, 2016 at 1:00
  • Sorry.. I'm still missing something... if the nohup.out file doesn't write any status message when complete, how can I determine if it's still in the process of transferring the db? Maybe add a -vvv to the mysql command?
    – Mike B
    Commented Jun 7, 2016 at 14:47
  • Play around with a small-ish table.
    – Rick James
    Commented Jun 23, 2016 at 5:23
2

Little bit late for the party, But myself struggled with this issue and found a solution and wanted to share.

Solution 1

Using screen is ideal for this situation as your processes will run even after you close your terminal.

sudo apt install screen

Open up a screen

screen

Now you can execute you command here and import the mysql db dump.

mysql -u username -p database_name < sqldump.sql or by simply login into your database and importing the file source /var/www/html/db/my_database.sql;

To exit from the screen “ctrl + a + d”

you can list your available screens by

screen -ls and simply open the screen by screen -r

when the job is done simply open the screen and use exit command to close the screen.


Solution 2

Second way is to use nohup where I noticed sometime nohup also terminates when session closes. To sort that you have to use disown

nohup mysql -u'root' -p'password' databasename < sqldump.sql & > sql_load.log 2>sql_load_err.log disown %1

1
  • or tmux - same concept.
    – danblack
    Commented Mar 30, 2020 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.