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
[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
[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
https://www.jianshu.com/p/a5f24d0f736e
https://dev.mysql.com/doc/refman/5.7/en/programs-server.html