天天看點

Linux 平台MySQL啟動關閉方式總結

<b></b>

MySQL的啟動方法有很多種,下面對比、總結這幾種方法的一些差異和特性,下面實驗的版本為MySQL 5.6。如有疏漏或不足,敬請指點一二。

<b></b> 

<b>1:使用mysqld啟動、關閉MySQL服務</b>

   mysqld是MySQL的守護程序,我們可以用mysqld來啟動、關閉MySQL服務,關于mysqld, MySQL 5.6官方介紹資料如下所示:

mysqld, also known as MySQL Server, is the main program that does most of the work in a MySQL installation. MySQL Server manages access to the MySQL data directory that contains databases and tables. The data directory is also the default location for other information such as log files and status files.

When MySQL server starts, it listens for network connections from client programs and manages access to databases on behalf of those clients.

The mysqld program has many options that can be specified at startup. For a complete list of options, run this command:

shell&gt; mysqld --verbose --help

MySQL Server also has a set of system variables that affect its operation as it runs. System variables can be set at server startup, and many of them can be changed at runtime to effect dynamic server reconfiguration. MySQL Server also has a set of status variables that provide information about its operation. You can monitor these status variables to access runtime performance characteristics.

如果MySQL是rpm方式安裝的話,mysqld位于/usr/sbin下,如果MySQL是二進制安裝的話,mysqld則位于bin目錄下面。

<b>2:使用mysqld_safe啟動、關閉MySQL服務</b>

很多時候,人們會糾結mysqld與mysqld_safe的差別. 其實mysqld_safe是一個腳本,一個非常安全的啟動、關閉MySQL服務的腳本。它實際上也是調用mysqld來啟動、關閉MySQL服務。關于mysqld_safe,可以參考官方文檔mysqld_safe — MySQL Server Startup Script

<a href="http://images2015.cnblogs.com/blog/73542/201607/73542-20160720115656669-70311494.png"></a>

<b>3:使用mysql.server啟動、關閉MySQL服務</b>

mysql.server其實也是一個腳本,它通過調用msqld_safe來啟動、關閉MySQL服務。部分腳本腳本如下

<b>4:使用mysqld_multi啟動、關閉MySQL服務</b>

當伺服器上運作了多個MySQL執行個體時,mysqld_multi是一個非常棒的管理MySQL伺服器的工具。當然在使用前,你必須提前做配置

mysqld_multi is designed to manage several mysqld processes that listen for connections on different Unix socket files and TCP/IP ports. It can start or stop servers, or report their current status.

mysqld_multi searches for groups named [mysqldN] in my.cnf (or in the file named by the --defaults-file option). N can be any positive integer. This number is referred to in the following discussion as the option group number, or GNR. Group numbers distinguish option groups from one another and are used as arguments tomysqld_multi to specify which servers you want to start, stop, or obtain a status report for. Options listed in these groups are the same that you would use in the[mysqld] group used for starting mysqld. (See, for example, Section 2.10.5, “Starting and Stopping MySQL Automatically”.) However, when using multiple servers, it is necessary that each one use its own value for options such as the Unix socket file and TCP/IP port number. For more information on which options must be unique per server in a multiple-server environment, see Section 5.6, “Running Multiple MySQL Instances on One Machine”.

<b>5:使用service 啟動、關閉MySQL服務</b>

其實如果你對service比較熟悉的話,就會知道運作上面指令,其實是service指令去找/etc/init.d下的相關的mysql腳本去執行啟動、關閉動作。

<b>6: 使用/etc/init.d/mysql啟動、關閉MySQL服務。</b>

    如果你非常了解方法5,那麼就多了這麼一個啟動資料庫的方式。其實/etc/init.d/mysql也是一個腳本,它調用mysqld_safe腳本來啟動MySQL服務。如下所示,你會看到相關代碼

<a href="http://images2015.cnblogs.com/blog/73542/201607/73542-20160720115658607-620100305.png"></a>

<b>6:使用mysqladmin關閉資料庫</b>

      mysqladmin是一個執行管理操作的客戶程式,這個指令可以使用安全模式關閉資料庫,但是不能啟動資料庫。當然它可以停止和啟動MySQL replication on a slave server

[root@DB-Server bin]# /usr/bin/mysqladmin -u root -p shutdown

Enter password:

參考資料:

<a href="http://blog.csdn.net/shuishoujushi/article/details/10827471">http://blog.csdn.net/shuishoujushi/article/details/10827471</a>

<a href="https://dev.mysql.com/doc/refman/5.6/en/mysqld-safe.html">https://dev.mysql.com/doc/refman/5.6/en/mysqld-safe.html</a>