• root用户自动安装mysql 5.7脚本


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

    chown  mysql:mysql /etc/my.cnf

    #初始化mysql并设置空密码使用-initialize生成随机密码,使用-initialize-insecure生成空密码。默认帐号root,后面的-user=mysql不更改
    echo "初始化mysql"
    sudo -u mysql  $mysql_install/mysql/bin/mysqld --initialize-insecure


    #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

    脚本测试:

    [root@t3-dtpoc-dtpoc-web06 ~]# sh mysql_install.sh 
    清理系统环境
    rpm: no packages given for erase
    系统环境检查
    /usr/lib64/libaio.so.1
    /usr/lib64/libaio.so.1依赖检查
    安装目录并创建用户
    解压安装包
    mysql-5.7.26/
    mysql-5.7.26/bin/
    mysql-5.7.26/bin/myisam_ftdump
    。。。
    mysql-5.7.26/backup/logs/
    mysql-5.7.26/backup/tmp/
    mysql-5.7.26/backup/chkpoint/
    创建数据目录
    写入/etc/my.cnf配置
    初始化mysql
    把mysql添加到系统服务
    启动mysql
    Starting MySQL... SUCCESS! 
    设置mysql开机自启动
    添加环境变量
    检查mySQL是否启动
    LISTEN    0         459                      *:3306                   *:*        users:(("mysqld",pid=986603,fd=28))                                            
    mysql部署success

    root密码:tzIrmrMrRsCqFg&V
    deploy密码:VqxSINILWT7i!9Db#
    [root@t3-dtpoc-dtpoc-web06 ~]# su - mysql
    [mysql@t3-dtpoc-dtpoc-web06 ~]$ mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 5
    Server version: 5.7.26-log MySQL Community Server (GPL)

    Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    mysql> 

  • 相关阅读:
    《好代码 坏代码》阅读
    【牛客刷题-算法】加精 | 合并两个有序的链表 - 从思路设计、bug排除到最终实现的全过程
    001图机器学习与图神经网络简介
    深度操作系统20.5发布 deepin 20.5更新内容汇总
    TypeScript核心篇——类(class)-可选参数-存取器-构造函数-静态属性方法-抽象类
    netsh interface portproxy端口转发,从本地端口到本地端口不起作用的解决办法
    初级算法_字符串 --- 最长公共前缀
    串口通信
    Mysql —— 子查询的 ”必备“ 练习
    本地环境搭建
  • 原文地址:https://blog.csdn.net/liys0811/article/details/133275751