mysql 几种启动和关闭mysql方法介绍

https://dev.mysql.com/doc/refman/5.7/en/programs-server.html

mysql几种启动方法

方法1、mysqld

mysqld指定读取my.cnf配置文件启动,加一个 & 实现后台启动。终端不会被阻塞

复制代码
./mysqld --defaults-file=/etc/my.cnf &

方法2、mysqld_safe

复制代码
  ./mysqld_safe --defaults-file=/etc/my.cnf  & 

方法3、mysql.server

将support-files/mysql.server 脚本做成一个linux服务。使用服务的方式启动

复制代码
cp -v /usr/local/mysql/support-files/mysql.server /etc/init.d/ 
chkconfig --add mysql.server 
service mysql.server {start|stop|restart|reload|force-reload|status} 

方法4、./etc/init.d/mysqld start

这种方式其实和support-files一样。不过更清晰点,我比较喜欢用这个。

方法5、mysqld_multi

mysqld_multi 主要用来多实例启动的。见 https://www.jianshu.com/p/88adb36bbca9

mysqld和mysqld_safe启动有什么区别?

1、使用file命令分别查看mysqld和mysqld_safe两个执行文件。可见

mysqld是一个64位的 dynamically linked ;

mysqld_safe 是一个shell script;shell脚本。可以直接编辑的。

复制代码
[root@localhost bin]# file mysqld
mysqld: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, BuildID[sha1]=e1d1f6fd34afae9ac33181c12d605976c81cc6e1, not stripped
[root@localhost bin]# file mysqld_safe 
mysqld_safe: POSIX shell script, ASCII text executable

2、使用ps-ef查看下二者区别

mysqld

复制代码
[root@localhost bin]# ps -ef |grep mysql
mysql      9294   8364 12 11:12 pts/2    00:00:00 ./mysqld --defaults-file=/etc/my.cnf

mysqld_safe

复制代码
[root@localhost ~]# ps -ef |grep mysql
root       9399   8364  0 11:13 pts/2    00:00:00 /bin/sh ./mysqld_safe --defaults-file=/etc/my.cnf
mysql      9555   9399  2 11:13 pts/2    00:00:00 /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64 --datadir=/mdata/mysql57 --plugin-dir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/lib/plugin --user=mysql --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock3

mysqld_safe命令启动的方式要比mysqld启动多了一个mysqld_safe的进程;mysqld_safe 其实就是mysqld服务的一个守护进程;它的作用是当mysql宕机后会自动重启mysqld服务。所以平时应该使用mysqld_safe。

如下我手动kill掉9555这个mysql进程,随后再次执行ps -ef |grep mysql 发现又生成了一个pid为9918 的mysql进程。

复制代码
[root@localhost ~]# kill -9 9555
[root@localhost ~]# ps -ef |grep mysql
root       9399   8364  0 11:13 pts/2    00:00:00 /bin/sh ./mysqld_safe --defaults-file=/etc/my.cnf
mysql      9918   9399  8 11:19 pts/2    00:00:00 /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64 --datadir=/mdata/mysql57 --plugin-dir=/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/lib/plugin --user=mysql --log-error=error.log --pid-file=localhost.localdomain.pid --socket=/tmp/mysql.sock3305 --port=3305
root       9950   8476  0 11:19 pts/3    00:00:00 grep --color=auto mysql

mysql几种关闭方法

mysql终端使用,需要密码 方法1、进入mysql终端执行shutdown命令可以关闭mysql服务

复制代码
[root@localhost bin]# mysql -S /tmp/mysql.sock3305 -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.33 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(root@localhost) [(none)]>shutdown;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [(none)]>show variables like '%port%';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock3305' (2)
ERROR: 
Can't connect to the server

(root@not_connected) [(none)]>exit;
Bye
[root@localhost bin]# 

mysql终端使用,需要密码 方法2、在mysql终端关闭mysql的另一种命令:

mysqladmin可以做到在mysql终端无需真正登录进去就可stop mysql server

复制代码
[root@localhost bin]# ./mysqladmin -uroot -p1111aaA_ -S /tmp/mysql.sock3305 shutdown
mysqladmin: [Warning] Using a password on the command line interface can be insecure.

在部署mysql的服务器上使用,不需要密码 方法3、./support-files/mysql.server stop

使用file命令查看下

复制代码
[root@localhost ~]# file /home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/support-files/mysql.server
/home/mysql5.7/mysql-5.7.33-linux-glibc2.12-x86_64/support-files/mysql.server: POSIX shell script, ASCII text executable

mysql.server 是一个 shell script。那么我们查看一下它:

复制代码
....
 'stop')
    # Stop daemon. We use a signal here to avoid having to know the
    # root password.

    if test -s "$mysqld_pid_file_path"
    then
      # signal mysqld_safe that it needs to stop
      touch "$mysqld_pid_file_path.shutdown"

      mysqld_pid=`cat "$mysqld_pid_file_path"`

      if (kill -0 $mysqld_pid 2>/dev/null)
      then
        echo $echo_n "Shutting down MySQL"
        kill $mysqld_pid
        # mysqld should remove the pid file when it exits, so wait for it.
        wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
      else
        log_failure_msg "MySQL server process #$mysqld_pid is not running!"
        rm "$mysqld_pid_file_path"
      fi

      # Delete lock for RedHat / SuSE
      if test -f "$lock_file_path"
      then
        rm -f "$lock_file_path"
      fi
      exit $return_value
    else
      log_failure_msg "MySQL server PID file could not be found!"
    fi
    ;;
...

可见,mysql.server stop使用kill -0 pid 来实现关闭的。

利用了linux中的一个信号机制。kill -9 表示强制关闭。若使用的是kill -0 表示发生一个信号给进程,若应用程序中有负责接收处理信号的话,就会走正常关闭的代码流程。故linux的kill -0 命令在mysql中是一个正常的安全关闭。可以做到不需要密码也可以把mysql关闭。
请使用kill -0 pid 来关闭mysql而不是kill -9 pid
最后编辑于:2024-10-27 16:14:32
© 著作权归作者所有,转载或内容合作请联系作者

喜欢的朋友记得点赞、收藏、关注哦!!!

相关推荐
麦麦麦当劳大王3 小时前
【油猴脚本】Boss直聘 岗位职责复制
开发语言·前端·javascript
starzy19903 小时前
深入理解Scala Map:从实现原理到工程实践
开发语言·后端·scala
只睡四小时4 小时前
Three.js Sub-tick:漏判 60.7%→0
开发语言·javascript·性能优化·ecmascript·游戏开发·three.js·碰撞检测
码农小河664 小时前
基于SSM+JSP的高校专业信息管理系统【附源码+数据库】
java·开发语言·数据库
东鹏特饮6 小时前
系统运维(传统/通用运维)面试题
开发语言·php
其实防守也摸鱼6 小时前
个人学习 Pwn 思路:从基础到漏洞利用的进阶路径
大数据·前端·人工智能·chrome·算法·安全·网络安全
风若飞6 小时前
Spring Ai 入门遇到问题1
java·后端·spring
今天要早睡_7 小时前
C++01_入门基础复习
开发语言·c++
Nuanyt7 小时前
LeetCode Hot 100 链表专题(五)· 2 题(Java)
java·算法·leetcode·链表