MySQL
在類Unix
系統上安裝時包含一個mysql.server
啓動腳本,它經過mysqld_safe
命令來啓動MySQL
服務,但咱們一般把這個啓動腳本重命名爲mysqld
或者mysql
。html
這個啓動腳本在有些系統上安裝時被默認註冊,很方便使用,但在其餘系統上由於沒有必要就不是默認註冊,須要咱們手動註冊服務。mysql
mysql.server
啓動腳本咱們能夠很方便的調用這個啓動腳本:sql
shell> mysql.server start | stop
mysql.server
啓動腳本先進入MySQL
的註冊目錄,而後調用mysqld_safe
命令,調用時會默認使用/etc/my.cnf;~/my.cnf
兩個配置文件,因此若是你想要更精確的控制啓動,能夠修改相關的配置文件。shell
咱們能夠查看啓動腳本的內容:vim
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
的位置,默認爲空。segmentfault
在使用時,啓動腳本會從配置文件中讀取[mysql.server]
和[mysqld]
兩處配置塊的啓動選項,所以咱們通常設置爲:socket
[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
兩個參數,更多的參數經過配置文件來指定:this
#只支持這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
:code
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