search
HomeDatabaseMysql TutorialIntroduction 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

Mar 22, 2019 am 11:26 AM
linux operation and maintenancemysqlpidservicelog

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  | --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</service></service></service>

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. If there is any infringement, please contact admin@php.cn delete
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor