Linux system shell script to install MYSQL with one click

This article uses the configuration source to install MYSQL, the configuration source address is http://repo.mysql.com/, you can download the version you need by yourself, my installation environment: debian10

1. Configuration source installation MYSQL process

This part is manual installation. If you need shell script to install automatically, you can skip this part and go directly to the second part.

1. Download the configuration source file (.deb file), the version I downloaded is mysql-apt-config_0.8.23-1_all.deb

wget http://repo.mysql.com/mysql-apt-config_0.8.23-1_all.deb

2. Install the release package with root privileges

sudo apt install ./mysql-apt-config_0.8.23-1_all.deb

3. Select the mysql version to be installed, you can directly choose the default 8.0, or you can choose the 5.7 version

After selecting the required version, select ok to start installing the configuration source. The principle is to generate a source configuration file mysql.list in the /etc/apt/sources.list.d/ directory, and the later installation will read from the configuration file Get source information

4. Update source

apt-get update

5. Install mysql-server

This process will automatically install mysql-server, mysql-client, mysq-common

apt-get install mysql-server

During the installation process, you will be prompted to enter the password twice (the second time is to confirm the password)

Choose whether to use a strong password

The MYSQL installation is complete, and you can add users and set passwords according to your needs.

2. Shell script automatic installation

The above is the manual installation mode. Students who have operation and maintenance needs can refer to the following code to write shell scripts to achieve one-click deployment of MYSQL

My installation file directory is as follows:

setmysql.sh: shell script to install MYSQL

my.cnf: mysql configuration file, used to configure mysql’s default port, data storage location, etc.

mysql.list: mysql configuration source file, when installing mysql, it will read the configuration file

The content of each file is as follows:

setmysql.sh

Note that the script uses the debconf tool, so please install it in advance: apt-get install debconf -y

#!/bin/bash

# Install MYSQL database

# Script path, including files setmysql.sh, mysql.list, my.cnf
TESTPATH="/home/test"

# mysql configuration source path
SOURCELISTDIR="/etc/apt/sources.list.d/"

# mysql configuration source file
MYSQLLISTCONFIG="$TESTPATH/config/mysql.list"

# mysql default data storage location
MYSQLDATAPATH="/var/lib/mysql"

# Our own mysql data storage location
MYSQLDATATAR="/home/MYSQLDATA"

if [ ! -f "$SOURCELISTDIR/mysql.list" ];then
# Copy the mysql configuration source file to the mysql configuration source path (when installing mysql later, the source information will be automatically read from the configuration file)
cp $MYSQLLISTCONFIG $SOURCELISTDIR

# update source
apt-get update

# create prefab file
# 1. Because in the process of installing mysql-server, interaction is required, such as filling in passwords, etc., prefabricated files are required for automatic interaction
# 2. Debconf must be installed in advance: apt-get install debconf -y

# Enter the root password (the password I set here is 111111)
echo 'mysql-server mysql-community-server/root-pass password 111111' | debconf-set-selections

# Confirm root password
echo "mysql-server mysql-community-server/re-root-pass password 111111" | debconf-set-selections

# Choose a strong password option (if you choose another option, just change select 1 to select 2)
echo "mysql-server mysql-server/default-auth-override select 1" | debconf-set-selections

# Start installing mysql-server, mysql-common, mysql-clint
apt-get install mysql-server -y

#Modify the mysql data storage location (you can leave this part alone, because sometimes the disk capacity of the default installation path is relatively small, so you have to specify the location yourself)
# Stop mysql running first
service mysql stop

# Create a new path for mysql data storage (if installed by default, the path is under /var/lib/mysql)
mkdir $MYSQLDATATAR

# Copy the files under the default data storage path to the directory we specified (this part of the file contains database data, database startup files, etc.)
mv $MYSQLDATAPATH $MYSQLDATATAR

# Create a soft connection, point the original startup file path to our newly created path
ln -s $MYSQLDATATAR/mysql/mysql.sock /var/run/mysqld/mysqld.sock

# Change the owner information of the new storage path
chown -R mysql:mysql $MYSQLDATATAR

# Change the permissions of the /mysql directory under the new storage path (this is because non-root users cannot access the mysqld.sock file in the above steps if the permissions are insufficient, resulting in failure to log in to the database)
chmod 751 $MYSQLDATATAR/mysql

# Modify mysql configuration information (default port, data storage path, etc.)
cat $TESTPATH/config/my.cnf > /etc/mysql/my.cnf

# start mysql
service mysql start

# Add the account test and grant permissions (the user name, user password, host, etc. here can be modified as needed)
mysql -uroot -111111 -e "create user 'test'@'host' identified by '111111'"
mysql -uroot -111111 -e "GRANT PROCESS ON *.* TO 'test'@'%';flush privileges;"

echo "install mysql succeed"
else
echo "mysql has been installed already"
the fi

my.cnf

The default port of the database is 3306, and friends can modify it to other ports

# Copyright (c) 2015, 2022, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[client]
port=3306
default-character-set=utf8

[mysqld]
port=3306
character-set-server=utf8
datadir=/home/MYSQLDATA/mysql
socket=/home/MYSQLDATA/mysql/mysql.sock

[mysql]
default-character-set=utf8

mysql.list

My source configuration here is mysql8.0 version. Students who want to install other versions need to modify this file. For students who don’t know how to modify it, see the first part of the manual installation process. You can choose the .deb file you need and check it after installation. /etc/apt/sources.list.d/mysq.list file, which is what you want

### THIS FILE IS AUTOMATICALLY CONFIGURED ###
# You may comment out entries below, but any other modifications may be lost.
# Use command 'dpkg-reconfigure mysql-apt-config' as root for modifications.
deb http://repo.mysql.com/apt/debian/ buster mysql-apt-config
deb http://repo.mysql.com/apt/debian/ buster mysql-8.0
deb http://repo.mysql.com/apt/debian/ buster mysql-tools
#deb http://repo.mysql.com/apt/debian/ buster mysql-tools-preview
deb-src http://repo.mysql.com/apt/debian/ buster mysql-8.0

Finally, execute setmysql.sh with root privileges to complete the installation of MYSQl