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
RolandoMySQLDBA
  • 185,223
  • 33
  • 326
  • 536
Mike B
  • 617
  • 4
  • 10
  • 17

2 Answers2

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 !!!).

RolandoMySQLDBA
  • 185,223
  • 33
  • 326
  • 536
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

Sachi.Dila
  • 121
  • 3