mysql单点多实例部署方式

一、清理环境

清理方式与mysql-部署相同

二、部署

1.创建mysql目录
mkdir /root/soft
cd /root/soft

2.下载安装包并解压
wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
tar -xf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz -C /opt/
mv /opt/mysql-5.7.28-linux-glibc2.12-x86_64 /opt/mysql5.7.28/


3.创建软连接到指定目录下并更名
ln -s /opt/mysql5.7.28/ /opt/mysql

mkdir -p /opt/mysql_3306
mkdir -p /opt/mysql_3307
mkdir -p /opt/mysql_3308
chown -R mysql.mysql /opt/mysql*

4.安装mysql5.7的依赖包
yum install -y libaio-devel numactl

5.设置环境变量
echo 'export PATH=$PATH:/opt/mysql/bin' >> /etc/profile
source /etc/profile

mysql -V
mysql  Ver 14.14 Distrib 5.7.43, for Linux (x86_64) using  EditLine wrapper

6.编译安装并初始化mysql
cd /opt/mysql/bin
./mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql_3306
./mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql_3307
./mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql_3308

7.编辑配置文件my.cnf,添加配置如下
cat >/etc/mysql_3306.cnf <<'EOF'
[mysqld]
port=3306
user=mysql
basedir=/opt/mysql/
datadir=/opt/mysql_3306/
socket=/opt/mysql_3306/mysql.sock
log_error=/opt/mysql_3306/mysql.log
EOF

cat >/etc/mysql_3307.cnf <<'EOF'
[mysqld]
port=3307
user=mysql
basedir=/opt/mysql/
datadir=/opt/mysql_3307/
socket=/opt/mysql_3307/mysql.sock
log_error=/opt/mysql_3307/mysql.log
EOF

cat >/etc/mysql_3308.cnf <<'EOF'
[mysqld]
port=3308
user=mysql
basedir=/opt/mysql/
datadir=/opt/mysql_3308/
socket=/opt/mysql_3308/mysql.sock
log_error=/opt/mysql_3308/mysql.log
EOF

8.编写启停脚本(只需将3306改成对应端口即可)
cat > /opt/3306.sh <<'EOF'
port="3306"
mysql_user="mysql"
Cmdpath="/opt/mysql/bin/"
mysql_sock="/opt/mysql_${port}/mysql.sock"
mysqld_pid_file_path=/opt/mysql_${port}/mysqld_${port}.pid

start(){
if [ ! -e "$mysql_sock" ];then
    printf "Starting MySQL...\n"
    /bin/sh ${Cmdpath}/mysqld_safe --defaults-file=/etc/mysql_${port}.cnf --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null &
    sleep 3
else
    printf "MySQL is running...\n"
    exit 1
fi
}


stop(){
    if [ ! -e "$mysql_sock" ];then
        printf "MySQL is stopped...\n"
        exit 1
    else
        printf "Stoping MySQL...\n"
        mysqld_pid=`cat "$mysqld_pid_file_path"`
    if (kill -0 $mysqld_pid 2>/dev/null)
        then
        kill $mysqld_pid
        sleep 2
        fi
    fi
}



restart(){
    printf "Restarting MySQL...\n"
    stop
    sleep 2
    start
}



case "$1" in
start)
    start
;;
stop)
    stop
;;
restart)
    restart
;;
*)
    printf "Usage: /data/${port}/mysql{start|stop|restart}\n"
esac
EOF
posted @ 2023-11-16 15:43  村尚chun叔  阅读(22)  评论(0编辑  收藏  举报