Search code examples
mysqlcentosmariadbrestartreboot

Graceful reboot on CentOS from command line (worried about database corruption)


I have always been a WHM/Cpanel guy so whenever the need to gracefully reboot the server came around I just used the gui command in WHM to do so.

Now, I have a situation where I do not have WHM/Cpanel and need to do the same from command line after ssh'ing in. This is a vm inside a vpc (VMware) that is dedicated to MySQL/MariaDB only running centos. The kernel was recently updated through yum and the reboot is needed to take affect.

I have found quite a lot of differing opinions on this when searching such as just doing something as simple as :

# reboot
or
# shutdown -r

My main concerns are :

  1. Using the correct command for my situation
  2. This vm is strictly for our databases which use innodb, but of course MySQL itself uses myisam. The last thing I want to do is end up with corrupted data upon reboot and our db's don't function. Does the reboot take care of this or should I be doing a service mysql stop before the reboot? Along with that, will it restart automatically after reboot or is service mysql start going to be needed?

Solution

  • Both commands will call the required run-levels at shutdown and or reboot either way.

    shutdown -r and reboot are the same thing provided you just call them 'as is' with no additional options.

    shutdown -r now = reboot after shutdown.
    reboot = shutdown then reboot
    

    Extract from the usage help;

    [root@test ~]# reboot --help
    usage: reboot [-n] [-w] [-d] [-f] [-h] [-i]
            -n: don't sync before halting the system
            -f: force halt/reboot, don't call shutdown.
    

    Note that if you call shutdown -f it will force halt/reboot, and not call shutdown, likewise -n is not what you want. Doing this is bad as it won't stop services gracefully via there init scripts and you might end up with data loss and or corruption in your case.

    [root@test~]# shutdown --help
    Usage:    shutdown [-akrhHPfnc] [-t secs] time [warning message]
              -r:      reboot after shutdown.
    

    You may have noticed when CentOS starts it runs through a list of services and attempts to start them. At shutdown and reboots (normally) the same list is called to stop the services gracefully.

    If you are really very paranoid about your databases and want to be doubly sure they are stopped before you reboot then you can issue the stop command directly.

    `/etc/init.d/mysql stop`
    `/etc/init.d/mariadb stop`
    

    You will then get a confirmation that the services are stopped. Then you can issue your reboot or shutdown -r now command.

    On start up it should restart any services (like your Db's) provided they where installed correctly. You can check this by running the following command chkconfig --list then look down the list for the things your interested in and you should expect to see them 'start' at run-level 3 and 'stop' at run-level 6.

    e.g

    chkconfig --list
    mysqld          0:off   1:off   2:off   3:on    4:off   5:off   6:off