关于mysql:一键编译部署Mysql

42次阅读

共计 5315 个字符,预计需要花费 14 分钟才能阅读完成。

#!/bin/bash
# 一键装置 mysql5.7.35

#安装包下载目录
MYSQL_DOWNLOAD_DIR=/data/software/
#数据包目录
MYSQL_INSTALL_DIR=/data/modules/

#数据库信息
info_mysql(){
    echo "======> MYSQL 装置信息 <======";
    echo "Mysql 版本:5.7.35";
    echo "下载目录:/data/software/";
    echo "装置目录:/data/modules/";
}

#旧版 mysql
uninstall_mysql(){
    echo "\n======> MYSQL 装置筹备 <======";
    echo "残留 Mysql 检测......."
    echo "卸载旧版 mysql......."
    echo [+] Uninstall Mysql
    yum remove mysql* -y
    yum remove mariadb* -y
    if [-f "/etc/my.cnf"];then
        rm -rf /etc/my.cnf
    fi

    if [-f "/root/.mysql_sercret"];then
        rm -rf /root/.mysql_sercret
    fi

    if [-d "/var/lib/mysql"];then
        rm -rf /var/lib/mysql
    fi
 
    if [-d "/usr/lib64/mysql"];then
        rm -rf /usr/lib64/mysql
    fi
 
    if [-d "/usr/share/mysql"];then
        rm -rf /usr/share/mysql
    fi
    echo "卸载实现!"
}
 
#下载 mysql
IS_CINTINUE='y';
download_mysql(){
    echo "装置目录检测......."
    if [! -d "/data/software"]; then
        echo "创立下载目录......."
        mkdir -p /data/software/
    fi
    if [! -d "/data/modules/mysql/"]; then
        echo "创立数据目录......."
        mkdir -p /data/modules/
    fi
    echo "目录准备就绪 !";
    echo "======> MYSQL 装置过程 <======";
    cd /data/software/
    read -p "确认是否装置?【y/n】" IS_CINTINUE
    if [[$IS_CINTINUE == 'y']]; then
        echo "请稍等, 大概须要 20 分钟, 正在下载 MYSQL.......";
        cd /data/software/
        file="/data/software/mysql-boost-5.7.35.tar.gz"
        if [! -f $file]; then
            echo "File not found!"
            yum install -y wget && wget -c https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.35.tar.gz;
            echo "下载实现, 正在解压.......";
            tar -zxvf mysql-boost-5.7.35.tar.gz -C /data/modules/
            cd /data/modules/
            mv /data/modules/mysql-5.7.35/ mysql
            exit 0
        fi
        echo "解压实现, 执行装置.......";
        
        #yum install -y wget && wget -c https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.35-el7-x86_64.tar.gz;
    else
        echo "请您自行下载安装包!";
        exit;
    fi

}

#装置 mysql
install_mysql(){
    echo "======> 装置 mysql 依赖....... <======";
    yum -y install gcc gcc-devel gcc-c++ gcc-c++-devel autoconf* automake* zlib* libxmlncurses-devel ncurses libgcrypt libtool* cmake openssl openssl-devel bisonbison-devel perl-Data-Dumper boost boost-doc boost-devel
    yum -y install gcc make cmake ncurses-devel libxml2-devel libtool-ltdl-devel gcc-c++ autoconf automake bison zlib-devel
    echo "======> 装置前的设置 <======";
    sleep 2s
    cd /data/modules/mysql/
    mkdir data
    sleep 2s
    cmake -DCMAKE_INSTALL_PREFIX=/data/modules/mysql/ \
    -DMYSQL_DATADIR=/data/modules/mysql/data \
    -DSYSCONFDIR=/etc \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_MEMORY_STORAGE_ENGINE=1 \
    -DWITH_READLINE=1 \
    -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
    -DMYSQL_TCP_PORT=3306 \
    -DWITH_BOOST=/data/modules/mysql/boost \
    -DENABLED_LOCAL_INFILE=1 \
    -DWITH_PARTITION_STORAGE_ENGINE=1 \
    -DEXTRA_CHARSETS=all \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci
    echo "======> 创立 mysql 用户和组 <======";
    cd /data/modules/mysql/
    sleep 2s
    make && make install
    groupadd mysql && useradd mysql -g mysql -M -s /sbin/nologin && chown mysql:mysql /data/modules/mysql/
    # if [$? -eq 0];then
    #         echo "======> 设置胜利!开始编译装置,大概须要 20 分钟,急躁期待...<======";
    #         sleep 2s
    #         make && make install
    #         if [$? -eq 0]; then
    #             cd ~
    #             echo "======> 编译胜利!开始设置组和用户!<======";
    #             sleep 2s
    #             groupadd mysql && useradd mysql -g mysql -M -s /sbin/nologin && chown mysql:mysql /data/modules/mysql/
    #         else
    #             echo "======> 编译失败!<======";
    #             exit;
    #         fi
    #     else
    #             echo "======> 设置失败!<======";
    #             exit;
    # fi
    echo "======> 装置实现! <======";
}

#配置 Mysql
configuration_mysql(){

echo "======> 配置 mysql <======";
cd /data/modules/mysql/
touch mysql.pid
echo "10254" > /data/modules/mysql/mysql.pid
touch mysql.err
touch mysqld.sock
chown mysql:mysql /data/modules/mysql/mysql.pid
chown mysql:mysql /data/modules/mysql/mysql.err
chown mysql:mysql /data/modules/mysql/data/mysqld.sock
cat > /etc/my.cnf<<EOF
[client]
port            = 3306
socket          = /data/modules/mysql/data/mysqld.sock
[mysqld]
#server-id  = 6878                           #运行 ID
bind-address=0.0.0.0                        #绑定地址运行近程连贯
port=3306                                   #Mysql 凋谢的端口
user=mysql                                  #数据库登录用户
basedir=/data/modules/mysql/                #Mysql 装置的绝对路径
datadir=/data/modules/mysql/data            #Mysql 数据寄存的绝对路径
socket=/data/modules/mysql/data/mysqld.sock #套接字文件
log-error=/data/modules/mysql/mysql.err     #mysql 生成的谬误日志寄存的门路
pid-file=/data/modules/mysql/mysql.pid      #为 mysqld 程序指定一个寄存过程 ID 的文件
character_set_server=utf8                   #数据库字符编码
symbolic-links=0                            #是否开启链接符号
explicit_defaults_for_timestamp=true        #数据库 timestamp 类型的列自动更新  
EOF


sleep 2s
echo "初始化 mysql.......";
cd /data/modules/mysql/bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/data/modules/mysql/ --datadir=/data/modules/mysql/data/ --user=mysql --initialize
ln -s  /data/modules/mysql/bin/mysql    /usr/bin
}

#将 mysql 设置成为服务启动
service_mysql(){
    echo "======> 制作服务启动 <======";
    cd /data/modules/mysql
    /bin/cp -rf support-files/mysql.server /etc/init.d/mysqld
    chmod a+wrx /etc/init.d/mysqld
    if [$? -eq 0]; then
        echo "======> 服务设置胜利!<======";
        cd ~
        service mysqld start
        sleep 2s
        service mysqld stop
        sleep 2s
    else
        echo "======> 服务设置失败!<======";
    fi

    echo "======> 增加到开机启动项!<======";
    chkconfig --add mysqld
    if [$? -eq 0]; then
        echo "======> 设置开机启动项胜利!<======";
    else
        echo "======> 设置开机启动项失败!<======";
    fi
    service mysqld stop
    echo "======> 配置环境变量 <======";
    echo "export PATH=$PATH:/data/modules/mysql/bin/" >>/etc/profile
    source /etc/profile
    echo "启动 mysql 服务.......";service mysqld start
    echo "Mysql 曾经启动.......";}


#启动数据库设置明码
# mysql_password='123456'
# up_mysql(){
#     cd /data/modules/mysql/
#     echo "======> 请输出数据库明码: <======";
#     read -p "输出明码:" -s mysql_password echo
#     echo password read, is "$mysql_password"
#     # read -p "确认是否装置?【y/n】" IS_CINTINUE
#     # ./bin/mysqladmin -u root password "123456"
#     if [$? -eq 0];then
#         echo "设置 root 明码胜利!";
#     else
#         echo "设置 root 明码失败!";
#     fi
#     source /etc/profile
# }

#完结装置
end_mysql(){
    echo "运行端口: 3306"
    echo "数据库用户: mysql"
    echo "数据库 root 明码 (root@localhost 前面):"
    cat /data/modules/mysql/mysql.err | grep root@localhost:
    echo "数据库装置门路: /data/modules/mysql/"
    echo "数据库数据门路: /data/modules/mysql/data"
    mysql -uroot -p
}

#装置流程
mysql(){
    #数据库信息
    info_mysql
    #旧版数据库卸载
    uninstall_mysql
    #下载数据库
    download_mysql
    #装置数据库
    install_mysql
    #配置数据库
    configuration_mysql
    #数据库以服务启动
    service_mysql
    #启动 MySQL 设置明码
    #显示数据库信息
    info_mysql
    end_mysql

}
mysql

正文完
 0