Linux 7下MySQL自启动配置(glibc)

使用glibc编译后的mysql二进制安装方法被广泛使用,因为它和Windows下的zip方式一下,简单几个步骤,配置一下环境即可。而在Linux 7版本中,MySQL的自启动,不再建议将启动脚本存放到/etc/init.d目录中,因此,我们需要手动配置一下基于systemd方式的自启动文件。下文供大家参考。

一、当前环境

# more /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

安装位置(glibc解压)
# cd /usr/local/mysql/
# ls
bin COPYING data docs include lib man my.cnf README share support-files

二、配置mysql systemd服务

Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。
Systemd的功能是用于集中管理和配置类UNIX系统。
在Linux 7版本中,依旧兼容将启动脚本放到/etc/init.d,但不建议这么做。

由于系统存在基于yum方式安装的mysql,因此可以直接复制重命名后修改mysqld.service
# cp /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/mysqld_glibc.service
# vim /usr/lib/systemd/system/mysqld_glibc.service

修改后的对比
# diff /usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/mysqld_glibc.service
35c35
< PIDFile=/var/run/mysqld/mysqld.pid
---
> PIDFile=/var/run/mysqld/mysqld_glibc.pid
47c47
< ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS
---
> ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld_glibc.pid $MYSQLD_OPTS

my.cnf的配置
# more /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
pid-file=/var/run/mysqld/mysqld_glibc.pid

启动mysqld_glibc服务
# systemctl start mysqld_glibc.service 
# ps -ef|grep mysql
mysql 7590 1 23 11:12 ? 00:00:00 /usr/local/mysql/bin/mysqld \
  --daemonize --pid-file=/var/run/mysqld/mysqld_glibc.pid

开启自启动
# systemctl enable mysqld_glibc.service

# mysql -uroot -p -S /tmp/mysql.sock
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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.

mysql>

三、完整的mysqld.service

[root@centos7 ~]# grep -Ev "^$|^[#;]" /usr/lib/systemd/system/mysqld_glibc.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/var/run/mysqld/mysqld_glibc.pid
TimeoutSec=0
PermissionsStartOnly=true
ExecStartPre=/usr/bin/mysqld_pre_systemd
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld_glibc.pid $MYSQLD_OPTS
EnvironmentFile=-/etc/sysconfig/mysql
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
上一篇:U盘安装linux开机无法启动解决方法


下一篇:云原生安全-更安全的密文管理 Vault on ACK