[shell脚本] mysql服务启动脚本

liushun 2019-12-20

服务启动脚本(初始化、启动、登录)

#!/bin/bash

export PID=/usr/local/nestdb_master/bin/mysqld
export PASSWORD=123456


function status()
{
    ps -ef | grep -v ‘grep‘|grep ${PID} >/dev/null

    if [ $? -eq 0 ]; then
        echo "running"
        return 0
    else
        echo "not running"
        return 1
    fi
}

function start()
{
    status
    if [ $? -eq 0 ]; then
        return 0
    fi

    echo "start begin"

    /usr/local/nestdb_master/bin/mysqld --defaults-file=/usr/local/nestdb_master/my.cnf --basedir=/usr/local/nestdb_master --datadir=/usr/local/nestdb_master/var --user=root --plugin-dir=/usr/local/nestdb_master/lib/plugin --open-files-limit=10000 --pid-file=/usr/local/nestdb_master/var/mysql.pid --socket=/usr/local/nestdb_master/tmp/mysql.sock --port=9998 >/dev/null & 

    sleep 3
    ps -ef |grep -v ‘grep‘ | grep ${PID}
    if [ $? -eq 0 ]; then
        echo "start ok"
        return 0
    fi
    echo "start fail"
    return 1 
}


function init()
{
    echo "clean dir: var log tmp"

    cd  /usr/local/nestdb_master
    rm -rf var log tmp
    mkdir var log tmp
    chown mysql:mysql var log tmp

    if [ $? -eq 0 ]; then 
        echo "clean dir ok"
    fi

    echo "init begion ..."

    #/usr/local/nestdb_master/bin/mysqld.init   --defaults-file=/usr/local/nestdb_master/my.cnf --initialize --user=mysql
    /usr/local/nestdb_master/bin/mysqld --defaults-file=/usr/local/nestdb_master/my.cnf --initialize --user=mysql

    cd /usr/local/nestdb_master

    echo "clean remote dir"
    ssh -i /data/id_ras  10.0.199.8  rm -rf /usr/local/nestdb_master/var*

    echo "scp -r var to remote"
    scp -r -i /data/id_ras  /usr/local/nestdb_master/var  10.0.199.8:/usr/local/nestdb_master/

    if [ $? -eq 0 ]; then
        echo "scp ok"
    fi

    echo "init end"
}

function stop()
{
    status
    if [ $? -eq 1 ]; then
        return 0
    fi

    ps -ef | grep mysql | grep -v ‘grep‘ | awk ‘{print $2}‘ | xargs kill -9 

    ps -ef | grep mysql | grep -v ‘grep‘

    if [ $? -eq 1 ]; then
        echo "stop ok"
    fi
}

function login()
{
    /usr/local/nestdb_master/bin/mysql -uroot -S /usr/local/nestdb_master/tmp/mysql.sock -P9988 -p${PASSWORD}
}

function sed_innodb()
{
    cat /usr/local/nestdb_master/var/innodb_repl.info
    sed -i ‘1{s/0/1/}‘ /usr/local/nestdb_master/var/innodb_repl.info
    echo "after sed:"
    cat /usr/local/nestdb_master/var/innodb_repl.info
}

case $1 in
    ‘help‘)
        echo "status|init|start|stop|login|sed"
        ;;
    ‘status‘)
        status 
        ;;
    ‘init‘)
        init
        ;;
    ‘sed‘)
        sed_innodb
        ;;
    ‘start‘)
        start
        ;;
    ‘stop‘)
        stop 
        ;;
    ‘login‘)
        login 
        ;;
    esac

相关推荐