实验:基于YUM安装的多实例

1.安装mariadb

yum install mariadb

2.创建多实例对应的目录结构

mkdir /mysql/{3306,3307,3308}/{data,,socket,log,bin,pid} -pv
实验:基于YUM安装的多实例
实验:基于YUM安装的多实例_第1张图片
chown -R mysql.mysql /mysql
实验:基于YUM安装的多实例

3.创建多实例的数据库文件

mysql_install_db --datadir=/mysql/3306/data/ --user=mysql
实验:基于YUM安装的多实例
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
实验:基于YUM安装的多实例_第2张图片

cp /mysql/3306/etc/my.cnf /mysql/3307/etc/my.cnf
/mysql/3307/etc/my.cnf
实验:基于YUM安装的多实例
实验:基于YUM安装的多实例
cp /mysql/3306/etc/my.cnf /mysql/3308/etc/my.cnf
/mysql/3308/etc/my.cnf
实验:基于YUM安装的多实例

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
实验:基于YUM安装的多实例

6.启动和关闭实例

/mysql/{3306,3307,3308}/bin/mysqld start
/mysql/{3306,3307,3308}/bin/mysqld stop
实验:基于YUM安装的多实例_第3张图片
记得更改脚本端口号

7.测试连接

mysql -S /mysql/{3306,3307,3308}/socket/mysql.sock
实验:基于YUM安装的多实例

8.安全加固

mysqladmin -S /mysql/{3306,3307,3308}/socket/mysql.sock password 'centos'
vim /mysql/{3306,3307,3308}/bin/mysqld 加上对应centos口令