There are a lot of great morsels here. However, everyone is over complicating different pieces of the puzzle.
mroe said:
...the slave seems to hold the connection open (even after calling "SLAVE STOP"), meaning the tunnel is always open...
1.
It's not the slave mysqld's role to terminate an incoming SSH session. (That's closer to the way tunneling with netcat works, but this is not the place for that so let's not confuse the issue.) The master should terminate the connection because [a] it originated it [b] it is the one inside the network you wish to protect. If you want terminate the connection for the slave side call "kill -s HUP $pid_of_the_sshd_owned_by_mysqlmaster
". I'll say again, I don't see the use.
2.
Why would you ever call "SLAVE STOP
"? The slave is a slave. Since we're on the topic... The slave should never stop itself. If the slave is going to be stopped, the master server (see point 4.
) or the administrator should do it.
mroe said:
Updates from master to slave need to be near-real time (i.e. a couple of minutes latency is acceptable, but not more).
You can't have it both ways. You either have the slave IO thread running or it is out of date.
3.
Don't worry about the 'binlog position of master' that's what built in replication does.
4.
If you really have your heart set on disconnecting and reconnecting every minute, write a cron script on the master. It should do the following:
- Test a lock file and either exit or create it.
- Create your tunnel like so
ssh -N -R 3333:127.0.0.1:3306 mysqlmaster@slave.server &
mysql -hslave.server -e 'SLAVE START'
- Now you have to make a choice:
- Stay connected until the slave is caught up with the master. You can test for '0' in the output of the following (one line, ignore wrapping):
mysql -hslave.server -e 'show slave status\G'|awk '/Seconds_Behind_Master/{print $2}'
- Stay connected of a certain amount of time. Maybe you would
sleep
for this. Do research about your system to make sure it doesn't consume any CPU.
- MySQL is handles dropped connections gracefully, so this is optional:
mysql -hslave.server -e 'SLAVE STOP'
(infact, the SLAVE START
is optional if you use MASTER_CONNECT_RETRY)
kill
the pid of the ssh client from the 2nd bullet.
That should do it. Now keep in mind that you have to get the slave ready to replicate before this is going to work. Since you mention issuing a SLAVE STOP
, I'll take that to mean that you were able to get a consistent backup from the master to the slave and get the log position for the CHANGE MASTER
command. I'll just add that you need to specify MASTER_HOST='127.0.0.1, MASTER_PORT=3333
Obviously I have no problem taking the time to research, and explain in detail. I'm also trying to not insult anyone by explaining too much. If you want more detail, just ask me.