Home  >  Article  >  Database  >  Introduction to the method of starting and stopping mysql using the service command

Introduction to the method of starting and stopping mysql using the service command

不言
不言forward
2019-03-22 11:26:257417browse

This article brings you an introduction to the method of starting and stopping mysql using the service command. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

After installing mysql, it is very troublesome to start and stop every time. Sometimes I can’t remember to search online and see that everyone uses service to manage services. I tried it and it really works. It is recommended that everyone should use it this way.

Starting and stopping the mysql service

# 启动
/usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/tmp/mysql.sock
# 停止
kill `cat /usr/local/mysql/var/mysqld.pid`

Starting and stopping in this way requires remembering the path of mysql and the storage location of the pid, so it is more troublesome.

service System service management

The service command is used to manage system services, such as starting (start), stopping (stop), restarting (restart), viewing status (status), etc. The service command itself is a shell script that is used to conveniently call the script to complete tasks. It searches for the specified service script in the /etc/init.d/ directory.

Relevant commands include:

chkconfig: used to view and set the running level of the service

ntsysv: used to set the self-starting of the service

service runs the specified service (called the System V initial script), retains only the two environment variables LANG and TERM, and sets the current path to /. If a service script wants to be managed by service, it must at least support the start and stop commands and save the script in the /etc/init.d/ directory.

How to use service

## 命令格式
Usage: service < option > | --status-all | [ service_name [ command | --full-restart ] ]

# 查看指定服务的命令行使用帮助
service <service>
## mysqld 举例
$ service mysqld
Usage: mysqld  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]

# 启动、停止、重启指定服务
service <service> start|stop|restart
## mysqld举例restart,即先执行stop再执行start命令
$ service mysqld restart
Shutting down MySQL..                                      [  OK  ]
Starting MySQL.                                            [  OK  ]

# 显示指定服务的状态
$ service mysqld status
MySQL running (27390)                                      [  OK  ]

# 显示所有服务的状态
service --status-all

# 查看系统服务列表,以及每个服务的运行级别
chkconfig --list

# 设置指定服务开机时是否自动启动
chkconfig <service> on|off

# 以全屏幕文本界面设置服务开机时是否自动启动
# 必须以root启动,空格切换状态,tab切换按钮,上下鼠标移动光标
ntsysv

In addition to using the mysqld example, when we modify the host name, IP address and other information, we must often restart the network to take effect. At this time, we can call:

service network status|restart

Use service to manage mysqld service

We have already understood the basic knowledge, and now we can start the transformation. First, we must find the mysql.server file in your installation directory.

$ locate mysql.server
/usr/local/mysql/support-files/mysql.server

This file is initialized during installation. It is actually a script file that supports inputting different parameters to perform different functions. We can view its start function:

# 根据输入的命令执行不同的脚本,首先判断是否为start
case "$mode" in
  'start')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    # 查看该bin/mysqld_safe命令是否存在,如果不存在,就直接报错:找不到
    if test -x $bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      # 直接执行mysqld_safe命令,并传入参数,然后创建pid文件
      $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
      wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?

      # Make lock for RedHat / SuSE
      if test -w "$lockdir"
      then
        touch "$lock_file_path"
      fi

      exit $return_value
    else
      log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
    fi
    ;;

  'stop')

and then copy it Go to the /tmp/init.d/ directory:

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

At this point, you can directly call the service to start:

service mysqld start

Some problems encountered

Above The theory is very simple, but we still encountered some problems during the startup process. The easiest way to solve the problem is to check the log file on Google. These two methods can locate and solve it very quickly.

Configuration files are ignored

Look directly at the error message at startup:

$ service mysqld start
Warning: World-writable config file '/etc/my.cnf' is ignored
Starting MySQL.Warning: World-writable config file '/etc/my.cnf' is ignored

Look directly at the reason for the error, which indicates that globally writable configuration files are ignored. This error means that the configuration file can be modified by all users, so there is a possibility of malicious tampering, so the configuration of this file will not be introduced and ignored.

The solution is to change the file to be readable and writable by users and user groups, and only readable and non-writable by other users:

chmod 664 /etc/my.cnf

The PID file cannot be created

See the error message directly :

$ service mysqld start
Starting MySQL.The server quit without updating PID file (/usr/var/mysql/var/mysqld.pid).

The reason for the error shows that it unexpectedly exited when starting MySQL because the PID file was not updated.

At this time, some people may not understand it. It doesn’t matter. Let’s look at the error log directly:

2019-03-21 22:29:45 32896 [ERROR] /usr/local/mysql/bin/mysqld: Can't create/write to file '/usr/var/mysql/var/mysqld.pid' (Errcode: 2 - No such file or directory)
2019-03-21 22:29:45 32896 [ERROR] Can't start server: can't create PID file: No such file or directory

This log clearly states that the mysqld.pid file cannot be created, or It is a permission issue, or the path does not exist. Later, I found that the path was written incorrectly, so just change it to the correct path.

Setting of error log path:

$ vim my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/var/mysql/var/mysqld.pid
log-error = /user/local/mysql/log/mysql.err

log command is abandoned

Solved the above problem, we continued to execute, still found the same error message, but the log file was Different:

$ service mysqld start
Starting MySQL.The server quit without updating PID file (/usr/var/mysql/var/mysqld.pid).

# 日志文件
2019-03-21 22:37:33 0 [ERROR] /usr/local/mysql/bin/mysqld: ambiguous option '--log=/usr/local/mysql/log/mysql.log' (log-bin, log_slave_updates)
2019-03-21 22:37:33 0 [ERROR] Aborting

Found the log error message: vague option --log, I don't understand this very well, you can see the reason by searching on Google: the --log command has been abandoned for a long time, now use -- general-log instead. Just modify the my.cnf configuration file:

$ vim my.cnf
[mysqld]
general-log = /user/local/mysql/log/mysql.log

Start successfully

After solving the above problems, it can finally be started successfully. At this time, we can easily start, stop and reload the mysqld service. Now:

$ service mysqld start
Starting MySQL.                                            [  OK  ]

Summary

Service service management tool can help us quickly start and stop applications. In the process of Mysql startup, mastering the viewing of log files and Google search is the biggest help for us.

This article has ended here. For more other exciting content, you can pay attention to the MySQL Tutorial Video column on the PHP Chinese website!

The above is the detailed content of Introduction to the method of starting and stopping mysql using the service command. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete