1. 程式人生 > >實驗:基於YUM安裝的多實例

實驗:基於YUM安裝的多實例

my.cnf color iad 腳本 端口 https tar exit running

實驗:基於YUM安裝的多實例

1.安裝mariadb

yum install mariadb

2.創建多實例對應的目錄結構

mkdir /mysql/{3306,3307,3308}/{data,,socket,log,bin,pid} -pv
技術分享圖片
技術分享圖片
chown -R mysql.mysql /mysql
技術分享圖片

3.創建多實例的數據庫文件

mysql_install_db --datadir=/mysql/3306/data/ --user=mysql
技術分享圖片
mysql_install_db --datadir=/mysql/3307/data/ --user=mysql
mysql_install_db --datadir=/mysql/3308/data/ --user=mysql

4.創建對應配置文件

cp /etc/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

技術分享圖片

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
技術分享圖片

5.準備各實例的啟動腳本

vim /mysql/{3306,3307,3308}/bin/mysqld
cat /mysq/3306/bin/mysqld

#!/bin/bash  
port=3306  
mysql_user="root"  
mysql_pwd=" "  
cmd_path="/usr/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
技術分享圖片

6.啟動和關閉實例

/mysql/{3306,3307,3308}/bin/mysqld start
/mysql/{3306,3307,3308}/bin/mysqld stop
技術分享圖片
記得更改腳本端口號

7.測試連接

mysql -S /mysql/{3306,3307,3308}/socket/mysql.sock
技術分享圖片

8.安全加固

mysqladmin -S /mysql/{3306,3307,3308}/socket/mysql.sock password ‘centos‘
vim /mysql/{3306,3307,3308}/bin/mysqld 加上對應centos口令

實驗:基於YUM安裝的多實例