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

Jack丶韦
发布于 2022-7-6 09:00
6473浏览
1收藏

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

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

1、创建一个目录:

mkdir mysql
  • 1.

2、压缩包放在这个目录里:

mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
  • 1.

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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.

4、执行该脚本:

sh mysql.sh
  • 1.

5、查看脚本的端口:

netstat -anptul
  • 1.

6、重启服务器,登录:

mysql -uroot -p123456 -P3306 -h127.0.0.1
  • 1.

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
9
收藏 1
回复
举报
9
3
1
3条回复
按时间正序
/
按时间倒序
mb62ce7f01b465f
mb62ce7f01b465f

一次成功,没问题

回复
2022-7-13 16:16:43
Jack丶韦
Jack丶韦 回复了 mb62ce7f01b465f
一次成功,没问题

谢谢,这些我都是测试了很多次

回复
2022-7-28 18:07:38
Jack丶韦
Jack丶韦

谢谢支持

回复
2022-7-28 18:07:53


回复
    相关推荐
    恭喜您,今日已阅读两篇内容,特奖励+2声望, 快来领取吧。