#夏日挑战赛#多实例部署mysql,适用于新机部署 原创

发布于 2022-7-6 09:00
浏览
1收藏

【本文正在参加星光计划计划3.0–夏日挑战赛】
活动链接:https://ost.51cto.com/posts/13641

这样可以一机多数据库并发做主从

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
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

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
标签
9
收藏 1
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐