GVKun编程网logo

centos7.x设置mysql自启动

17

对于centos7.x设置mysql自启动感兴趣的读者,本文将会是一篇不错的选择,并为您提供关于Centos7.x配置PostgreSQL自启动、CentOS7安装Mysql并设置开机自启动的方法、c

对于centos7.x设置mysql自启动感兴趣的读者,本文将会是一篇不错的选择,并为您提供关于Centos 7.x 配置PostgreSQL自启动、CentOS 7安装Mysql并设置开机自启动的方法、centos set mysql 自启动、centos6 添加 mysql 服务自启动的有用信息。

本文目录一览:

centos7.x设置mysql自启动

centos7.x设置mysql自启动

编译安装MysqL并成功初始化后

whereis MysqL
cd /usr/local/MysqL
cp support-files/my-default.cnf /etc/my.cnf

vi /etc/systemd/system/MysqLd.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
ExecStart=/usr/local/MysqL/bin/MysqLd --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#添加到开机启动
systemctl enable MysqLd.service
systemctl start MysqLd
systemctl stop MysqLd
systemctl status MysqLd

Centos 7.x 配置PostgreSQL自启动

Centos 7.x 配置PostgreSQL自启动

Postgresql starts with the system

The document describe how to configure automatic start Postgresql when the system starts.

The ways suitable forsystemdon CentOS 7.0 or latest release.


1.Create and configure postgresql.service

# vim /usr/lib/systemd/system/postgresql.service

[Unit]

Description=Postgresql database server

After=remote-fs.target nss-lookup.target

After=network.target sshd.service

After=proc-fs-nfsd.mount

After=network.target local-fs.target

After=nfs-config.service

After=nfs-mountd.service

After=nfs-blkmap.service

After=nfs-client.target

After=nfs-config.service

After=nfs-idmapd.service

After=nfs-idmap.service

After=nfs-lock.service

After=nfslock.service

After=nfs-mountd.service

After=nfs-secure-server.service

After=nfs-secure.service

After=nfs.service

After=nfs-utils.service

[Service]

Type=forking

User=postgres

Group=appuser

Environment=PGPORT=5432

Environment=PGDATA=/data/01/local/pgsql/data

ExecStart=/data/01/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300

ExecStop=/data/01/local/pqsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast

ExecReload=/data/01/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s

TimeoutSec=300

[Install]

WantedBy=remote-fs.target

#chmod 644 /usr/lib/systemd/system/postgresql.service


2.Reload systemdand enable postgresql.service

#systemctl daemon-reload


#systemctl enable postgresql.service

Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql.service to /usr/lib/systemd/system/postgresql.service.


#systemctl is-enabled postgresql.service

Enabled


#systemctl start postgresql.service


#systemctl status postgresql.service

● postgresql.service - Postgresql database server

Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)

Active: active (running) since Fri 2018-02-23 22:48:49 KST; 28min ago

Main PID: 2506 (postgres)

CGroup: /system.slice/postgresql.service

├─2506 /data/01/local/pgsql/bin/postgres -D /data/01/local/pgsql/data -p 5432

├─2508 postgres: logger process

├─2510 postgres: checkpointer process

├─2511 postgres: writer process

├─2512 postgres: wal writer process

├─2513 postgres: autovacuum launcher process

└─2514 postgres: stats collector process

Feb 23 22:48:47 ec5d-pbfcompilation-02 systemd[1]: Starting Postgresql database server...

Feb 23 22:48:48 ec5d-pbfcompilation-02 pg_ctl[1414]: < 2018-02-23 22:48:48.671 KST >LOG:redirecting log output to logging collector process

Feb 23 22:48:48 ec5d-pbfcompilation-02 pg_ctl[1414]: < 2018-02-23 22:48:48.671 KST >HINT:Future log output will appear in directory "pg_log".

Feb 23 22:48:49 ec5d-pbfcompilation-02 systemd[1]: Started Postgresql database server.


Note: CentOS7.x not support /etc/rc.local script file,Detailed description as following:

#cat /etc/rc.local

#!/bin/bash

# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES

#

# It is highly advisable to create own systemd services or udev rules

# to run scripts during boot instead of using this file.

#这是明智的在系统重启的时候,去创建一个systemd 服务或者udev规则去运行脚本,而不是用/etc/rc.local

#

# In contrast to prevIoUs versions due to parallel execution during boot

# this script will NOT be run after all other services.

#与先前的系统版本相比较,在系统启动的时候,由于服务是并行运行的,所以在其他服务启动之后,/etc/rc.local不会运行

#

# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure

# that this script will be executed during boot.

虽然这里说可以使用chmod +x /etc/rc.local 去执行这个脚本,但是笔者在这个文件中添加一行:su - postgres -c ‘pg_ctl start -D /usr/local/pgsql/data’

然后reboot测试了多次,postgres最后都没有随系统启动成功,所以不再推荐(官方也不推荐了)使用/etc/rc.local文件的方式启动postgres,启动其他服务也一样,最好配置一个systemd服务。

CentOS 7安装Mysql并设置开机自启动的方法

CentOS 7安装Mysql并设置开机自启动的方法

CentOS 7不带MysqL数据库了,默认的数据库是MariaDB(MysqL的一个分支)。

可以按照以下步骤手动安装MysqL数据库。

1. 下载rpm安装文件

wget http://repo.MysqL.com/MysqL-community-release-el7.rpm

2. 执行rpm安装

rpm -ivh MysqL-community-release-el7.rpm

依赖解析完成后,出现下列选项:

Dependencies Resolved

==============================================================================================================
 Package             Arch     Version     Repository       Size
==============================================================================================================
Installing:
 MysqL-community-libs         x86_64    5.6.32-2.el7    MysqL56-community     2.0 M
 replacing mariadb-libs.x86_64 1:5.5.47-1.el7_2
 MysqL-community-server         x86_64    5.6.32-2.el7    MysqL56-community      59 M
 Installing for dependencies:
 MysqL-community-client         x86_64    5.6.32-2.el7    MysqL56-community      19 M
 MysqL-community-common         x86_64    5.6.32-2.el7    MysqL56-community     256 k
 perl-Compress-Raw-Bzip2         x86_64    2.061-3.el7    base         32 k
 perl-Compress-Raw-Zlib         x86_64    1:2.061-4.el7   base         57 k
 perl-DBI            x86_64    1.627-4.el7    base         802 k
 perl-IO-Compress          noarch    2.061-2.el7    base         260 k
 perl-Net-Daemon           noarch    0.48-5.el7    base         51 k
 perl-PlRPC            noarch    0.2020-14.el7   base         36 k

Transaction Summary
=============================================================================================================
Install 2 Packages (+8 Dependent packages)

Total download size: 82 M
Is this ok [y/d/N]:

3. 可以看出,server和client都被选择安装。选择y,自动下载安装。

4. 安装完成后,启动MysqL。

systemctl start MysqLd.service

5. 设置root密码。

update user set password=password("123456") where user='root';

6. 开机自启动。

vim /etc/rc.local
添加service MysqLd start

7.重要更新:

新的rpm安装文件没有自动yum安装的脚本了,需要手动执行yum安装。

即步骤2之后执行yum install MysqL-server即可。

8.关于自启动

步骤6只适用于MysqLd没有自启动的条件下。

如果默认MysqL是自启动的,可能和rc.local中的自启动出现乱序之类的问题。

更稳妥的一个解决办法见:

CentOS 7 程序自启动的问题

一、问题现象:

系统重启后,发现MysqLd服务启动正常,但是依赖MysqL数据库的应用程序A启动失败。

查看日志显示,程序A启动的时候链接数据库失败。

二、原因分析:

MysqLd服务是正常启动的。

此时手动重启程序A,A也正常运行。

结论:说明程序A启动的时候,MysqLd可能没有启动。

三、启动顺序

首先想到的就是调整MysqLd服务的启动顺序,让它高过我的程序A。

然而发现/etc/init.d路径下没有关于MysqLd的启动脚本。

四、解决思路

按照链接指示安装的MysqLd默认是自动启动的。

可以取消MysqLd的自启动,然后写一个启动脚本确保它启动在程序A启动之前。

五、解决办法

1. 查看系统当前默认启动项目的方法,不再是setup之类的了。

systemctl list-unit-files

 执行此命令能查看当前系统的服务启动和服务状态。

 结果显示如下:

 ...
 microcode.service       enabled 
 MysqL.service        enabled 
 MysqLd.service        enabled 
 NetworkManager-dispatcher.service   enabled 
 ...

2. 取消MysqLd的自启动

systemctl disable MysqLd

执行该命令后再查看当前系统的服务状态:

 ...
 microcode.service       enabled 
 MysqLd.service        disabled
 NetworkManager-dispatcher.service   enabled 
 ...

3. 自定义/etc/rc.local

先执行systemctl start MysqLd

再执行startA

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

您可能感兴趣的文章:

  • centos 7安装mysql5.5和安装 mariadb使用的命令
  • centos 7安装mysql5.5的方法
  • 64位CentOs7源码安装mysql-5.6.35过程分享
  • CentOS 7中源码安装MySQL 5.7.6+详细教程
  • 在CentOS 7下使用yum配置MySQL源并安装MySQL
  • centos7.3 安装mysql5.7.18的详细教程
  • CentOS7使用rpm包安装mysql 5.7.18
  • CentOS7.2虚拟机上安装MySQL 5.6.32的教程
  • CentOS7安装mysql5.7解压缩版简明教程

centos set mysql 自启动

centos set mysql 自启动

CentOS Mysql 经过长时间的发展,这里我发表一下个人理解,下面就这就来讲术 CentOS Mysql。在 CentOS 下用 yum 安装 CentOS Mysql 时遇到的问题在一个刚刚安装好的 Centos 上安装 mysql,使用 yum 方式,因为其简单。

命令行如下:yum -y install mysql-server 装完后,出现了莫名其妙的问题,输入 “mysql”, 并不能启动 Mysql,而是报 ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock'' (2)

Google 一下后得知是 没启动 CentOS Mysql 的守护进程于是输入  service mysqld star 但又报:CentOS Mysqld: unrecognized service 最后弄明白了,yum 命令并没有没有把 CentOS Mysql 装入 service

所以需要执行一条命令 /etc/rc.d/init.d/mysqld start 同时又查到将 CentOS Mysql 放入到启动服务列表中的方法


  
  
  1. [root@sample ~]# chkconfig mysqld on  ← 设置MySQL服务随系统启动自启动  
  2. [root@sample ~]# chkconfig --list mysqld  ← 确认MySQL自启动  
  3. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off  ← 如果2--5on的状态就OK 

到此问题解决。

【编辑推荐】

centos6 添加 mysql 服务自启动

centos6 添加 mysql 服务自启动

环境:

os: centos 6

db:mysql 5.6.40

 

1. 拷贝一份 mysql 安装目录下的 mysql.server 到 init.d 目录,并命名为 mysqld

cd /home/mysql5640/support-files
cp mysql.server /etc/init.d/mysqld

 

 

2. 修改 /etc/init.d/mysqld 文件,默认文件以及存在,该文件内容如下:

#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind

# MySQL daemon start/stop script.

# Usually this is put in /etc/init.d (at least on machines SYSV R4 based
# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
# When this is done the mysql server will be started when the machine is
# started and shut down when the systems goes down.

# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine.

# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: mysql
# Required-Start: $local_fs $network $remote_fs
# Should-Start: ypbind nscd ldap ntpd xntpd
# Required-Stop: $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop MySQL
# Description: MySQL is a very fast and reliable SQL database engine.
### END INIT INFO
 
# If you install MySQL on some other places than /usr/local/mysql, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
#   [mysqld]
#   basedir=<path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
#   and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
#   below.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/opt/mysql-5.6.40
datadir=/opt/mysql-5.6.40/data

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start. 
# Value here is overriden by value in my.cnf. 
# 0 means don''t wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900

# Lock directory for RedHat / SuSE.
lockdir=''/var/lock/subsys''
lock_file_path="$lockdir/mysql"

# The following variables are only set for letting mysql.server find things.

# Set some defaults
mysqld_pid_file_path=
if test -z "$basedir"
then
  basedir=/usr/local/mysql
  bindir=/usr/local/mysql/bin
  if test -z "$datadir"
  then
    datadir=/usr/local/mysql/data
  fi
  sbindir=/usr/local/mysql/bin
  libexecdir=/usr/local/mysql/bin
else
  bindir="$basedir/bin"
  if test -z "$datadir"
  then
    datadir="$basedir/data"
  fi
  sbindir="$basedir/sbin"
  libexecdir="$basedir/libexec"
fi

# datadir_set is used to determine if datadir was set (and so should be
# *not* set inside of the --basedir= handler.)
datadir_set=

#
# Use LSB init script functions for printing messages, if possible
#
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
  . $lsb_functions
else
  log_success_msg()
  {
    echo " SUCCESS! $@"
  }
  log_failure_msg()
  {
    echo " ERROR! $@"
  }
fi

PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
export PATH

mode=$1    # start or stop

[ $# -ge 1 ] && shift


other_args="$*"   # uncommon, but needed when called from an RPM upgrade action
           # Expected: "--skip-networking --skip-grant-tables"
           # They are not checked here, intentionally, as it is the resposibility
           # of the "spec" file author to give correct arguments only.

case `echo "testing\c"`,`echo -n testing` in
    *c*,-n*) echo_n=   echo_c=     ;;
    *c*,*)   echo_n=-n echo_c=     ;;
    *)       echo_n=   echo_c=''\c'' ;;
esac

parse_server_arguments() {
  for arg do
    case "$arg" in
      --basedir=*)  basedir=`echo "$arg" | sed -e ''s/^[^=]*=//''`
                    bindir="$basedir/bin"
                    if test -z "$datadir_set"; then
                      datadir="$basedir/data"
                    fi
                    sbindir="$basedir/sbin"
                    libexecdir="$basedir/libexec"
        ;;
      --datadir=*)  datadir=`echo "$arg" | sed -e ''s/^[^=]*=//''`
                    datadir_set=1
        ;;
      --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e ''s/^[^=]*=//''` ;;
      --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e ''s/^[^=]*=//''` ;;
    esac
  done
}

wait_for_pid () {
  verb="$1"           # created | removed
  pid="$2"            # process ID of the program operating on the pid-file
  pid_file_path="$3" # path to the PID file.

  i=0
  avoid_race_condition="by checking again"

  while test $i -ne $service_startup_timeout ; do

    case "$verb" in
      ''created'')
        # wait for a PID-file to pop into existence.
        test -s "$pid_file_path" && i='''' && break
        ;;
      ''removed'')
        # wait for this PID-file to disappear
        test ! -s "$pid_file_path" && i='''' && break
        ;;
      *)
        echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
        exit 1
        ;;
    esac

    # if server isn''t running, then pid-file will never be updated
    if test -n "$pid"; then
      if kill -0 "$pid" 2>/dev/null; then
        :  # the server still runs
      else
        # The server may have exited between the last pid-file check and now.  
        if test -n "$avoid_race_condition"; then
          avoid_race_condition=""
          continue  # Check again.
        fi

        # there''s nothing that will affect the file.
        log_failure_msg "The server quit without updating PID file ($pid_file_path)."
        return 1  # not waiting any more.
      fi
    fi

    echo $echo_n ".$echo_c"
    i=`expr $i + 1`
    sleep 1

  done

  if test -z "$i" ; then
    log_success_msg
    return 0
  else
    log_failure_msg
    return 1
  fi
}

# Get arguments from the my.cnf file,
# the only group, which is read from now on is [mysqld]
if test -x "$bindir/my_print_defaults";  then
  print_defaults="$bindir/my_print_defaults"
else
  # Try to find basedir in /etc/my.cnf
  conf=/etc/my.cnf
  print_defaults=
  if test -r $conf
  then
    subpat=''^[^=]*basedir[^=]*=\(.*\)$''
    dirs=`sed -e "/$subpat/!d" -e ''s//\1/'' $conf`
    for d in $dirs
    do
      d=`echo $d | sed -e ''s/[  ]//g''`
      if test -x "$d/bin/my_print_defaults"
      then
        print_defaults="$d/bin/my_print_defaults"
        break
      fi
    done
  fi

  # Hope it''s in the PATH ... but I doubt it
  test -z "$print_defaults" && print_defaults="my_print_defaults"
fi

#
# Read defaults file from ''basedir''.   If there is no defaults file there
# check if it''s in the old (depricated) place (datadir) and read it from there
#

extra_args=""
if test -r "$basedir/my.cnf"
then
  extra_args="-e $basedir/my.cnf"
else
  if test -r "$datadir/my.cnf"
  then
    extra_args="-e $datadir/my.cnf"
  fi
fi

parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`

#
# Set pid file if not given
#
if test -z "$mysqld_pid_file_path"
then
  mysqld_pid_file_path=$datadir/`hostname`.pid
else
  case "$mysqld_pid_file_path" in
    /* ) ;;
    * )  mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
  esac
fi

case "$mode" in
  ''start'')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    if test -x $bindir/mysqld_safe
    then
      # Give extra arguments to mysqld with the my.cnf file. This script
      # may be overwritten at next upgrade.
      $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
      wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?

      # Make lock for RedHat / SuSE
      if test -w "$lockdir"
      then
        touch "$lock_file_path"
      fi

      exit $return_value
    else
      log_failure_msg "Couldn''t find MySQL server ($bindir/mysqld_safe)"
    fi
    ;;

  ''stop'')
    # Stop daemon. We use a signal here to avoid having to know the
    # root password.

    if test -s "$mysqld_pid_file_path"
    then
      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
    ;;

  ''restart'')
    # Stop the service and regardless of whether it was
    # running or not, start it again.
    if $0 stop  $other_args; then
      $0 start $other_args
    else
      log_failure_msg "Failed to stop running server, so refusing to try to start."
      exit 1
    fi
    ;;

  ''reload''|''force-reload'')
    if test -s "$mysqld_pid_file_path" ; then
      read mysqld_pid <  "$mysqld_pid_file_path"
      kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
      touch "$mysqld_pid_file_path"
    else
      log_failure_msg "MySQL PID file could not be found!"
      exit 1
    fi
    ;;
  ''status'')
    # First, check to see if pid file exists
    if test -s "$mysqld_pid_file_path" ; then 
      read mysqld_pid < "$mysqld_pid_file_path"
      if kill -0 $mysqld_pid 2>/dev/null ; then 
        log_success_msg "MySQL running ($mysqld_pid)"
        exit 0
      else
        log_failure_msg "MySQL is not running, but PID file exists"
        exit 1
      fi
    else
      # Try to find appropriate mysqld process
      mysqld_pid=`pidof $libexecdir/mysqld`

      # test if multiple pids exist
      pid_count=`echo $mysqld_pid | wc -w`
      if test $pid_count -gt 1 ; then
        log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
        exit 5
      elif test -z $mysqld_pid ; then 
        if test -f "$lock_file_path" ; then 
          log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists"
          exit 2
        fi 
        log_failure_msg "MySQL is not running"
        exit 3
      else
        log_failure_msg "MySQL is running but PID file could not be found"
        exit 4
      fi
    fi
    ;;
    *)
      # usage
      basename=`basename "$0"`
      echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"
      exit 1
    ;;
esac

exit 0

 

整个文件只要修改红色部分 (根据自己的实际情况修改)

basedir=/opt/mysql-5.6.40
datadir=/opt/mysql-5.6.40/data

 

 

3. 检查是否有执行权限,没有的话需要加上可执行权限
chmod +x /etc/init.d/mysqld

4. 添加服务
chkconfig --add mysqld

5. 设置开启启动
chkconfig mysqld on

 

遇到的问题:

1.The server quit without updating PID file (/var/lib/mysql/h [失败]).

原因是 /etc/ 下面有一个 my.cnf 配置文件,但是我这里是自定义配置文件的,文件为:/home/mysql5640/conf/my.cnf

将 etc 目录下的 my.cnf 删除即可

 

今天关于centos7.x设置mysql自启动的分享就到这里,希望大家有所收获,若想了解更多关于Centos 7.x 配置PostgreSQL自启动、CentOS 7安装Mysql并设置开机自启动的方法、centos set mysql 自启动、centos6 添加 mysql 服务自启动等相关知识,可以在本站进行查询。

本文标签: