多实例部署mysql,适用于新服务器部署

风流不在谈锋胜,袖手无言味最长。这篇文章主要讲述多实例部署mysql,适用于新服务器部署相关的知识,希望能为你提供帮助。
1、创建一个目录:
mkdir mysql
2、压缩包放在这个目录里:
mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
3、在这个目录里编辑脚本文件 vi mysql.sh
#!/bin/bash


if [ $UID -ne 0 ]; then
echo "Please execute this script uses root user."
exit 250
fi


install_dir=/usr/local
data_dir=/opt/data


read -p "请输入要创建的实例的个数: " instance_count
if [ -z $instance_count ]; then
instance_count=1
fi
echo $instance_count | grep -E "^[0-9]+$" & > /dev/null
if [ $? -ne 0 ]; then
instance_count=1
fi


read -p "请输入您要使用的数据库password: " password
if [ -z $password ]; then
password=1
fi


yum -y install ncurses-compat-libs perl


id mysql & > /dev/null
if [ $? -ne 0 ]; then
useradd -r -M -s /sbin/nologin mysql
fi


if [ ! -d $install_dir/mysql-5.7.35-linux-glibc2.12-x86_64 ] & & [ ! -d $install_dir/mysql ]; then
tar xf /mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz -C $install_dir
【多实例部署mysql,适用于新服务器部署】ln -s $install_dir/mysql-5.7.35-linux-glibc2.12-x86_64 $install_dir/mysql
fi


chown -R mysql.mysql $install_dir/mysql*


echo "export PATH=$install_dir/mysql/bin:\\$PATH" > /etc/profile.d/mysql.sh




function single(){
if [ ! -d $data_dir ]; then
mkdir -p $data_dir
fi
chown -R mysql.mysql $data_dir
content=$(ls $data_dir | wc -l)
if [ $content -eq 0 ]; then
$install_dir/mysql/bin/mysqld --initialize-insecure --user mysql --datadir=$data_dir
fi
cat > /etc/my.cnf < < EOF
[mysqld]
basedir = $install_dir/mysql
datadir = $data_dir
socket = /tmp/mysql.sock
port = 3306
pid-file = $data_dir/mysql.pid
user = mysql
skip-name-resolve
EOF
sed -ri "s#^(basedir=).*#\\1$install_dir/mysql#g" $install_dir/mysql/support-files/mysql.server
sed -ri "s#^(datadir=).*#\\1$data_dir#g" $install_dir/mysql/support-files/mysql.server
cat > /usr/lib/systemd/system/mysqld.service < < EOF
[Unit]
Description=Mysql server daemon
After=network.target


[Service]
Type=forking
ExecStart=$install_dir/mysql/support-files/mysql.server start
ExecStop=$install_dir/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID


[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now mysqld
sleep 3
$install_dir/mysql/bin/mysql -uroot -e "set password = password($password)"
echo -e "你的passwd设置成功,passwd是 \\033[1; 32; 40m $password \\033[0m"
}


function multi(){
port=3305
export PATH=$install_dir/mysql/bin:$PATH
cat > /etc/my.cnf < < EOF
[mysqld_multi]
mysqld = $install_dir/mysql/bin/mysqld_safe
mysqladmin = $install_dir/mysql/bin/mysqladmin


EOF
sed -i s!^bindir=/usr/local/mysql/bin!bindir=/usr/local/mysql/bin\\nexport PATH=\\$bindir:\\$PATH!g $install_dir/mysql/support-files/mysqld_multi.server


for i in $(seq $instance_count); do
let port++
if [ ! -d $data_dir/port ]; then
mkdir -p $data_dir/$port
chown -R mysql.mysql $data_dir
fi
content=$(ls $data_dir/$port | wc -l)
if [ $content -eq 0 ]; then
$install_dir/mysql/bin/mysqld --initialize-insecure --user mysql --datadir=$data_dir/$port
fi
cat > > /etc/my.cnf < < EOF
[mysqld$port]
datadir = $data_dir/$port
port = $port
socket = /tmp/mysql$port.sock
pid-file = $data_dir/$port/mysql.pid
log-error=/var/log/$port.log


EOF
$install_dir/mysql/bin/mysqld_multi start $port
sleep 3
$install_dir/mysql/bin/mysql -uroot -P$port -h127.0.0.1 -e "set password = password($password)"
echo -e "你的passwd设置成功,passwd是 \\033[1; 32; 40m $password \\033[0m"
$install_dir/mysql/bin/mysqld_multi stop $port
done


cat > /usr/lib/systemd/system/mysqld.service < < EOF
[Unit]
Description=mysql server daemon
After=network.targe


[Service]
Type=forking
ExecStart=$install_dir/mysql/support-files/mysqld_multi.server start
ExecStop=$install_dir/mysql/support-files/mysqld_multi.server stop
ExecReload=/bin/kill -HUP


[Install]
WantedBy=multi-user.target
EOF




systemctl daemon-reload
systemctl enable --now mysqld
}


if [ $instance_count -eq 1 ]; then
single
else
multi
fi
4、执行该脚本:
sh mysql.sh
5、查看脚本的端口:
netstat -anptul
6、重启服务器,登录:
mysql -uroot -p123456 -P3306 -h127.0.0.1





    推荐阅读