me小怪兽

导航

mysql自动安装脚本

1、下载mysql安装包

https://downloads.mysql.com/archives/community/  下载地址

 

 

https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz  复制、立即下载

2、上传安装包和安装脚本

mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz  安装包

mysql_install5.7.sh  安装脚本

注:俩个文件同时放在一个目录下

[root@localhost]# vim mysql_install5.7.sh  #添加安装脚本

#!/bin/bash
# -*- codeing = utf-8 -*-
# @Time : 2021/11/7 22:11
# @Author : xiaoguaishou
# @File : mysql_install5.7.sh
# @Software : PyCharm

#1.设置mysql安装目录
mysql_install=/home
#2.设置mysql端口
mysql_port=20001
#3.设置mysql版本号
mysql_version=mysql-5.7.36-linux-glibc2.12-x86_64

#清理系统环境
rpm -e `rpm -qa |grep mariadb` --nodeps
rm -rf /var/lib/mysql
rm -rf /etc/my.cnf.d/
rm -rf /etc/my.cnf
rm -rf /etc/init.d/mysql
#ps aux |grep mysql |grep -v grep |awk '{print $2}'|xargs kill -9
sleep 5
#系统环境检查
find / -iname libaio.so.1
echo "/usr/lib64/libaio.so.1依赖检查"
sleep 5
#创建安装目录
mkdir -p $mysql_install
#解压安装包
tar -zxvf $mysql_version.tar.gz -C $mysql_install
sleep 3
mv $mysql_install/$mysql_version $mysql_install/mysql
cd $mysql_install/mysql
#创建mysql数据存放目录
mkdir {data,logs}
touch $mysql_install/mysql/logs/error.log

echo "
[mysql]
socket=$mysql_install/mysql/mysql.sock
# set mysql client default chararter
default-character-set=utf8

[mysqld]
socket=$mysql_install/mysql/mysql.sock
# set mysql server port
port = $mysql_port
# set mysql install base dir
basedir=$mysql_install/mysql
# set the data store dir
datadir=$mysql_install/mysql/data/
pid-file=$mysql_install/mysql/mysql.pid
# set the number of allow max connnection
max_connections=2048 #最大连接数
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
# 设置MySQL对表名等不区分大小写
lower_case_table_names=1
max_allowed_packet=200M
explicit_defaults_for_timestamp=true
#阻止过多尝试失败的客户端以防止暴力破解密码的情况,与性能并无太大的关系
max_connect_errors=30
#此参数确定数据日志文件的大小,以M为单位,根据数据更新频率调整。
innodb_log_file_size=50
#指定大小的内存来缓冲数据和索引,最大可以把该值设置成物理内存的80%
innodb_buffer_pool_size=10G
key_buffer_size=16M
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
log-bin=mysql-bin #添加这一行就ok
binlog-format=ROW #选择row模式
expire_logs_days=7 #日志的保存天数
#错误日志存放地址
log-error = $mysql_install/mysql/logs/error.log
#开启slow日志
slow_query_log = ON
slow_query_log_file = $mysql_install/mysql/logs/slow.log
long_query_time = 20
log_queries_not_using_indexes
server_id=1 #配置mysql replaction需要定义,不能和canal的slaveId重复
innodb_file_per_table=1

[mysql.server]
user=mysql
basedir=$mysql_install/mysql
#可以防止出现插入中文报错;如果此时不生效,可以强制在创建表的时候指定使用utf8的编码集
[client]
default-character-set = utf8
" > /etc/my.cnf

#创建用户
useradd -s /sbin/nologin -M mysql
#编译成功后执行安装命令
chown -R mysql:mysql $mysql_install/mysql
chmod -R 755 $mysql_install/mysql

#初始化mysql并设置空密码使用-initialize生成随机密码,使用-initialize-insecure生成空密码。默认帐号root,后面的-user=mysql不更改
./bin/mysqld --initialize-insecure --user=mysql --basedir=$mysql_install/mysql/ --datadir=$mysql_install/mysql/data/
#重新初始化时把data的数据清理掉
sleep 5
#mysql添加到系统服务
cp $mysql_install/mysql/support-files/mysql.server /etc/init.d/mysql
#启动mysql进程
/etc/init.d/mysql start
#mysql开机启动
cd /etc/init.d
chkconfig --add mysql
chkconfig --level 345 mysql on
#添加环境变量
sou=`cat /etc/profile |grep "mysql/bin" |wc -l`
if [ $sou -eq 0 ];then
echo export PATH='$PATH':$mysql_install/mysql/bin >> /etc/profile
fi

#检查mysql是否启动
ss -antlp |grep $mysql_port
if [ $? -eq 0 ];then
echo "mysql部署success"
else
echo "mysql部署fail"
fi
sleep 5
source /etc/profile
#前面初始化mysql为空密码
#1、新建远程访问用户:
mysql -e "grant all on *.* to deploy@'%' identified by 'VqxSINILWT7i!9Db';"
#2、权限查看:
#mysql> FLUSH PRIVILEGES;
mysql -e "flush privileges;"
#设置root登陆密码
mysql -e "SET PASSWORD = PASSWORD('tzIrmrMrRsCqFg&V');"
#3、输出密码:
echo '
root密码:tzIrmrMrRsCqFg&V
deploy密码:VqxSINILWT7i!9Db
'
firewall-cmd --zone=public --add-port=$mysql_port/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-port

3、使用方式

[root@localhost]# chmod +x mysql_install5.7.sh  #授权

[root@localhost]# ./mysql_install5.7.sh  #执行安装

等待安装完成..........哈哈,安装mysql就是这么简答

 

posted on 2022-08-21 22:57  me小怪兽  阅读(426)  评论(0编辑  收藏  举报