在Linux平臺下啟動和關閉MySQL服務

hipilee發表於2017-12-15

首先需要檢視下載MySQL服務的狀態

[root@localhost bin]# netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2482/dnsmasq
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1071/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1084/cupsd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2289/master
tcp6       0      0 :::3306                 :::*                    LISTEN      52044/mysqld

命令列方式啟動和關閉

現在MySQL服務處於啟動中,執行下列命令進行關閉操作。

[root@localhost bin]# ./mysqladmin -uroot -p shutdown
Enter password:
171215 09:11:29 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
[1]+  完成                  ./mysqld_safe
[root@localhost bin]# netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2482/dnsmasq
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1071/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1084/cupsd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2289/master
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::22                   :::*                    LISTEN      1071/sshd
tcp6       0      0 ::1:631                 :::*                    LISTEN      1084/cupsd
tcp6       0      0 ::1:25                  :::*                    LISTEN      2289/master
udp        0      0 192.168.122.1:53        0.0.0.0:*                           2482/dnsmasq
udp        0      0 0.0.0.0:67              0.0.0.0:*                           2482/dnsmasq
udp        0      0 127.0.0.1:323           0.0.0.0:*                           708/chronyd
udp        0      0 0.0.0.0:37653           0.0.0.0:*                           705/avahi-daemon: r
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           705/avahi-daemon: r
udp6       0      0 ::1:323                 :::*                                708/chronyd
raw6       0      0 :::58                   :::*                    7           800/NetworkManager

可以發現MySQL服務已經被關閉。然後執行下列命令來啟動服務。

[root@localhost bin]# ./mysqld_safe &
[1] 51836
[root@localhost bin]# 171215 09:19:05 mysqld_safe Logging to '/var/log/mysqld.log'.
171215 09:19:05 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

[root@localhost bin]# clear
[root@localhost bin]# netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2482/dnsmasq
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1071/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1084/cupsd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2289/master
tcp6       0      0 :::3306                 :::*                    LISTEN      52044/mysqld
[root@localhost bin]# service mysql start
Redirecting to /bin/systemctl start  mysql.service
[root@localhost bin]# netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2482/dnsmasq
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1071/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1084/cupsd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2289/master
tcp6       0      0 :::3306                 :::*                    LISTEN      52044/mysqld

最後再執行命令將MySQL服務關閉,我使用中發現一個問題,就是服務方式開啟貌似必須用服務方式關閉。


服務方式啟動和關閉

通過服務先開啟MySQL服務

[root@localhost bin]# service mysql start
Redirecting to /bin/systemctl start  mysql.service
[root@localhost bin]# netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2482/dnsmasq
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1071/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1084/cupsd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2289/master
tcp6       0      0 :::3306                 :::*                    LISTEN      56017/mysqld

看見MySQL的服務了,再通過服務的方式來關閉服務。

[root@localhost bin]# service mysql stop
Redirecting to /bin/systemctl stop  mysql.service
[root@localhost bin]# netstat -nlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2482/dnsmasq
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1071/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1084/cupsd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2289/master
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::22                   :::*                    LISTEN      1071/sshd
tcp6       0      0 ::1:631                 :::*                    LISTEN      1084/cupsd
tcp6       0      0 ::1:25                  :::*                    LISTEN      2289/master
udp        0      0 192.168.122.1:53        0.0.0.0:*                           2482/dnsmasq
udp        0      0 0.0.0.0:67              0.0.0.0:*                           2482/dnsmasq
udp        0      0 127.0.0.1:323           0.0.0.0:*                           708/chronyd
udp        0      0 0.0.0.0:37653           0.0.0.0:*                           705/avahi-daemon: r
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           705/avahi-daemon: r
udp6       0      0 ::1:323                 :::*                                708/chronyd
raw6       0      0 :::58                   :::*                    7           800/NetworkManager


命令和服務方式的區別

       啟動一些程式服務的時候,有時候直接去程式的bin目錄下去執行命令,有時候利用service啟動。比如啟動mysql服務時,大部分喜歡執行service mysql start。當然也可以去mysql下執行bin命令帶上幾個引數什麼的。linux可以man一下,看出來就是去/etc/init.d下執行了可執行的shell指令碼。service執行的服務指令碼都是在/etc/init.d目錄下,各個程式下指令碼里執行的命令仍然是在各個bin下。

相關文章