linux系统shell脚本一键安装MYSQL

本文使用配置源安装MYSQL,配置源地址http://repo.mysql.com/ ,大家可自行去下载自己需要的版本,本人安装环境:debian10

一、配置源安装MYSQL流程

这部分为手动安装方式,如需要shell脚本自动安装,可略过这部分,直接看第二部分

1、下载配置源文件(.deb文件),鄙人下载的版本是mysql-apt-config_0.8.23-1_all.deb

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

2、使用root权限安装发行包

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

3、选择要安装的mysql版本,可直接选择默认的8.0,也可选5.7版本

选择需要的版本后,选 ok 开始安装配置源,其原理是在 /etc/apt/sources.list.d/ 目录下生成一个源配置文件mysql.list,稍后的安装将会从该配置文件读取源信息

4、更新源

apt-get update

5、安装mysql-server

该过程会自动安装mysql-server、mysql-client、mysq-common

apt-get install mysql-server

安装过程中会提示输入两次密码(第二次为确认密码)

选择是否使用加强密码

 MYSQL安装完成,后续根据需求自己添加用户设置密码等。

二、 shell脚本自动安装

上面是手动安装模式,有运维需求的同学可参考下面的代码,编写shell脚本实现MYSQL一键部署

我的安装文件目录如下:

setmysql.sh:安装MYSQL的shell脚本

my.cnf:mysql配置文件,用于配置mysql的默认端口、数据存储位置等

mysql.list:mysql配置源文件,安装mysql的时候,会读取该配置文件

各文件内容如下:

setmysql.sh

注意该脚本用到了debconf工具,所以请提前安装:apt-get install debconf -y

#!/bin/bash

# 安装MYSQL数据库

# 脚本路径,包含文件setmysql.sh、mysql.list、my.cnf
TESTPATH="/home/test"

# mysql配置源路径
SOURCELISTDIR="/etc/apt/sources.list.d/"

# mysql配置源文件
MYSQLLISTCONFIG="$TESTPATH/config/mysql.list"

# mysql默认数据存储位置
MYSQLDATAPATH="/var/lib/mysql"

# 我们自己设置的mysql数据存储位置
MYSQLDATATAR="/home/MYSQLDATA"

if [ ! -f "$SOURCELISTDIR/mysql.list" ];then
	# 将mysql配置源文件拷贝到mysql配置源路径路径下(稍后安装mysql时,会自动从该配置文件读取源信息)
	cp $MYSQLLISTCONFIG $SOURCELISTDIR

	# 更新源
	apt-get update

	# 创建预制文件
	# 1、因为安装mysql-server过程中,需要交互,如填写密码等,需要预制文件做自动交互
	# 2、须提前安装debconf: apt-get install debconf -y

	# 输入root密码(这里我设置的密码是111111)
	echo 'mysql-server mysql-community-server/root-pass password 111111' | debconf-set-selections

	# 确认root密码
	echo "mysql-server mysql-community-server/re-root-pass password 111111" | debconf-set-selections

	# 选择强密码选项(如选择另外一个选项,将select 1 改为 select 2即可)
	echo "mysql-server mysql-server/default-auth-override select 1" | debconf-set-selections

	# 开始安装mysql-server、mysql-common、mysql-clint
	apt-get install mysql-server -y

	# 修改mysql数据存储位置(这部分大家也可以不改,因为有时候默认安装路径磁盘容量比较小,那么就要自己指定位置了)
	# 先停止mysql运行
	service mysql stop

	# 创建mysql数据存储新路径(默认安装的话,路径是在/var/lib/mysql下)
	mkdir $MYSQLDATATAR

	# 拷贝默认数据存储路径下的文件到我们指定的目录下(这部分文件包含了数据库数据、数据库启动文件等)
	mv $MYSQLDATAPATH $MYSQLDATATAR

	# 创建软连接,将原来的启动文件路径指向我们新创建的路径下
	ln -s $MYSQLDATATAR/mysql/mysql.sock /var/run/mysqld/mysqld.sock

	# 更改新存储路径的属主信息
	chown -R mysql:mysql $MYSQLDATATAR

	# 更改新存储路径下/mysql目录的权限(这里是因为权限不够的话,非root用户无法访问上面步骤中的mysqld.sock文件,导致登录数据库失败)
	chmod 751 $MYSQLDATATAR/mysql

	# 修改mysql配置信息(默认端口、数据存储路径等)
	cat $TESTPATH/config/my.cnf > /etc/mysql/my.cnf

	# 启动mysql
	service mysql start

	# 添加账户test并赋予权限(这里的用户名、用户密码、host等自己根据需要修改)
	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"
fi

 my.cnf

数据库默认端口是3306,小伙伴们可修改为其他端口

# 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

这里我的源配置是mysql8.0版本,要安装其他版本的同学,需要修改该文件,不知道怎么改的同学,看第一部分手动安装的流程,可选择自己需要的.deb文件,安装后查看/etc/apt/sources.list.d/mysq.list文件,里面就是你想要的内容

### 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

最后,使用root权限执行setmysql.sh,即可完成MYSQl的安装

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值