How to use Linux service command? You will understand after reading it

table of Contents

Preface

We can often see that in some examples, people use service [service name] start to start the service, or use service to stop the service, and so on.

But sometimes, we will report an error when using the service command. For example, I installed mysql in cenos7 and wanted to start Mysql with service mysqld start, but the following error was reported

Failed to stop mysqld.service: Unit mysqld.service not loaded.

Let's take a look at how the service command runs


service command

The service command is used to manage the services of the system. The service command itself is also a script. It will look for the specified service script in the /ect/init.d/ directory, and then execute the corresponding script to complete the command.

The /etc/init.d directory stores a series of system service management (start and stop) scripts. Use the service command to execute the scripts of the corresponding services in the init.d directory.

After reading the above explanation, everyone should guess why I can't use the service command to start MySQL.

The reason is that there is no mysql management script in the /etc/init.d directory

We want to use the service command to manage MySQL, so we only need to put the mysql.server script in the /etc/init.d directory.

What is mysql.server?

mysql.server是官方针对Unix和类Unix系统二进制版本安装包当中包含的脚本,
它是一个SHELL脚本,被用来启动、查看和停止mysqld进程服务。
mysql.server其实上调用的是mysqld_safe命令。

Note: The mysql.server script is generally placed in the support-files folder under our mysql installation directory

Example:

#移动mysql.server脚本到/etc/init.d
cp /root/data/mysql-5.7.24/support-files/mysql.server  /etc/init.d/mysql
#使用service命令启动mysql
service mysql start

After reading how to use the service command to manage mysql, our other services can also be managed in a similar way.

Guess you like

Origin blog.csdn.net/qq_36551991/article/details/111185267