1. 程式人生 > >基於二進制安裝的mariadb實現多實例

基於二進制安裝的mariadb實現多實例

修改 $1 -a 安裝 adb access ech and down

基於二進制安裝的mariadb實現多實例

1、mariadb版本:10.2.23

2、二進制安裝mariadb的shell腳本

    #!/bin/bash
    id mysql &>/dev/null
    if [ `echo $?` -ne 0 ];then
        userdel -r mysql &>/dev/null
        useradd -r -u 336 -s /sbin/nologin -d /data/mysql mysql &>/dev/null
    else
        useradd -r -u 336 -s /sbin/nologin -d /data/mysql mysql &>/dev/null
    fi 
    rpm -q libaio &>/dev/null
    [ `echo $?` -ne 0 ] && yum -y install libaio
    rpm -q expect &>/dev/null
    [ `echo $?` -ne 0 ] && yum -y install expect
    if [ -e mariadb-10.2.23-linux-x86_64.tar.gz ];then
        #此文件可能會造成影響,所以先清空 
        > /etc/my.cnf
        tar xf mariadb-10.2.23-linux-x86_64.tar.gz -C /usr/local/
        cd  /usr/local/
        ln -s mariadb-10.2.23-linux-x86_64/ mysql
        chown -R root.root /usr/local/mysql/
        echo ‘PATH=/usr/local/mysql/bin:$PATH‘ >/etc/profile.d/mysql.sh 
        mkdir /data/mysql -p
        chown mysql.mysql /data/mysql/
        cd /usr/local/mysql
        ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
        mkdir -p /etc/mysql
        cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
        #禁止主機名解析,建議使用
        sed -ri ‘/^\[mysqld\]/askip_name_resolve = on‘ /etc/mysql/my.cnf
        sed -ri ‘/^\[mysqld\]/adatadir=\/data\/mysql‘ /etc/mysql/my.cnf
        cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
        chkconfig --add mysqld
        service mysqld start
        expect <<EOF
            spawn mysql_secure_installation
            expect {
            "Enter current password for root" { send "\n";exp_continue }
            "Set root password" { send "\n";exp_continue }
            "New password" { send "123456\n";exp_continue }
            "Re-enter new password" { send "123456\n";exp_continue }
            "Remove anonymous users" { send "y\n";exp_continue }
            "Disallow root login remotely" { send "y\n";exp_continue }
            "Remove test database and access to it" { send "y\n";exp_continue }
            "Reload privilege tables now" { send "y\n" }
            }
    #expect "]#" { send "exit\n" }
    expect eof
    EOF
    mysqladmin -uroot -p123456 ping &>/dev/null
    [ `echo $?` -eq 0 ] && echo ‘mysql is running !‘ || echo ‘mysql is stopped‘ 
else
    echo -e "mariadb-10.2.23-linux-x86_64.tar.gz is not exist"
    exit
fi

3、實現多示例

1、創建多實例對應的目錄結構

    mkdir -p /mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid}
    chown -R mysql.mysql /mysql/

2、創建多實例的數據庫文件

    /usr/local/mysql/scripts/mysql_install_db --datadir=/mysql/3306/data/ --user=mysql
    /usr/local/mysql/scripts/mysql_install_db --datadir=/mysql/3307/data/ --user=mysql
    /usr/local/mysql/scripts/mysql_install_db --datadir=/mysql/3308/data/ --user=mysql

3、創建對應配置文件

    cp /etc/mysql/my.cnf /mysql/3306/etc/
    vim /mysql/3306/etc/my.cnf
        [mysqld]
        port=3306 加一行                                                                                        
        datadir=/mysql/3306/data
        socket=/mysql/3306/socket/mysql.sock
        [mysqld_safe]
        log-error=/mysql/3306/log/mariadb.log
        pid-file=/mysql/3306/pid/mariadb.pid
    #3307和3308同步修改
        cp /mysql/3306/etc/my.cnf  /mysql/3307/etc/my.cnf   
        /mysql/3307/etc/my.cnf 修改
        cp /mysql/3306/etc/my.cnf  /mysql/3308/etc/my.cnf   
        /mysql/3308/etc/my.cnf 修改   

4、準備各實例的啟動腳本並添加執行權限

    vi /mysql/{3306,3307,3308}/bin/mysqld   
    cat /mysql/3306/bin/mysqld              
        #!/bin/bash                                                                                                                                                                      
        port=3306                                                                                         
        mysql_user="root"                                                                                 
        mysql_pwd="123456"                                                                                
        #cmd_path="/mysql/3306/bin"                                                                       
        cmd_path="/usr/local/mariadb-10.2.23-linux-x86_64/bin"                                            
        mysql_basedir="/mysql"                                                                            
        mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"                                           
        function_start_mysql()                                                                            
        {                                                                                                 
            if [ ! -e "$mysql_sock" ];then                                                                
              printf "Starting MySQL...\n"                                                                
              ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  &> /dev/null  &
            else                                                                                          
              printf "MySQL is running...\n"                                                              
              exit                                                                                        
            fi                                                                                            
        }                                                                                                                                                                                                 
        function_stop_mysql()                                                                             
        {                                                                                                 
            if [ ! -e "$mysql_sock" ];then                                                                
               printf "MySQL is stopped...\n"                                                             
               exit                                                                                       
            else                                                                                          
               printf "Stoping MySQL...\n"                                                                
               ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown                                                     
           fi                                                                                             
        }                                                                                                 
        function_restart_mysql()                                                                          
        {                                                                                                 
            printf "Restarting MySQL...\n"                                                                
            function_stop_mysql                                                                           
            sleep 2                                                                                       
            function_start_mysql                                                                          
        }                                                                                                 
        case $1 in                                                                                        
        start)                                                                                            
            function_start_mysql                                                                          
        ;;                                                                                                
        stop)                                                                                             
            function_stop_mysql                                                                           
        ;;                                                                                                
        restart)                                                                                          
            function_restart_mysql                                                                        
        ;;                                                                                                
        *)                                                                                                
            printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"                    
        esac                                                                                              
    #添加執行權限
    chmod +x /mysql/{3306,3307,3308}/bin/mysqld

5、啟動服務並做安全加固(如果不做安全加固,則stop時的腳本有問題,密碼不正確)

    /mysql/{3306,3307,3308}/bin/mysqld start
    #安全加固
    /usr/local/mysql/bin/mysql_secure_installation -S /mysql/{3306,3307,3308}/socket/mysql.sock (輸入的密碼與啟動腳本一致)

6、測試連接

    mysql -uroot -p123456 -S /mysql/{3306,3307,3308}/socket/mysql.sock

基於二進制安裝的mariadb實現多實例