Shell one-click deployment of mysql

0. Download the installation package (5.7.32)

链接: https://pan.baidu.com/s/17MEMnWlxt0OiwCkPNTWrJw  密码: ldge

1. Create a directory dedicated to the execution of the installation script, and create the directory required for the compressed file in this directory

mkdir -p /opt/shell/files
cd /opt/shell
vi mysql_install.sh

2. Edit shell script

#!/bin/bash
#离线安装mysql
#我们需要安装mysql的目录
ipath="/usr/local"
#获取当前脚本目录的路径 $0-shell本身的文件名
installpath=$(cd `dirname $0`; pwd)
echo ${
    
    installpath}
#提前在脚本目录创建files文件夹 上传安装包到该目录
filepath=${
    
    installpath}/files
j=`ps -el | grep mysqld`
#查看是否已经安装mysql
mysql=$(echo ${
    
    j} | grep "mysqld")
echo ${
    
    mysql}
if [[ "$mysql" != "" ]]
then
	echo "mysql was installed!"
else
	echo "mysql not installed!"
	echo "开始解压mysql安装包"
	tar -xzvf ${
    
    filepath}/mysql-*.tar.gz > /dev/null 2>&1
	echo "进入mysql安装目录 获取mysql版本"
	cd mysql* && mysqlname=`pwd | awk -F '/' '{print $NF}'`
	echo "mysql版本:${mysqlname}"
	echo "移动解压后的文件夹到指定安装目录${ipath}"
	mv ${
    
    mysqlname} ${
    
    ipath}/mysql
	echo "mysql安装目录${ipath}/mysql"
	echo "添加用户组"
	groupadd mysql
	useradd -r -g mysql mysql
	echo ;
	echo "创建数据文件夹"
	mkdir -p ${
    
    ipath}/mysql/data
	echo "授权"
	chown -R mysql:mysql ${
    
    ipath}/mysql
	echo "初始化数据库"
	${
    
    ipath}/mysql/bin/mysqld --initialize-insecure --user=mysql  --basedir=${
    
    ipath}/mysql --datadir=${
    
    ipath}/mysql/data
    echo "将mysql加入到服务"
	cp ${
    
    ipath}/mysql/support-files/mysql.server /etc/init.d/mysql
	echo "设置开机启动"
	chkconfig mysql on
	echo "创建自己的默认的my.cnf"
	rm -rf /etc/my.cnf
	touch /etc/my.cnf
	echo "[client]" >> /etc/my.cnf
	echo "user=root" >> /etc/my.cnf
	echo "password=" >> /etc/my.cnf
	echo "port = 3306" >> /etc/my.cnf
	echo "socket = /tmp/mysql.sock" >> /etc/my.cnf
	echo "[mysqld]" >> /etc/my.cnf
	echo "init-connect='SET NAMES utf8'" >> /etc/my.cnf
	echo "basedir=/usr/local/mysql" >> /etc/my.cnf
	echo "datadir=/usr/local/mysql/data" >> /etc/my.cnf
	echo "socket=/tmp/mysql.sock" >> /etc/my.cnf
	echo "max_connections=50" >> /etc/my.cnf
	echo "character-set-server=utf8" >> /etc/my.cnf
	echo "default-storage-engine=INNODB" >> /etc/my.cnf
	echo "启动mysql"
	service mysql start
	echo "设置mysql命令到全局 软连接"
	ln -s  ${
    
    ipath}/mysql/bin/mysql  /usr/bin
	mysql -uroot mysql -e "update user set authentication_string=password('123456') where user='root'"
	mysql -uroot mysql -e "update user set host='%' where user = 'root'"
	mysql -uroot mysql -e "flush privileges"
fi

Guess you like

Origin blog.csdn.net/q18729096963/article/details/114402532