手動安裝的mysql是不會自動添加到系統服務中的。如果需要配置開機啓動,最好是將mysql配置成系統服務,也便於管理。
確定mysql運行時的pid文件位置
1、先去mysql安裝目錄啓動mysql
[root@host01 mysql]# cd /usr/local/mysql/support-files/
[root@host01 support-files]# pwd
/usr/local/mysql/support-files
[root@host01 support-files]# ./mysql.server start
Starting MySQL. SUCCESS!
[root@host01 support-files]#
2、查看mysql進程,找到【–pid-file】對應的值,然後可以停掉mysql
[root@host01 support-files]# ps -ef | grep mysql
root 5877 1 0 10:32 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/host01.pid
mysql 6029 5877 1 10:32 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --pid-file=/usr/local/mysql/data/host01.pid --socket=/var/lib/mysql/mysql.sock
root 6059 3035 0 10:33 pts/0 00:00:00 grep --color=auto mysql
[root@host01 support-files]#
[root@host01 support-files]# ./mysql.server stop
Shutting down MySQL.. SUCCESS!
[root@host01 support-files]#
添加mysql服務
1、在/usr/lib/systemd/system目錄下創建【mysql.service】內容如下,注意路徑可能有變化,根據實際情況寫,PIDFile就是上面拿到的路徑文件名
[Unit]
Description=Mysql
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/mysql/data/host01.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
[Install]
WantedBy=multi-user.target
2、重啓systemctl,讓mysql.service生效
[root@host01 support-files]# systemctl daemon-reload
3、測試通過service方式啓動mysql
[root@host01 support-files]# ps -ef |grep mysql
root 6105 3662 0 10:42 pts/2 00:00:00 grep --color=auto mysql
[root@host01 support-files]# systemctl start mysql
[root@host01 support-files]# ps -ef |grep mysql
root 6119 1 1 10:42 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/host01.pid
mysql 6272 6119 10 10:42 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --pid-file=/usr/local/mysql/data/host01.pid --socket=/var/lib/mysql/mysql.sock
root 6302 3662 0 10:42 pts/2 00:00:00 grep --color=auto mysql
[root@host01 support-files]#
4、mysql服務相關命令
將mysql設置爲開機啓動
systemctl enable mysql
將mysql設置爲開機不啓動
systemctl disable mysql
啓動mysql
systemctl start mysql
關閉mysql
systemctl stop mysql
重啓mysql
systemctl restart mysql