MySQL
在類Unix
系統上安裝時包含一個mysql.server
啓動腳本,它通過mysqld_safe
命令來啓動MySQL
服務,但我們通常把這個啓動腳本重命名爲mysqld
或者mysql
。
這個啓動腳本在有些系統上安裝時被默認註冊,很方便使用,但在其他系統上因爲沒有必要就不是默認註冊,需要我們手動註冊服務。
mysql.server
啓動腳本
我們可以很方便的調用這個啓動腳本:
shell> mysql.server start | stop
mysql.server
啓動腳本先進入MySQL
的註冊目錄,然後調用mysqld_safe
命令,調用時會默認使用/etc/my.cnf;~/my.cnf
兩個配置文件,所以如果你想要更精確的控制啓動,可以修改相關的配置文件。
我們可以查看啓動腳本的內容:
shell> vim /home/work/mysql/support-files/mysql.server
# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
# [mysqld]
# basedir=<path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
# and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
# below.
#
basedir=/home/work/mysql/
datadir=/home/work/mysql/data/
可以看到默認的安裝位置是/usr/local/mysql
,而我們很多人的安裝路徑可能跟這個不一致,如果你需要通過該啓動腳本啓動,需要在這個啓動文件中修改basedir/datadir
的位置,默認爲空。
啓動選項
在使用時,啓動腳本會從配置文件中讀取[mysql.server]
和[mysqld]
兩處配置塊的啓動選項,因此我們一般設置爲:
[mysqld]
datadir=/home/work/mysql/data/
socket=/home/work/mysql/tmp/mysql.sock
port=3306
user=mysql
pid-file=/home/work/mysql/tmp/mysqld.pid
[mysql.server]
basedir=/home/work/mysql
mysql.server
啓動腳本在命令行只支持start|stop
兩個參數,更多的參數通過配置文件來指定:
#只支持這4種參數
[mysql.server]
basedir=MySQL安裝目錄
datadir=MySQL數據目錄
pid-file=存儲MySQL服務進程ID的文件
service-startup-timeout=等待啓動成功的超時時間,默認爲900s,缺省時無限等待,超時時報錯退出
如果pid-file
不指定,默認在data
目錄下創建${host_name}.pid
文件,在指定時[mysqld_safe]
配置塊中的優先級最高,但啓動腳本讀取的是[mysqld]
配置塊,因此如果你使用啓動腳本,可以在兩個配置塊裏配置相同的內容。
使用mysqladmin
關閉服務
除了啓動腳本進行啓動和關閉之外,我們還可以這麼關閉服務:
shell> ~/mysql/bin/mysqladmin shutdown -p
Enter password: (這裏輸入root密碼)
註冊服務
簡單來說就是將啓動腳本放在系統級service
服務下,並重命名爲mysqld
:
shell> ln -S ~/mysql/support-files/mysql.server /etc/init.d/mysqld
shell> service mysqld start|stop|status
具體註冊詳情參考我的另一篇文章:用service命令管理mysql啓停。
開機自啓動
我們也可以設置相關的開機自啓動:
shell> chkconfig --add mysqld
shell> chkconfig --list
參考資料
- 4.3.3 mysql.server — MySQL Server Startup Script: https://dev.mysql.com/doc/ref...
- 用service命令管理mysql啓停: https://segmentfault.com/a/11...