Centos mysql-5.7.26一键安装脚本

tar包,shell脚本百度云盘链接,提取码:i6a4

https://pan.baidu.com/s/19L5O0bR69HldSxoiDTgzcQ

安装脚本如下

可参考https://blog.csdn.net/rzhidong/article/details/78432119

将安装包、脚本、配置文件放在同一目录

此脚本适用于centos6,centos7防火墙开放端口命令不一致,如需在centos7安装,3306开启命令改成

firewall-cmd --zone=public --add-port=3306/tcp --permanent

Centos mysql-5.7.26一键安装脚本_第1张图片

MySQL安装路径 /usr/local,安装完后的临时密码在passwd.txt

脚本安装完后执行下面,让mysql环境变量生效

source /etc/profile
#!/bin/bash
# write by luozhidong
# date: 20190610
# description:默认路径为/usr/local,若要变更则修改MYSQL_HOME
# mysql包为:mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
MYSQL_HOME='/usr/local'
#my-default.cnf参数检查
checkCNF(){
	BASEDIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep basedir)
	if [[ $(echo $BASEDIR|awk -F '=' '{print $2}')  != $MYSQL_HOME/mysql ]];then
        	sed -i "s|$BASEDIR|basedir = $MYSQL_HOME/mysql|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
	fi
	DATADIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep datadir)
	if [[ $(echo $DATADIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/data ]];then
        	sed -i "s|$DATADIR|datadir = $MYSQL_HOME/mysql/data|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
	fi
	LOGDIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep log)
    if [[ $(echo $LOGDIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/log/mysqld.log ]];then
            sed -i "s|$LOGDIR|log-error = $MYSQL_HOME/mysql/log/mysqld.log|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
    fi
	#检查pid-file = $MYSQL_HOME/mysql/mysqld.pid
	PIDDIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep pid)
    if [[ $(echo $PIDDIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/mysqld.pid ]];then
             sed -i "s|$PIDDIR|pid-file = $MYSQL_HOME/mysql/mysqld.pid|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
    fi
	#检查socket = $MYSQL_HOME/mysql/mysql.sock
	SOCKETLINENUM=$(cat -n $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep socket|wc -l)
	SOCKETLINE=$(cat -n $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep socket|awk '{print $1}')
	for((i=1;i<=${SOCKETLINENUM};i++));
	do
        LINE=$(echo $SOCKETLINE|awk '{print $'$i'}')
        SOCKDIR=$(sed -n  ${LINE}p /usr/local/mysql/support-files/my-default.cnf)
        if [[ $(echo $SOCKDIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/mysql.sock ]];then
             sed -i "s|$SOCKDIR|socket = $MYSQL_HOME/mysql/mysql.sock|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
        fi
	done
}
#检查其他库文件并删除
if [[ -n $(rpm -qa | grep mysql) ]];then
	rpm -qa | grep mysql|awk '{print $0}'|xargs rpm -e --nodeps
fi
#检查mysql组
if [[ -z $(cat /etc/group|grep mysql) ]];then
	groupadd mysql
fi
#检查mysql用户
if [[ -z $(cat /etc/passwd | grep mysql) ]];then
	useradd -r -g mysql mysql
fi
id mysql
CHECK=$(ls $MYSQL_HOME |grep mysql)
if [[ -z $CHECK ]];then
	#解压
	TARPATH=$(find . -name 'mysql*linux*.tar.gz')
	tar  -zvxf $TARPATH -C $MYSQL_HOME
    	mv $MYSQL_HOME/$(ls $MYSQL_HOME |grep mysql) $MYSQL_HOME/mysql
	mkdir -p $MYSQL_HOME/mysql/data $MYSQL_HOME/mysql/log
	touch $MYSQL_HOME/mysql/log/mysqld.log
	touch  $MYSQL_HOME/mysql/mysqld.pid
	touch  $MYSQL_HOME/mysql/mysql.sock
	#复制
	CNFPATH=$(find . -name 'my-default.cnf')
	cp $CNFPATH $MYSQL_HOME/mysql/support-files
	#参数检查
	checkCNF
	#复制到etc
	cp $(find $MYSQL_HOME/mysql/ -name "my-default.cnf") /etc/my.cnf
	cp $(find $MYSQL_HOME/mysql/ -name "mysql.server") /etc/init.d/mysqld
	chown -R mysql:mysql $MYSQL_HOME/mysql/
	#环境变量
	echo "export PATH=$MYSQL_HOME/mysql/bin:\$PATH" >> /etc/profile
	#echo "export MYSQL_HOME_PATH=$MYSQL_HOME/mysql/bin" >> /etc/profile
	#source /etc/profile
	#执行安装
	$MYSQL_HOME/mysql/bin/mysqld_safe --initialize --user=mysql --basedir=$MYSQL_HOME/mysql --datadir=$MYSQL_HOME/mysql/data
	#$MYSQL_HOME/mysql/bin/mysqld --initialize --user=mysql --basedir=$MYSQL_HOME/mysql --datadir=$MYSQL_HOME/mysql/data
	#$MYSQL_HOME/mysql/bin/mysqld_safe --user=mysql &
	#自启动
	chkconfig --add mysqld
	#临时密码
	#cat /var/log/mysqld.log |grep "temporary password"|awk -F':' '{print $4}'
	cat $MYSQL_HOME/mysql/log/mysqld.log |grep "temporary password"|awk -F'for' '{print $2}' > passwd.txt
	/etc/init.d/mysqld start
	#开启3306端口(centos6)
	iptables -A INPUT -p tcp -m state --state  NEW  -m tcp --dport 3306 -j ACCEPT
	/etc/init.d/iptables save
fi
ps -ef |grep mysql|grep -v grep
echo "mysql installed.."
echo "temporary password: " $(cat $(find . -name 'passwd.txt')) 
sleep 1s
#ps -ef |grep install_mysql|grep -v grep |awk '{print $2}'|xargs kill
exit 0

my-default.cnf如下

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock 
 
[mysqld_safe]
log-error = /usr/local/mysql/log/mysqld.log
pid-file = /usr/local/mysql/mysqld.pid

[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
 
 
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
 
 
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
# server_id = .....
socket = /usr/local/mysql/mysql.sock
 
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M?
 
 
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8

# 取消密码验证
#skip-grant-tables
 
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

 

你可能感兴趣的:(MySQL)