CentOS下MySQL开机自启动服务

CentOS 6

使用chkconfig 命令设置自启动服务,chkconfig命令主要用来更新、启动、停止、查询系统服务在不同运行等级的状态。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。

语法:

chkconfig [--add][--del][--list] [系统服务]
chkconfig [--level <运行级别>] [系统服务] [on/off/reset]

参数:

   --add 增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。
   --del 删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据。
   --list 显示所有系统服务在不同运行级别的状态信息。如果指定了服务名,那么只显示指定的服务在不同运行级的状态
   --level<等级代号>[on/off/reset]  指定系统服务要在哪一个执行等级中开启或关毕。
      0表示:表示关机
      1表示:单用户模式
      2表示:无网络连接的多用户命令行模式
      3表示:有网络连接的多用户命令行模式
      4表示:不可用
      5表示:带图形界面的多用户模式
      6表示:重新启动 

设置mysql开机自启动:

cd /usr/local/mysql
cp support-files/mysql.server /etc/init.d/mysql.server

chkconfig mysql.server  on              #设定mysqld在运行定级2、3、4、5下为on
chkconfig --level 35 mysql.server  on   #设定mysqld在运行等级3、5为开启状态
chkconfig --list mysql.server           #列出mysqld服务状态
chkconfig --list                        #列出所有的系统服务状态

  

CentOS 7

使用systemctl命令设置自启动服务:

cd /usr/lib/systemd/system
touch mysqld.service 

编辑内容如下

vim mysqld.service 
# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # systemd service file for MySQL forking server # [Unit] Description=MySQL Server Documentation=man:mysqld(8) Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql Type=forking PIDFile=/usr/local/mysql/run/mysqld.pid # Disable service start and stop timeout logic of systemd for mysqld service. TimeoutSec=0 # Execute pre and post scripts as root PermissionsStartOnly=true # Needed to create system tables #ExecStartPre=/usr/bin/mysqld_pre_systemd # Start main service ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysqld.pid $MYSQLD_OPTS # Use this to switch malloc implementation EnvironmentFile=-/etc/sysconfig/mysql # Sets open_files_limit LimitNOFILE = 65535 Restart=on-failure RestartPreventExitStatus=1 PrivateTmp=false

加载

systemctl daemon-reload
systemctl enable mysqld.service
systemctl is-enabled mysqld

启动服务:systemctl start xxx.service
关闭服务:systemctl stop xxx.service
重启服务:systemctl restart xxx.service
显示服务的状态:systemctl status xxx.service
在开机时启用服务:systemctl enable xxx.service
在开机时禁用服务:systemctl disable xxx.service
查看服务是否开机启动:systemctl is-enabled xxx.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed

 

posted @ 2022-03-19 13:38  VicLW  阅读(757)  评论(0编辑  收藏  举报