#!/bin/bash
# -*- codeing = utf-8 -*-
# @Time : 2023/09/25 22:11
# @Author : lidachuan
# @File : mysql_install.sh
# @Software : PyCharm
#1.设置mysql安装目录
mysql_install=/home/mysql
#2.设置mysql安装目录
mysql_data=/testdata
#3.设置mysql端口
mysql_port=3306
#4.设置mysql版本号
mysql_version=mysql-5.7.26
#清理系统环境
echo "清理系统环境"
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
#系统环境检查
echo "系统环境检查"
find / -iname libaio.so.1
echo "/usr/lib64/libaio.so.1依赖检查"
#安装目录并创建用户
echo "安装目录并创建用户"
mkdir -p $mysql_install
userdel mysql
groupadd mysql && useradd -r -g mysql mysql
chown -R mysql:mysql $mysql_install
#解压安装包
echo "解压安装包"
tar -zxvf mysql-5.7.26.tar.gz -C $mysql_install
sleep 3
mv $mysql_install/$mysql_version $mysql_install/mysql
#创建mysql数据目录
echo "创建数据目录"
mkdir -p $mysql_data/mysql
cd $mysql_data/mysql
mkdir {data,logs}
touch $mysql_data/mysql/logs/error.log
chown -R mysql:mysql $mysql_data/mysql
echo "写入/etc/my.cnf配置"
echo "
[mysql]
socket=$mysql_data/mysql/mysql.sock
# set mysql client default chararter
default-character-set=utf8
[mysqld]
socket=$mysql_data/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_data/mysql/data/
pid-file=$mysql_data/mysql/data/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_data/mysql/logs/error.log
#开启slow日志
slow_query_log = ON
slow_query_log_file = $mysql_data/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
chmod 755 /etc/my.cnf
#初始化mysql并设置空密码使用-initialize生成随机密码,使用-initialize-insecure生成空密码。默认帐号root,后面的-user=mysql不更改
echo "初始化mysql"
sudo -u mysql $mysql_install/mysql/bin/mysqld --initialize
#mysql添加到系统服务
echo "把mysql添加到系统服务"
cp $mysql_install/mysql/support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
#启动mysql进程
echo "启动mysql"
sudo -u mysql /etc/init.d/mysql start
#mysql开机启动
echo "设置mysql开机自启动"
cd /etc/init.d
chkconfig --add mysql
chkconfig --level 345 mysql on
#添加环境变量
echo "添加环境变量"
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是否启动
echo "检查mySQL是否启动"
ss -antlp |grep $mysql_port
if [ $? -eq 0 ];then
echo "mysql部署success"
else
echo "mysql部署fail"
fi
#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