tags:

views:

20

answers:

1

ALTER TABLE can easily take a few days - and during this time there's a non-negligible chance of connection getting terminated due to network problems. Is it possible to start ALTER TABLE (or CREATE TABLE ... SELECT ...; or some other very long running query) and leave it running without keeping connection open all the time?

(the obvious solution of screen + console mysql client won't easily work as there's no ssh running on that server, only mysqld).

A: 

Put your alter table command in a script that you will call from a shell script like this

#!/usr/bin/ksh

muser=username
mpass=password
mhost=hostname
mdb=databasename

mysql --user=$muser --password=$mpass --host=$mhost $mdb < alter.sql

then call the shell script with nohup

nohup myscript.ksh >logfile 2>&1 &

You can log out of the shell session immediately afterwards. You may get a warning message that you have running jobs, but it is safe to ignore.

You can probably skip the sql script and put the commands in the shell script. I just don't recall the syntax

coolgeek
If it wasn't immediately clear from my answer, you can run this from any linux/unix box that can remotely connect to the mysql database
coolgeek
This will not work at all, because as I said, there's only mysql on the server, not any sort of ssh. If I start a script such on another machine, network connection can fail during all the days it runs.
taw
first, you're connecting to the database via the mysql command in the shell script, not via ssh. second, read the man page for nohup. this is exactly what it is designed for - long-running jobs where there is a risk that the connection will be terminated. note also that I'm logging the output and putting the job in the background
coolgeek