Home  >  Article  >  Operation and Maintenance  >  What are the commands to stop the mysql service in Linux?

What are the commands to stop the mysql service in Linux?

王林
王林forward
2023-05-12 18:40:134965browse

The Linux stop mysql service commands are: 1. Use the service command to stop, with the syntax such as "service mysqld stopRedirecting to /bin/systemctl"; 2. Use the systemctl command to stop, the syntax is such as "/ bin/systemctl stop mysqld.service"; 3. Use the command mysqladmin to stop, with the syntax such as "mysqladmin -uroot".

Linux commands to start/restart/stop the MySQL database

1. Commands to start the MySQL database

(1) Use the command service Start

[root@htlwk0001host /]# service mysqld startRedirecting to /bin/systemctl start mysqld.service

Note:
The MySQL database service can be started successfully, but it is actually redirected to the command systemctl to start the service.

(2) Use the command systemctl to start

[root@htlwk0001host ~]# systemctl start mysqld.service

If the relevant environment variables are not set, you can enter the directory where the command systemctl is located and then execute the above command statement, or enter it directly in the command terminal Execute the complete path of the command:

[root@htlwk0001host ~]# /bin/systemctl start mysqld.service

If you don’t know the directory where the command is located, you can use the command which to query.

2. Command to stop the MySQL database

(1) Use the command service to stop

[root@htlwk0001host /]# service mysqld stopRedirecting to /bin/systemctl 
stop mysqld.service

Instructions:
(1) It can be stopped successfully MySQL database service, but it actually redirects to the command systemctl to stop the service.
(2) If it is an ubuntu system, the command to stop the database is: service mysql stop

Where is the command service?

[root@htlwk0001host ~]# which service
/usr/sbin/service

Note: Only the user root has permission to use the commands in the directory /sbin.

Where is the command systemctl?

[root@htlwk0001host ~]# which systemctl
/usr/bin/systemctl

(2) Use the command systemctl to stop

[root@htlwk0001host ~]# /bin/systemctl stop mysqld.service

If the environment variable is configured, you do not need to bring the complete path, just enter the command directly:

[root@htlwk0001host ~]# systemctl stop mysqld.service

(3) Use the command mysqladmin to stop

Use this command to stop the database service. In fact, you need to log in to the database, so you need to enter the user name and password to log in to the database. After successful login, execute the command option shutdown. The command statement is as follows :

[root@htlwk0001host ~]# mysqladmin -uroot -p shutdown

3. Command to restart MySQL database

(1) Use the command service to restart

[root@htlwk0001host ~]# service mysqld restartRedirecting to /bin/systemctl restart mysqld.service

Instructions:
is also redirected to Command systemctl to restart the service.

(2) Use the command systemctl to restart

[root@htlwk0001host home]# /bin/systemctl restart mysqld.service

4. How to check whether MySQL is started

(1) Use the command pgrep to check

[root@htlwk0001host ~]# pgrep -l mysqld1060114 mysqld

Note:
The command grep is to find the processes currently running on the system. As shown above, the results can be found based on the process name, indicating that the process mysqld is running.

(2) Use the command ps to view

[root@htlwk0001host ~]# ps aux | grep mysqlmysql    1060114  1.5  4.9 1604076 182836 ?      Sl   08:33   0:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root     1060165  0.0  0.0  12320   972 pts/0    S+   08:33   0:00 grep --color=auto mysql

Description:
As shown above, the process mysqld is a background process (that is, daemonize), The process status is sl, which means that the process is a child process and is in a dormant state. If MySQL has not been started, this process cannot be found. If it is found, it means that the database has been started.

Explanation of search results:
The process belongs to user mysql, the process ID is 1060114, the CPU occupied time percentage is 1.5%, the memory occupied percentage is 4.9%, and the virtual memory size that can be used is 1604076 kilobytes, the logistics memory occupied by the process is 182836 kilobytes, the process startup time is 8:33 (see the current system time for year, month, day), and the actual CPU usage time is 0 (it should not be updated!)

(3) Use the cat command to view

[root@htlwk0001host ~]# cat /proc/1060114/statusName:	mysqld                			# 进程名称Umask:	0026							# 用户权限屏蔽位,实际权限=预设权限-Umask						State:	S (sleeping)					# 进程状态,可中断的睡眠状态Tgid:	1060114							# 线程组Id,等于Pid								Ngid:	0
Pid:	1060114							# 进程IdPPid:	1								# 父进程Id,启动PidTracerPid:	0							# 跟踪进程的PidUid:	27	27	27	27					# Uid/Euid/Suid/Fsuid 拥有者Id/有效用户Id/Suid(Set User ID)/Fsuid(file system user ID)Gid:	27	27	27	27					# Gid/Egid/Sgid/Fsgid 对照Uid来理解FDSize:	128								# 文件描述符的最大个数Groups:	27 								# 启动该进程的用户所属的组的IdNStgid:	1060114
NSpid:	1060114
NSpgid:	1060113
NSsid:	1060113
VmPeak:	 1604620 kB                     # 表示进程所占用最大虚拟内存大小,即进程所使用的虚拟内存的峰值VmSize:	 1604620 kB						# 表示进程当前使用的虚拟内存大小VmLck:	       0 kB						# 表示被锁定的内存大小,即已经锁住的物理内存的大小(锁住的物理内存不能交换到硬盘)VmPin:	       0 kB
VmHWM:	  183668 kB						# 表示进程所占用物理内存的峰值VmRSS:	  183632 kB						# 表示进程当前占用物理内存的大小(与procrank中的RSS)RssAnon:	  166396 kB
RssFile:	   17236 kB
RssShmem:	       0 kB
VmData:	  549388 kB						# 表示进程占用的数据段的大小VmStk:	     132 kB						# 表示进程堆栈段的大小,即进程占用的栈的大小VmExe:	   25480 kB						# 表示进程代码的大小,进程占用的代码段大小(不包括库)VmLib:	    7596 kB						# 表示进程所使用共享库的大小,进程所加载的动态库所占用的内存大小(可能与其它进程共享)VmPTE:	     696 kB						# 表示进程页表项的大小,进程占用的页表大小(交换表项数量)VmSwap:	       0 kB						# 进程所使用的交换区的大小HugetlbPages:	       0 kB				# Huge Translation Lookaside Buffer Pages 巨大的快速查找缓冲页表CoreDumping:	0						# 核心转储数据大小Threads:	48							# 共享使用该信号描述符的任务的个数SigQ:	0/14419							# 待处理信号的个数/目前最大可以处理的信号的个数SigPnd:	0000000000000000				# 屏蔽位,存储了该线程的待处理信号ShdPnd:	0000000000000000				# 屏蔽位,存储了该线程组的待处理信号SigBlk:	0000000000084007				# 存放被阻塞的信号SigIgn:	0000000000003000				# 存放被忽略的信号SigCgt:	00000001800006e8				# 存放被俘获到的信号CapInh:	0000000000000000				# 能被当前进程执行的程序的继承的能力CapPrm:	0000000000000000				# 进程能够使用的能力,可以包含CapEff中没有的能力,这些能力是被进程自己临时放弃的CapEff:	0000000000000000				# 是CapPrm的一个子集,进程放弃没有必要的能力有利于提高安全性CapBnd:	0000003fffffffff				# 是系统的边界能力,我们无法改变它CapAmb:	0000000000000000
NoNewPrivs:	0							# 没有新权限Seccomp:	0							# seccomp机制用于限制应用程序可以使用的系统调用,增加系统的安全性。Speculation_Store_Bypass:	vulnerable
Cpus_allowed:	3						# 可以执行该进程的CPU掩码集Cpus_allowed_list:	0-1					# 该进程可以使用CPU的列表,这里是0-1Mems_allowed:							# 更改进程执行时占用的内存有关Mems_allowed_list:	0					# 进程只是使用了结点0的内存资源voluntary_ctxt_switches:	94			# 表示进程主动切换的次数nonvoluntary_ctxt_switches:	194			# 进程被动切换的次数

Description:

  • /proc/ is a The virtual file system is a mapping of the system memory. The data in this directory are stored in the memory and do not occupy any hard disk space. Therefore, if you can find the process information through this directory, it is enough to indicate that the process has been started. Otherwise, the relevant information cannot be found in the memory.

  • Tgid refers to the thread group Id, Tgid=Pid. A process is a thread group, and all threads of each process have the same Tgid. When the program starts running, there is only one main thread, and the Tgid of this main thread is equal to Pid. When other threads are created, they inherit the Tgid of the main thread.

  • CoreDumping When the program terminates abnormally or crashes during running, the system will record the current status of the program and save it in a Core file. This mechanism is called Core Dump, also known as "core dump". The Core Dump mechanism records information such as memory data, register status, and running stack when a program is abnormal. Developers can use debugging tools to analyze Core files to quickly locate the cause of program exceptions.

  • Ruid The actual user ID refers to who is the executor of the process.

  • Euid Effective user ID, refers to the access permission to the file when the process is executed.

  • #Suid Save the set user Id as a copy of the Effective User Id.

  • Fsuid The user identification code of the file system of the current process. Generally, the user identification code (fsuid) of the file system is the same as the effective user identification code (euid). )Are the same.

  • Cpus_allowed 进程可以使用 CPU 的亲和性掩码,因为我们指定为两块 CPU,所以这里就是 3,如果该进程指定为4个 CPU (如果有话),这里就是 F(1111)。

(四)通过命令 lsof 查看

  1. 查看 mysql 进程打开的文件列表:

[root@htlwk0001host ~]# lsof -c mysqlCOMMAND     PID  USER   FD   TYPE             DEVICE  SIZE/OFF      NODE NAME
mysqld  1064381 mysql  cwd    DIR              253,1      4096    636744 /var/lib/mysql
mysqld  1064381 mysql  rtd    DIR              253,1       244       128 /
mysqld  1064381 mysql  txt    REG              253,1 251816000  51228705 /usr/sbin/mysqld
mysqld  1064381 mysql  mem    REG              253,1    553480  50342901 /usr/lib64/libpcre2-8.so.0.7.1

如上所示,如果数据库已经启动了,则可以查询到一堆被打开的文件,否则查询不到任何文件。

  1. 查看数据库默认端口 3306 的使用情况:

[root@htlwk0001host ~]# lsof -i:3306COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
java    1050299 root   72u  IPv6 19482407      0t0  TCP htlwk0001host:37380->47.114.59.224:mysql (CLOSE_WAIT)java    1050299 root   78u  IPv6 19482908      0t0  TCP htlwk0001host:37470->47.114.59.224:mysql (CLOSE_WAIT)

从上面的查询结果可以看出,进程 java 已经关闭和 MySQL 服务器的连接,数据库所在的主机地址是:47.114.59.224,端口名称:mysql,端口状态:CLOSE_WAIT。

数据库服务器开启后,查看端口 3306 的使用情况,如下:

[root@htlwk0001host ~]# lsof -i:3306COMMAND     PID  USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
java    1050398  root   72u  IPv6 19485111      0t0  TCP htlwk0001host:37978->47.114.59.224:mysql (ESTABLISHED)java    1050398  root   78u  IPv6 19482585      0t0  TCP htlwk0001host:37480->47.114.59.224:mysql (CLOSE_WAIT)java    1050398  root   80u  IPv6 19485118      0t0  TCP htlwk0001host:37980->47.114.59.224:mysql (ESTABLISHED)java    1050398  root   81u  IPv6 19485120      0t0  TCP htlwk0001host:37982->47.114.59.224:mysql (ESTABLISHED)java    1050398  root   82u  IPv6 19484462      0t0  TCP htlwk0001host:37984->47.114.59.224:mysql (ESTABLISHED)java    1050398  root   83u  IPv6 19484463      0t0  TCP htlwk0001host:37986->47.114.59.224:mysql (ESTABLISHED)java    1050398  root   84u  IPv6 19484464      0t0  TCP htlwk0001host:37988->47.114.59.224:mysql (ESTABLISHED)java    1050398  root   85u  IPv6 19484465      0t0  TCP htlwk0001host:37990->47.114.59.224:mysql (ESTABLISHED)java    1050398  root   86u  IPv6 19484466      0t0  TCP htlwk0001host:37992->47.114.59.224:mysql (ESTABLISHED)java    1050398  root   87u  IPv6 19484467      0t0  TCP htlwk0001host:37994->47.114.59.224:mysql (ESTABLISHED)mysqld  1068848 mysql    4u  IPv6 19485112      0t0  TCP htlwk0001host:mysql->47.114.59.224:37978 (ESTABLISHED)mysqld  1068848 mysql   26u  IPv6 19485094      0t0  TCP *:mysql (LISTEN)mysqld  1068848 mysql   49u  IPv6 19485119      0t0  TCP htlwk0001host:mysql->47.114.59.224:37980 (ESTABLISHED)mysqld  1068848 mysql   50u  IPv6 19485121      0t0  TCP htlwk0001host:mysql->47.114.59.224:37982 (ESTABLISHED)mysqld  1068848 mysql   51u  IPv6 19485122      0t0  TCP htlwk0001host:mysql->47.114.59.224:37984 (ESTABLISHED)mysqld  1068848 mysql   52u  IPv6 19485123      0t0  TCP htlwk0001host:mysql->47.114.59.224:37986 (ESTABLISHED)mysqld  1068848 mysql   53u  IPv6 19485124      0t0  TCP htlwk0001host:mysql->47.114.59.224:37988 (ESTABLISHED)

(五)使用命令 netstat 查看

数据库启动后,使用命令 netstat 查看数据库服务端默认端口 3306 的使用情况:

[root@htlwk0001host ~]# netstat -an | grep ':3306'tcp6       0      0 :::3306                 :::*                    LISTEN     
tcp6       0      0 172.19.116.91:3306      47.114.59.224:37978     ESTABLISHED
tcp6       0      0 172.19.116.91:37982     47.114.59.224:3306      ESTABLISHED
tcp6       0      0 172.19.116.91:3306      47.114.59.224:37990     ESTABLISHED
tcp6       0      0 172.19.116.91:37988     47.114.59.224:3306      ESTABLISHED
tcp6       0      0 172.19.116.91:3306      47.114.59.224:37992     ESTABLISHED

从上面的查询结果看,我们看这条:tcp6 0 0 172.19.116.91:37982 47.114.59.224:3306 ESTABLISHED ,可以得知数据库服务端与客户端保持连接状态。

数据库关闭后,我们再使用命令 netstat 查看端口 3306 的使用情况:

[root@htlwk0001host ~]# netstat -an | grep ':3306'tcp6       0      0 172.19.116.91:3306      47.114.59.224:37978     FIN_WAIT2  
tcp6       1      0 172.19.116.91:37982     47.114.59.224:3306      CLOSE_WAIT 
tcp6       0      0 172.19.116.91:3306      47.114.59.224:37990     FIN_WAIT2  
tcp6       1      0 172.19.116.91:37988     47.114.59.224:3306      CLOSE_WAIT 
tcp6       0      0 172.19.116.91:3306      47.114.59.224:37992     FIN_WAIT2  
tcp6       0      0 172.19.116.91:3306      47.114.59.224:38006     FIN_WAIT2  
tcp6       0      0 172.19.116.91:3306      47.114.59.224:38004     FIN_WAIT2  
tcp6       1      0 172.19.116.91:38008     47.114.59.224:3306      CLOSE_WAIT 
tcp6       0      0 172.19.116.91:3306      47.114.59.224:37994     FIN_WAIT2  
tcp6       0      0 172.19.116.91:3306      47.114.59.224:38010     FIN_WAIT2  
tcp6       1      0 172.19.116.91:37984     47.114.59.224:3306      CLOSE_WAIT

(六)使用命令 service 查看数据库状态

[root@htlwk0001host ~]# service mysqld statusRedirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-04-23 16:11:01 CST; 8s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1069004 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 1068982 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 1069006 (mysqld)
    Tasks: 46 (limit: 23070)
   Memory: 214.2M
   CGroup: /system.slice/mysqld.service
           └─1069006 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

4月 23 16:10:59 htlwk0001host systemd[1]: Starting MySQL Server...
4月 23 16:11:01 htlwk0001host systemd[1]: Started MySQL Server.

从上述的查询结果可以看到: Active: active (running) since Fri 2021-04-23 16:11:01 CST; 8s agoactive(running),说明数据库处于运行中的状态。

(七)使用命令 systemctl 查看数据库状态

[root@htlwk0001host ~]# /bin/systemctl status mysqld.service

命令 service 其实就是调命令 systemctl 查询的,所以命令 systemctl 的查询结果和命令 service 相同,就不展示了。

The above is the detailed content of What are the commands to stop the mysql service in Linux?. For more information, please follow other related articles on the PHP Chinese website!

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