美文网首页
597.【linux】分享一个安装mysql的脚本[续篇]

597.【linux】分享一个安装mysql的脚本[续篇]

作者: 七镜 | 来源:发表于2023-03-02 21:24 被阅读0次

    之前写了个 426.【数据库】centos 7系统,二进制方式安装mysql ,很喜欢用二进制装mysql的方法,现在这个需求又出来了,我要装一个mysql在服务器上,于是乎,就想着这次把整个安装步骤,写出一个脚本出来。

    直接上脚本:

    
    #!/bin/bash
    
    #Program:
    
    # setup mysql
    
    #History:
    
    #2023/02/26 junfenghe.cloud@qq.com version:0.0.1
    
    path=/bin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:~/bin
    
    export path
    
    if [ -n "$1" ]
    
    then
    
        echo "mysql will be installed in $1"
    
    else
    
        echo "Please input path which mysql will be installed."
    
        echo "eg: $0 /usr/local"
    
        exit -1
    
    fi
    
    type 'mysql'
    
    if [ $? -eq 0 ]
    
    then
    
        echo 'mysql has installed'
    
        echo 'please uninstall mysql such as: check /var/lib/mysql  && /etc/profile && /etc/init.d/mysql.server && /var/log/mariadb'
    
        exit -1
    
    fi
    
    bin=$(dirname ${BASH_SOURCE-$0})
    
    bin=$(cd $bin ; pwd)
    
    cd $bin
    
    file_mysql=mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
    
    path_mysql=`echo $file_mysql | awk -F '.tar' '{print $1}'`
    
    path_mysql_full="$(cd $1 ; pwd)/$path_mysql"
    
    export PATH=$PATH:tmp/mysql-8.0.31-linux-glibc2.12-x86_64/bin
    
    msg_info="[INFO]"
    
    # check if exists group:mysql
    
    echo '@Step1 -> groupadd mysql'
    
    if_exists_group_mysql=$(cat /etc/group| grep mysql)
    
    if [ -n "${if_exists_group_mysql}" ]
    
    then
    
        echo ${msg_info}' group:mysql exists.'
    
    else
    
        groupadd mysql
    
        echo ${msg_info}' groupadd mysql ok.'
    
    fi
    
    if_exists_user_mysql=$(cat /etc/shadow | grep mysql)
    
    if [ -n "${if_exists_user_mysql}" ]
    
    then
    
        echo ${msg_info}‘ group:mysql exists’
    
    else
    
        useradd -r -g mysql -s /bin/false mysql
    
        echo ${msg_info} ' useradd -r -g mysql -s /bin/false mysql ok'
    
    fi
    
    if [ -f $file_mysql ]
    
    then
    
        echo "exist "$file_mysql
    
    else
    
        wget "https://downloads.mysql.com/archives/get/p/23/file/$file_mysql"
    
    fi
    
    tar -xvf $file_mysql -C $1/
    
    cd $1/$path_mysql
    
    mkdir -p mysql-files
    
    chown mysql:mysql mysql-files
    
    chmod 750 mysql-files
    
    yum install -y libaio
    
    ./bin/mysqld --initialize --user=mysql
    
    ./bin/mysql_s之前 写了个 [426.【数据库】centos 7系统,二进制方式安装mysql](https://www.jianshu.com/p/f391c72ea730?v=1677768474275) ,很喜欢用二进制装mysql的方法,现在这个需求又出来了,我要装一个mysql在服务器上,于是乎,就想着这次把整个安装步骤,写出一个脚本出来。
    
    直接上脚本:
    
    ~~~shell
    
    #!/bin/bash
    
    #Program:
    
    # setup mysql
    
    #History:
    
    #2023/02/26 junfenghe.cloud@qq.com version:0.0.1
    
    path=/bin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:~/bin
    
    export path
    
    if [ -n "$1" ]
    
    then
    
        echo "mysql will be installed in $1"
    
    else
    
        echo "Please input path which mysql will be installed."
    
        echo "eg: $0 /usr/local"
    
        exit -1
    
    fi
    
    type 'mysql'
    
    if [ $? -eq 0 ]
    
    then
    
        echo 'mysql has installed'
    
        echo 'please uninstall mysql such as: check /var/lib/mysql  && /etc/profile && /etc/init.d/mysql.server && /var/log/mariadb'
    
        exit -1
    
    fi
    
    bin=$(dirname ${BASH_SOURCE-$0})
    
    bin=$(cd $bin ; pwd)
    
    cd $bin
    
    file_mysql=mysql-8.0.31-linux-glibc2.12-x86_64.tar.xz
    
    path_mysql=`echo $file_mysql | awk -F '.tar' '{print $1}'`
    
    path_mysql_full="$(cd $1 ; pwd)/$path_mysql"
    
    export PATH=$PATH:tmp/mysql-8.0.31-linux-glibc2.12-x86_64/bin
    
    msg_info="[INFO]"
    
    # check if exists group:mysql
    
    echo '@Step1 -> groupadd mysql'
    
    if_exists_group_mysql=$(cat /etc/group| grep mysql)
    
    if [ -n "${if_exists_group_mysql}" ]
    
    then
    
        echo ${msg_info}' group:mysql exists.'
    
    else
    
        groupadd mysql
    
        echo ${msg_info}' groupadd mysql ok.'
    
    fi
    
    if_exists_user_mysql=$(cat /etc/shadow | grep mysql)
    
    if [ -n "${if_exists_user_mysql}" ]
    
    then
    
        echo ${msg_info}‘ group:mysql exists’
    
    else
    
        useradd -r -g mysql -s /bin/false mysql
    
        echo ${msg_info} ' useradd -r -g mysql -s /bin/false mysql ok'
    
    fi
    
    if [ -f $file_mysql ]
    
    then
    
        echo "exist "$file_mysql
    
    else
    
        wget "https://downloads.mysql.com/archives/get/p/23/file/$file_mysql"
    
    fi
    
    tar -xvf $file_mysql -C $1/
    
    cd $1/$path_mysql
    
    mkdir -p mysql-files
    
    chown mysql:mysql mysql-files
    
    chmod 750 mysql-files
    
    yum install -y libaio
    
    ./bin/mysqld --initialize --user=mysql
    
    ./bin/mysql_ssl_rsa_setup
    
    mkdir -p /var/log/mariadb
    
    touch /var/log/mariadb/mariadb.log
    
    chown -R mysql:mysql /var/log/mariadb
    
    ./bin/mysqld_safe --user=mysql & 
    
    #sed -i "s/basedir=\$/basedir=$(pwd)/g" support-files/mysql.server
    
    #sed -i "s/datadir=\$/basedir=$(pwd)\/data/g" support-files/mysql.server
    
    cp support-files/mysql.server /etc/init.d/mysql.server
    
    echo "export PATH=\$PATH:$path_mysql_full/bin" >> /etc/profile
    
    source /etc/profile
    
    echo 'Successfully!!!'
    
    echo 'you can execute  [/etc/init.d/mysql.server start] to start mysql'
    
    exit 0
    
    
    • 推荐执行的命令:./scripts.sh '/usr/local/mysql'

    • 整个安装脚本是上一篇文章的完整命令整合的

    • 脚本执行完毕之后,切记屏幕上会出现初始的root密码,不要看漏了,不然登录不上会有点麻烦,然后根据提示执行:/etc/init.d/mysql.server start 即可启动mysql。

    • 如果启动有问题,可以根据脚本去修改 /etc/init.d/mysql.server

    相关文章

      网友评论

          本文标题:597.【linux】分享一个安装mysql的脚本[续篇]

          本文链接:https://www.haomeiwen.com/subject/mexsldtx.html