• MySQL知识库 :: security
  • How can I get exclusive control over MySQL?

  • Discussion

    When restoring a database or performing other system wide maintenance, you may want to have exclusive control over the server, to block all other users from having access. You can do this by stopping the MySQL server and then restarting it like so:

    mysqld --socket=/tmp/mysql_temp.sock --skip-networking

    The --socket option will temporarily name a different socket file for Unix systems--any name will do.

    The --skip-networking option prevents users from accessing the server via TCP/IP and the socket file or named pipes. It will only permit connections from the localhost. This still allows access from a console physically on the server, or through a secure shell connection. This option with a different socket name will prevent outsiders and API scripts on the server from being able to access the database until you're able to restore the data.

    Once you have the server under your exclusive control, you can then proceed without worrying about other users trying to access the server while you're recovering data or other maintenance. To access the server from the mysql client, you would some like the following:

    mysql -u root -pmypwd --socket=/tmp/mysql_temp.sock

    For restoring a mysqldump file while access is restricted, you could enter something like the following:

    mysql -u root -pmypwd --socket=/tmp/mysql_tmp.sock \
        < /var/backup/20050420.sql

    When you finish working on the server, you can give access to other users again by restarting the server as you normally would, but without the --skip-networking option.