Configure MySQL to start automatically at boot

Personal blog address

I. Overview

After Linuxconfiguring the multi-instance in the environment, you MySQLmust manually start it every time you start it, and it is now configured to start up automatically. In the case of multiple instances, the command chkconfigno longer works, so we need to configure it manually.

Two, Linux startup knowledge

When configuring MySQLmulti-instance startup, let us first understand Linuxthe knowledge points of startup. LinuxThe startup sequence under normal circumstances is as follows:

  1. Load the kernel
  2. Execute initprogram
  3. /etc/rc.d/rc.sysinitThere initfirst script executed
  4. /etc/rc.d/rc $RUNLEVEL$RUNLEVEL is the default operating mode, linuxthere are 7 operating modes in total.
  5. /etc/rc.d/rc.local After the corresponding level of service is started, execute the file (in fact, you can also write the commands that need to be executed into the file)
  6. /sbin/mingetty Waiting for user to log in

There are several methods here, we choose the fifth one, which is to add the execution command to /etc/rc.d/rc.local.

Three, Ubuntu 18.04 environment

In ubuntu18.04not in use under the initdmanagement system, is used instead systemd. However, the systemdchanges are relatively large and difficult to use. Use the systemdsettings to start up, in order /etc/rc.localto set the start-up program in the same as before .

1. Implementation principle

systemdBy default /etc/systemd/system, the configuration file in the directory will be read , and the files in this directory are actually the files under the link /lib/systemed/system. Generally /lib/systemed/system, there will be rc-local.servicefiles after the system is installed , that is, the files we need to configure. /lib/system/systemThe files in the directory are as follows:

shell > cd /lib/systemd/system
shell > ll

The output result is as follows, find the file marked in the red box and rc-local.service
Insert picture description here
then under the /etc/systemd/systemdirectory to be viewed .

shell > cd /etc/systemd/system
shell > ll

The output is as follows:
Insert picture description here
we see that /etc/systemd/systemthere are no rc-local.servicefiles in the directory at this time . Then create a link and configuration file.

2. Create a link

  • Execute the following command to create a link

    shell > ln -fs /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
    
  • View file content

    shell > cd /etc/systemd/system/ 
    shell > cat rc-local.service
    

    The output is as follows:

    #  This file is part of systemd.
    #
    #  systemd is free software; you can redistribute it and/or modify it
    #  under the terms of the GNU Lesser General Public License as published by
    #  the Free Software Foundation; either version 2.1 of the License, or
    #  (at your option) any later version.
    
    # This unit gets pulled automatically into multi-user.target by
    # systemd-rc-local-generator if /etc/rc.local is executable.
    [Unit] #启动顺序与依赖关系。
    Description=/etc/rc.local Compatibility
    ConditionFileIsExecutable=/etc/rc.local #指定了执行的文件,
    After=network.target # `network.target` 这个target后面进行执行。也就是网络启动完成之后,执行` /etc/rc.local` 文件。
    
    [Service] # 启动行为,如何启动,启动类型。
    Type=forking
    ExecStart=/etc/rc.local start
    TimeoutSec=0
    RemainAfterExit=yes
    
  • Add [Install] block

    Add the following at the end of the above file:

    [Install] #定义如何安装这个配置文件,即怎样做到开机启动。
    WantedBy=multi-user.target
    Alias=rc-local.service
    

3. Modify the /etc/rc.localfile

In /etc/rc.localthe file exit 0add the following before:

# 指定 MySQL 的环境变量
MYSQL_HOME=/usr/local/mysql
export MYSQL_HOME
PATH=$PATH:$MYSQL_HOME/bin
export PATH

# 配置启动项
/usr/local/mysql/bin/mysqld_multi --defaults-file=/usr/local/mysql/etc/my.cnf start &

Description:

  1. If /etc/there is no directory rc.localfile can create your own. And grant executable permissions
  2. Since the /etc/rc.localfile is older than /etc/profilethe executable file, the /etc/profileconfiguration file in the environment variable is not yet functional, but all need to re-configuration MySQLenvironment variable.

4. Restart the system

Execute the rebootcommand to restart the system and check whether the service is started successfully.

Execute the following command to check whether the startup is successful:

shell >  netstat -ln | grep 330

The following result indicates that the startup is successful:
Insert picture description here

Description:

Such as MySQLnot start properly, you can view the log file /var/log/syslog. Find the error log and modify it accordingly.

Fourth, in the CentOS7 environment

In CentOS7an environment configuration is relatively simple. Only need to /etc/rc.d/rc.localadd the file command on the need to start, and has given /etc/rc.d/rc.localthe file executable permissions given to specific actions are as follows:

1. View the file

/etc/rc.d/rc.localThe initial content of the view file is as follows:

shell > cat /etc/rc.d/rc.local

The output is as follows:

#!/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.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

2. Add content

In /etc/rc.d/rc.localorder to perform the end of the file is added, as follows:

# 指定 MySQL 的环境变量
MYSQL_HOME=/usr/local/mysql
export MYSQL_HOME
PATH=$PATH:$MYSQL_HOME/bin
export PATH

/usr/local/mysql/bin/mysqld_multi --defaults-file=/etc/my.cnf start &

3. Grant executable permissions

shell > chmod +x /etc/rc.d/rc.local

4. Restart the system

Execute the rebootcommand to restart the system and check whether the service is started successfully.

Execute the following command to check whether the startup is successful:

shell >  netstat -ln | grep 330

The following result indicates that the startup is successful:
Insert picture description here

Description:

As MySQLthere is no normal startup, you can systemctl status rc-local.serviceview the boot log, find the error log, you can make the appropriate changes.

Five, summary

In this part describes the Linuxsystem startup process, Ubuntuand centOSways to configure the system from the start. Way above any application can be configured to boot from the start, for example, you can configure redis, tomcat, nginxand so on.

Guess you like

Origin blog.csdn.net/small_love/article/details/114539219