Home>Article>Database> What is the command to stop mysql service in linux?

What is the command to stop mysql service in linux?

藏色散人
藏色散人 Original
2023-02-13 09:39:04 6804browse

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, with the syntax such as "/bin/systemctl stop mysqld. service"; 3. Use the command mysqladmin to stop, with the syntax such as "mysqladmin -uroot".

What is the command to stop mysql service in linux?

## The operating environment of this tutorial: linux5.9.8 system, mysql5 .5 version, Dell G3 computer.

What is the command to stop the mysql service in Linux?

Linux commands to start/restart/stop the MySQL database

1. Commands to start the MySQL database

(1) Use the command service to start
[root@htlwk0001host /]# service mysqld startRedirecting to /bin/systemctl start mysqld.service

Instructions: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 directly enter the complete path of the command in the command terminal to execute:

[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) The MySQL database service can be stopped successfully, but it is actually redirected 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 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. Execute the command option shutdown after successful login. The command statement is as follows:

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

3. Command to restart the MySQL database

(1) Use the command Service restart
[root@htlwk0001host ~]# service mysqld restartRedirecting to /bin/systemctl restart mysqld.service

Note:also redirects to the command systemctl to restart the service.

(2) Use the systemctl command to restart
[root@htlwk0001host home]# /bin/systemctl restart mysqld.service

4. How to check whether MySQL is started

(1) View through the command pgrep
[root@htlwk0001host ~]# pgrep -l mysqld1060114 mysqld

Instructions:The command grep is to find the process currently running on the system. As shown above, the result can be found according to the process name, indicating the process mysqld Running.

(2) Use the ps command 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

Instructions: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 # 进程被动切换的次数

Instructions:

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

  • Tgid是指线程组 Id,Tgid=Pid。一个进程就是一个线程组,每个进程的所有线程都有着相同的 Tgid,当程序开始运行时,只有一个主线程,这个主线程的 Tgid 就等于 Pid。而当其他线程被创建的时候,就继承了主线程的 Tgid。

  • CoreDumping当程序在运行的过程中出现异常终止或崩溃,系统会将程序当时的状态记录下来,保存在一个 Core 文件中,这种机制称为 Core Dump,又称“核心转储”。Core Dump 机制记录了程序异常时的内存数据、寄存器状态以及运行堆栈等信息,开发人员可以使用调试工具分析 Core 文件来快速定位程序异常原因。

  • Ruid实际用户 Id,指的是进程执行者是谁。

  • Euid有效用户 Id,指进程执行时对文件的访问权限。

  • Suid保存设置用户 Id,作为 Effective User Id 的副本。

  • Fsuid目前进程的文件系统的用户识别码,一般情况下,文件系统的用户识别码(fsuid)与有效的用户识别码(euid)是相同的。

  • 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相同,就不展示了。

推荐学习:《MySQL视频教程

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn