代码改变世界

Mysql 一键安装脚本

2021-10-06 10:51  那个,我  阅读(351)  评论(0编辑  收藏  举报

  • 测试环境搭建用的脚本,CentOS6上没做测试,只在CentOS7上跑过
  • 因为已经足够使用,就没有再完善。

1.单实例脚本

用法

sh autoInstallMysql.sh mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz

脚本

autoInstallMysql.sh

#!/bin/bash
# autoInstallMysql.sh
# Author 那个,我
# base目录: /usr/local/mysql
# data目录: /data/mysql/3306/data
# 可用系统: CentOS6&7

# 改动参数路径可能导致脚本失效
mycnf=/etc/my.cnf
mysock=/tmp/mysql.sock

get_soft(){
if [ $# -lt 1 ];then
echo "请传入Mysql*.tar.gz包"
exit 1
fi
}

status(){
if [ $? == 0 ];then
	log 0 $2
else 
	log 1 $2
fi
}

log() {
    if [ $1 -eq 0 ]; then
        echo -e "\033[34;2m $2 \033[0m"
    elif [ $1 -eq 1 ]; then
        echo -e "\033[31;5m $2 \033[0m"
        exit 1
    fi
}

get_soft $1
mysqlsoft=$1

log 0 "-创建用户"
if [ ! $(id -u mysql) ];then
    useradd -s /sbin/nologin mysql
fi >/dev/null 2>&1

log 0 "-创建路径"
if [ ! -d "/data/mysql/3306/{data,binlog}" ]; then
    mkdir -p /data/mysql/3306/{data,binlog}
    chown -R mysql. /data/mysql
fi

log 0 "-解压缩文件"
basedir=/usr/local/${mysqlsoft%.tar*}
if [ ! -d $basedir ];then
    tar -xf $mysqlsoft -C /usr/local
fi

log 0 "-创建软连接"
cd /usr/local
if [ -L mysql ];
	then rm -rf mysql;
elif [ -d mysql ];
	then mv mysql mysql.`date +%Y%m%d`;
fi

ln -s $basedir /usr/local/mysql

# 参数文件配置
rpm -q mariadb-libs >/dev/null 2>&1
if [ $? == 0 ];then
    rpm -e --nodeps mariadb-libs
fi
cat > $mycnf <<EOF
[mysqld]
# server
explicit_defaults_for_timestamp=1
basedir=/usr/local/mysql
datadir=/data/mysql/3306/data
socket=$mysock
user=mysql
skip_name_resolve=on
log-error=/data/mysql/3306/mysql.error

# binlog
log-bin=/data/mysql/3306/binlog/mysql-bin
binlog_format=ROW
log-slave-updates
expire_logs_days=10
sync_binlog=1

#relay-log
relay_log_info_repository = TABLE
relay_log=/data/mysql/3306/binlog/mysql-relay
relay_log_recovery = 1

# InnoDB
default-storage-engine=InnoDB
character-set-server=utf8mb4
lower_case_table_names=1
transaction-isolation=READ-COMMITTED
innodb_flush_log_at_trx_commit=1
innodb_buffer_pool_size=1G

# Replication
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository = TABLE
slave_parallel_type = LOGICAL_CLOCK
slave_parallel_workers=4
slave_preserve_commit_order = 1

# 半同步复制
plugin-load="semisync_master.so;semisync_slave.so"
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_slave_enabled = 1
rpl_semi_sync_master_wait_no_slave=ON
rpl_semi_sync_master_timeout=3
rpl_semi_sync_master_wait_point=AFTER_SYNC
rpl_semi_sync_master_wait_for_slave_count=1
EOF

# PATH变量
echo "export PATH=/usr/local/mysql/bin:\$PATH" >>/etc/profile
source /etc/profile

log 0 "-初始化"
mysqld --initialize-insecure

log 0 "-启动"
echo yes | cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
release=`cat /etc/redhat-release|awk '{print $4}'`
case $release in 
6*)
    chkconfig mysqld on
    service mysqld status
    chkconfig --list mysqld
    ;;
7*)
    chkconfig --add mysqld
    chkconfig mysqld on
    systemctl start mysqld
    ;;
esac
sleep 5

log 0 "-创建DBA用户(root@'%') ,Pass(root_123)"
log 0 "-按回车继续..."
mysqladmin -u root -p password 'root_123'
# 创建远程管理用户
mysql -u root -p'root_123' -e "
create user root identified by 'root_123';
grant all on *.* to root with grant option;
"
log 0 "-安装完成,请退出并重新登录"
exit

2.多实例脚本

用法

sh autoInstallMysql_multi.sh mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz 3306
sh autoInstallMysql_multi.sh mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz 3307

脚本

myautoInstallmulti.sh

#!/bin/bash
# autoInstallMysql_multi.sh
# Author 那个,我
# base目录: /usr/local/mysql
# 可用系统: CentOS6&7

# 改动参数路径可能导致脚本失效

if [ ! $2 ];then
	export myport=3306
else
	export myport=$2
fi

myhome=/data/mysql/$myport
mycnf=$myhome/my.cnf
mytmp=$myhome/tmp
mysock=$mytmp/mysql.sock

get_soft(){
if [ $# -lt 1 ];then
echo "请传入Mysql*.tar.gz包"
exit 1
fi
}

status(){
if [ $? == 0 ];then
	log 0 $2
else 
	log 1 $2
fi
}

log() {
    if [ $1 -eq 0 ]; then
        echo -e "\033[34;2m $2 \033[0m"
    elif [ $1 -eq 1 ]; then
        echo -e "\033[31;5m $2 \033[0m"
        exit 1
    fi
}

get_soft $1
mysqlsoft=$1

log 0 "-创建用户"
if [ ! $(id -u mysql) ];then
    useradd -s /sbin/nologin mysql
fi >/dev/null 2>&1

log 0 "-创建路径"
if [ ! -d "$myhome/{data,binlog,tmp}" ]; then
    mkdir -p $myhome/{data,binlog,tmp}
    chown -R mysql. $myhome
fi

log 0 "-解压缩文件"
basedir=/usr/local/${mysqlsoft%.tar*}
if [ ! -d $basedir ];then
    tar -xf $mysqlsoft -C /usr/local
fi

log 0 "-创建软连接"
cd /usr/local
if [ -L mysql ];
	then rm -rf mysql;
elif [ -d mysql ];
	then mv mysql mysql.`date +%Y%m%d`;
fi

ln -s $basedir /usr/local/mysql

# 参数文件配置
cat > $mycnf <<EOF
[mysqld]
# server
explicit_defaults_for_timestamp=1
basedir=/usr/local/mysql
datadir=$myhome/data
port=$myport
socket=$mysock
user=mysql
skip_name_resolve=on
log-error=$mytmp/mysql.error

# binlog
log-bin=$myhome/binlog/mysql-bin
binlog_format=ROW
log-slave-updates
expire_logs_days=10
sync_binlog=1

# InnoDB
default-storage-engine=InnoDB
character-set-server=utf8mb4
lower_case_table_names=1
transaction-isolation=READ-COMMITTED
innodb_flush_log_at_trx_commit=1
innodb_buffer_pool_size=1G

# Replication
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
relay_log=$myhome/binlog/mysql-relay
relay_log_recovery = 1
slave_parallel_type = LOGICAL_CLOCK
slave_parallel_workers=4
slave_preserve_commit_order = 1

# 半同步复制
plugin-load="semisync_master.so;semisync_slave.so"
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_slave_enabled = 1
rpl_semi_sync_master_wait_no_slave=ON
rpl_semi_sync_master_timeout=3
rpl_semi_sync_master_wait_point=AFTER_SYNC
rpl_semi_sync_master_wait_for_slave_count=1
EOF

# PATH变量
echo "export PATH=/usr/local/mysql/bin:\$PATH" >>/etc/profile
source /etc/profile

log 0 "-初始化"
mysqld --defaults-file=$mycnf --initialize-insecure

log 0 "-启动"
release=`cat /etc/redhat-release|awk '{print $4}'`
case $release in 
6*)
mysqld --defaults-file=$mycnf &
;;
7*)
cat > /etc/systemd/system/mysqld$myport.service <<EOF
#!/bin/bash
# systemd for Mysql
[Unit]
Description=Mysql Server
After=syslog.target
After=network.target
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/8.0/en/using-systemd.html

[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=$mycnf
LimitNOFILE=65535
Restart=on-failure
RestartSec=3
RestartPreventExitStatus=1
PrivateTmp=false

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl restart mysqld$myport
systemctl enable mysqld$myport
;;
esac
sleep 5

log 0 "-创建DBA用户(root@'%') ,Pass(root_123)"
log 0 "-按回车继续..."
mysqladmin -S $mysock -u root -p password 'root_123'
# 创建远程管理用户
mysql -S $mysock -u root -p'root_123' -e "
create user root identified by 'root_123';
grant all on *.* to root with grant option;
"
log 0 "-安装完成,请退出并重新登录"
log 0 "-登录方式: ( mysql -S $mysock -uroot -proot_123 )"
exit