linux--把MySQL加入开机自启

准备: 1.先将服务文件拷贝到init.d下,并重命名为mysqld

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

2.赋予可执行权限

chmod +x /etc/init.d/mysqld

一、用/etc/rc.local方式

1.把“/etc/init.d/mysqld start”放入“/etc/rc.local”文件中

[root@Study ~]# echo "/etc/init.d/mysqld start" >> /etc/rc.local
rc.local本质上是一个shell脚本文件,可以把启动时需要执行的命令写在里面,启动时将按顺序执行。这里放入自启项末端。

<< 插曲: 当把命令放入过后,发现Centos重启后MySQL并没有自启。 2.检查下/etc/rc.local是否有权限,发现是有的:

[root@Study ~]# ll /etc/rc.local 
lrwxrwxrwx. 1 root root 13 6月  29 17:53 /etc/rc.local -> rc.d/rc.local

但是发现了这只是一个软连接,真实的文件是/etc/rc.d/rc.local(P.S.:这个文件默认的权限就是777) 3. 接着我们查看一下真实的启动文件/etc/rc.d/rc.local:

[root@Study ~]# ll /etc/rc.d/rc.local 
-rw-r--r--. 1 root root 473 7月   2 00:11 /etc/rc.d/rc.local

发现这个文件没有执行权限,那我们为其添加执行权限:

[root@Study ~]# chmod  +x  /etc/rc.d/rc.local

再次重启Centos后MySQL就可以自启了。 <<

二、使用chkconfig方式

1.添加服务

[root@Study ~]# chkconfig --add mysqld

2.查看下是否在显示服务列表

[root@Study ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 systemctl list-unit-files。
      查看在具体 target 启用的服务请执行
      systemctl list-dependencies [target]。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
chkconfig执行顺序是看运行级别
如果看到mysql的服务,并且3,4,5都是开的话则成功;如果是关,则键入 chkconfig --level 345 mysqld on

扩展:

3.如何关闭MySQL自启服务

[root@Study ~]# chkconfig --level 345 mysqld off

4.删除MySQL的自动启动选项的设置

[root@Study ~]# chkconfig --del mysqld
本文出现任何错误,请留言私信批评指正。
经验分享 程序员 微信小程序 职场和发展