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