手动安装的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