2

hej there!

I'm maintaining a network of 8 Pandaboards. One of them is acting as server to host a php-based browser game, others are clients running that game.

The server runs Debian Wheezy from SD card, which is pretty slow. To improve game performance on clients I moved mysql data dir and tmp dir to tmpfs and changed my.cnf accordingly. Data doesn't need to be synced back to SD because it isn't changed, only temporary game statistics are written to db.

Right now this is accomplished through additions to rc.local:

# mount tmpfs
sudo mount -t tmpfs tmpfs /var/tmpfs
# put a fresh copyy of mysql data there
sudo rsync -a /var/lib/mysql/ /var/tmpfs/mysql/
# restart mysql service
sudo service mysql restart

This solution is working but I suppose it's not the most elegant one, since mysql starts with modified my.cnf, won't find data in tmpfs yet and produces errors on first start. I worked around this by delaying the clients' initial game page request for 3 seconds.

How can I start mysql AFTER all data is synced to tmpfs? Add a script which does mount and rsync to all /etc/rc*.d where there's a Sxxmysql? Are rc*.d directories for services only?

Any help is appreciated.

1

You could use a service script yourself (good) or include it in the mysql script (temp. workarround, ugly, wont work after updates.)

something like /etc/init.d/mysql should exist.

then you would search for start and before mysql is actually started you would add your part of the script.

(might differ a bit with newer versions though)

on one of my hosts this would look like it:

# 
# main()
#
>
case "${1:-''}" in
'start')
    sanity_checks;
    # Start daemon 
    log_daemon_msg "Starting MySQL database server" "mysqld"
    if mysqld_status check_alive nowarn; then
       log_progress_msg "already running"
       log_end_msg 0
    else
       setup_chroot
        # Could be removed during boot
        test -e /var/run/mysqld || install -m 755 -o mysql -g root -d /var/run/mysqld

        # mount tmpfs
        sudo mount -t tmpfs tmpfs /var/tmpfs
        # put a fresh copyy of mysql data there
        sudo rsync -a /var/lib/mysql/ /var/tmpfs/mysql/

        # Start MySQL!   
        /usr/bin/mysqld_safe > /dev/null 2>&1 &

You might want to consider adding some sanity checks etc.

Note that as you said, a solution which would actually use the dependancys of the boot process would be more elegant:

So you could make your mounting a "service" and include it into the booot process, and make mysql depend on the service beeing started before. (i think you could include it in the mount service but i am not sure about this)

  • 1
    Thanks for your answer(s). I ended up mounting tmpfs via fstab and putting rsync command into 'mysql' in init.d, at the beginning of the function 'sanity checks'. Everything works fine now, pages load instantly instead of being delayed 1-3 seconds. Took me a while to find this is important: The mysql script runs sanety checks first and will exit if it doesn't find data dir (because it hasn't been rsync'ed yet). So don't put 'rsync' into any of the cases 'start|stop|restart'. The script will exit much earlier. – ronso0 Jun 30 '14 at 18:52
  • The complete solution won't fit in the comments. Soon I can answer my question myself. Steep learnig curve... o_o – ronso0 Jun 30 '14 at 19:19
1

Dennis pointed me in the right direction. In the comments I can't clearly show what worked out for me, so I'll answer my question based on his input.

Backup & edit /etc/fstab. Added:

tmpfs  /path/to/mount/point  tmpfs  rw  0 0

Backup & edit /etc/init.d/mysql. I have put my code at the beginning of sanity checks, before [start|stop] cases:

sanity_checks() {
  # check wether /var/tmpfs/ already contains /mysql directory
  if [ ! -d /var/tmpfs/mysql ]; then
     # copy fresh mysql data to tmpfs
     rsync -a /var/lib/mysql/ /var/tmpfs/mysql/
     # temp log message for debugging
     log_daemon_msg "Just synced MySQL data directory to /var/tmpfs" "Cool!"
     log_end_msg 0
  # also optional:
  else
     log_daemon_msg "MySQL data dir already in sync" "Supercool!"
     log_end_msg 0
  fi

  [ other checks ]
}

I'm happy with this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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