I. Overview
After Linux
configuring the multi-instance in the environment, you MySQL
must 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 chkconfig
no longer works, so we need to configure it manually.
Two, Linux startup knowledge
When configuring MySQL
multi-instance startup, let us first understand Linux
the knowledge points of startup. Linux
The startup sequence under normal circumstances is as follows:
- Load the kernel
- Execute
init
program /etc/rc.d/rc.sysinit
Thereinit
first script executed/etc/rc.d/rc $RUNLEVEL
$RUNLEVEL is the default operating mode,linux
there are 7 operating modes in total./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)/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.04
not in use under the initd
management system, is used instead systemd
. However, the systemd
changes are relatively large and difficult to use. Use the systemd
settings to start up, in order /etc/rc.local
to set the start-up program in the same as before .
1. Implementation principle
systemd
By 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.service
files after the system is installed , that is, the files we need to configure. /lib/system/system
The 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
then under the /etc/systemd/system
directory to be viewed .
shell > cd /etc/systemd/system
shell > ll
The output is as follows:
we see that /etc/systemd/system
there are no rc-local.service
files 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.local
file
In /etc/rc.local
the file exit 0
add 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:
- If
/etc/
there is no directoryrc.local
file can create your own. And grant executable permissions- Since the
/etc/rc.local
file is older than/etc/profile
the executable file, the/etc/profile
configuration file in the environment variable is not yet functional, but all need to re-configurationMySQL
environment variable.
4. Restart the system
Execute the reboot
command 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:
Description:
Such as
MySQL
not 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 CentOS7
an environment configuration is relatively simple. Only need to /etc/rc.d/rc.local
add the file command on the need to start, and has given /etc/rc.d/rc.local
the file executable permissions given to specific actions are as follows:
1. View the file
/etc/rc.d/rc.local
The 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.local
order 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 reboot
command 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:
Description:
As
MySQL
there is no normal startup, you cansystemctl status rc-local.service
view the boot log, find the error log, you can make the appropriate changes.
Five, summary
In this part describes the Linux
system startup process, Ubuntu
and centOS
ways 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
, nginx
and so on.