一键安装脚本JDK、mysql、redis
自动安装脚本
一键安装JDK
一键安装jdk
#!/bin/bash
#安装jdk
echo "开始安装jdk1.8.0_45"
#定义变量
INSTALLHOME=`pwd`
#下载jdk
wget http://software.moedu.net/download/jdk/jdk-8u45-linux-x64.tar.gz -P $INSTALLHOME
cd $INSTALLHOME && tar -zxvf jdk-8u45-linux-x64.tar.gz
#cd $INSTALLHOME/jdk* && JDKNAME=`pwd | awk -F '/' '{print $NF}'`
echo "#This is add env for use jdk1.8.0_45#" >> /etc/profile
echo "export JAVA_HOME=${INSTALLHOME}/jdk1.8.0_45" >> /etc/profile
echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> /etc/profile
echo "export CLASSPATH=.:\$JAVA_HOME/lib/tools.jar:\$JAVA_HOME/lib/dt.jar" >> /etc/profile
echo "export JAVA_HOME PATH CLASSPATH" >> /etc/profile
#让环境变量更新生效
source /etc/profile
bash
java -version
if [ $? -eq 0 ]; then
echo 'jdk1.8.0_45配置完成'
else
echo '安装失败,请重新尝试或手动安装'
fi
mysql安装
mysql_install.sh
#!/bin/bash
#Install MySQL
SOFTDIR="/opt/percona_soft"
MYSQLDIR="/opt/mysql"
DATADIR="/opt/mysql/data"
PASSWD="123456"
export LANG=zh_CN.UTF-8
echo -e "\n"
echo "############################# MySQL installing......########################"
#服务器初始化
if [ -s /etc/selinux/config ]; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
fi
setenforce 0
systemctl stop firewalld;
systemctl disable firewalld;
cat >>/etc/security/limits.conf<<EOF
* soft nproc unlimited
* hard nproc unlimited
* soft nofile 655350
* hard nofile 655350
EOF
if [ ! -d $SOFTDIR ];then
mkdir -p $SOFTDIR
fi
yum -y remove mariadb*
yum -y install wget perl perl-devel libaio libaio-devel perl-Time-HiRes net-tools
wget http://software.moedu.net/download/mysql/Percona-Server-5.7.29-32-r56bce88-el7-x86_64-bundle.tar -P $SOFTDIR
cd $SOFTDIR && tar -xvf Percona-Server-5.7.29-32-r56bce88-el7-x86_64-bundle.tar -C $SOFTDIR
rpm -ivh Percona-Server-shared-compat-57-5.7.29-32.1.el7.x86_64.rpm Percona-Server-shared-57-5.7.29-32.1.el7.x86_64.rpm Percona-Server-client-57-5.7.29-32.1.el7.x86_64.rpm Percona-Server-server-57-5.7.29-32.1.el7.x86_64.rpm
#edit mysqld.cnf
mv /etc/percona-server.conf.d/mysqld.cnf /etc/percona-server.conf.d/mysqld.cnf.bak
cat >>/etc/percona-server.conf.d/mysqld.cnf<<EOF
[mysqld]
port=3306
character-set-server=utf8mb4
datadir=${DATADIR}
socket=${MYSQLDIR}/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=${MYSQLDIR}/mysqld.log
pid-file=${MYSQLDIR}/mysqld.pid
#GENERAL
default-storage-engine=INNODB
#INNODB
innodb_buffer_pool_size=4G
innodb_file_per_table=1
#innodb_data_file_path= ibdata1:256M:autoextend
innodb_log_file_size=256M
innodb_log_files_in_group=2
#innodb_open_fiels=2048
innodb_thread_concurrency = 0
#OTHER
skip-name-resolve
back_log = 6000
connect_timeout=43200
max_connections = 1000
max_connect_errors = 600
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 512M
lower_case_table_names = 1
binlog_cache_size = 1M
tmp_table_size = 2G
max_heap_table_size = 2G
join_buffer_size=8M
sort_buffer_size=8M
read_buffer_size=8M
read_rnd_buffer_size=8M
secure_file_priv=
thread_cache_size = 64
query_cache_type=0
query_cache_size=0
key_buffer_size=512M
table_definition_cache=512
explicit_defaults_for_timestamp = true
log-bin-trust-function-creators=1
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
event_scheduler = 1
default-time_zone = '+8:00'
server_id=1000
log-bin=mysql-bin
binlog-format=ROW
slow_query_log=ON
slow_query_log_file=${DATADIR}/slow.log
long_query_time = 2
[mysql]
socket=${MYSQLDIR}/mysql.sock
default-character-set=utf8mb4
EOF
if [ ! -d $DATADIR ];then
mkdir -p $DATADIR
fi
mysqld --initialize
chown -R mysql:mysql $MYSQLDIR
systemctl start mysqld
systemctl enable mysqld
TMP_PASSWD=`cat $MYSQLDIR/mysqld.log | grep "A temporary password" | awk -F " " '{print$11}'`
if [ `systemctl status mysqld|grep running|wc -l` -eq 1 ];then
mysql -u root -p"$TMP_PASSWD" --connect-expired-password -e "SET PASSWORD = PASSWORD('$PASSWD');"
mysql -u root -p"$PASSWD" --connect-expired-password -e "update mysql.user set host='%' where user ='root';"
mysql -u root -p"$PASSWD" --connect-expired-password -e "flush privileges;"
mysql -u root -p"$PASSWD" --connect-expired-password -e "source $SOFTDIR/nacos-mysql.sql"
echo "############################# MySQL 5.7.29 install completed #############################"
echo -e "\n"
fi
❝编辑mysql_install.sh文件,改成需要的安装路径、mysql密码
❞
vi mysql_install.sh
SOFTDIR="/opt/percona_soft"
MYSQLDIR="/opt/mysql"
DATADIR="/opt/mysql/data"
PASSWD="123456"
nohup bash mysql_install.sh &
服务器做免密登录
ssh_key.sh
#!/bin/bash
#安装expect软件
yum -y install expect >/dev/null 2>&1 || exit 7
if [ ! -f /opt/user.txt ];
then
echo "/opt/user.txt not exits! exit..."
exit 1
fi
#生成公私钥
echo "== Create ssh private key ==";
/usr/bin/expect <<EOF
set timeout 1
spawn ssh-keygen;
expect "save" {send "\n"; exp_continue}
expect "(y/n)" {send "y\n"; exp_continue}
expect "empty" {send "\n"; exp_continue}
expect "again" {send "\n"; exp_continue}
EOF
#配置信任关系
while read IP USER PASS
do
echo "== Copy pub key to [${IP}], Wait 60s ==";
/usr/bin/expect <<EOF
set timeout 60
spawn ssh-copy-id ${USER}@${IP};
expect "(yes/no)" {send "yes\r"; exp_continue}
expect "password" {send "${PASS}\r"; exp_continue}
EOF
done < /opt/user.txt
#删除临时文件
rm -fv /opt/user.txt;
❝把需要远程登录的目标服务器信息写在如下文件中,执行脚本即可。如果几台服务器需要相互授信,填写所有服务器信息。在每台服务器上执行脚本。
❞
vi /opt/user.txt
#格式:服务器IP地址 用户名 密码
192.168.228.140 root 123456
192.168.228.141 root 123456
192.168.228.142 root 123456
bash ssh_key.sh
redis集群部署
一台服务器六个节点的redis安装
redis_install.sh
#!/bin/bash
IPADDR1="192.168.228.141"
IPADDR2=""
PORT1="7000"
#PORTSTART="7000"
#NODES="6"
#PORTEND="$[$PORTSTART+$NODES]"
PORT2="7001"
PORT3="7002"
PORT4="7003"
PORT5="7004"
PORT6="7005"
PASSWORD="123456"
#下载redis安装包
yum -y install gcc gcc-c++ tcl zlib zlib-devel openssl-devel expect >/dev/null 2>&1
wget http://software.moedu.net/download/redis/redis-4.0.9.tar.gz -O /opt/redis-4.0.9.tar.gz
cd /opt && tar zxf redis-4.0.9.tar.gz
cd /opt/redis-4.0.9
MALLOC=libc
make && make install
#安装ruby
yum remove ruby
wget http://software.moedu.net/download/redis/ruby-2.5.0.tar.gz -O /opt/ruby-2.5.0.tar.gz
cd /opt && tar -zxf ruby-2.5.0.tar.gz
cd /opt/ruby-2.5.0
./configure
make && make install
#安装ruby执行redis的相关依赖
wget http://software.moedu.net/download/redis/redis-4.2.1.gem -O /opt/redis-4.2.1.gem
#gem sources --add http://mirrors.aliyun.com/rubygems/ --remove https://rubygems.org/
gem install /opt/redis-4.2.1.gem
#安装redis
mkdir -p /opt/redis_cluster/{$PORT1,$PORT2,$PORT3,$PORT4,$PORT5,$PORT6}
wget http://software.moedu.net/download/redis/redis_example.conf -O /opt/redis_example.conf
for i in $PORT1 $PORT2 $PORT3 $PORT4 $PORT5 $PORT6
do
cp /opt/redis_example.conf /opt/redis_cluster/$i/redis.conf
sed -i 's/aaaaaaip/'$IPADDR1'/' /opt/redis_cluster/$i/redis.conf
sed -i 's/bbbbbbport/'$i'/' /opt/redis_cluster/$i/redis.conf
sed -i 's/ccccccpassword/'$PASSWORD'/' /opt/redis_cluster/$i/redis.conf
redis-server /opt/redis_cluster/$i/redis.conf
echo "redis-server /opt/redis_cluster/$i/redis.conf" >> /opt/redis_cluster/startup.sh
done
chmod u+x /opt/redis_cluster/startup.sh
#配置集群
sed -i 's/password: nil/password: "'$PASSWORD'"/' /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.2.1/lib/redis/client.rb
/usr/bin/expect <<EOF
spawn /opt/redis-4.0.9/src/redis-trib.rb create --replicas 1 $IPADDR1:$PORT1 $IPADDR1:$PORT2 $IPADDR1:$PORT3 $IPADDR1:$PORT4 $IPADDR1:$PORT5 $IPADDR1:$PORT6
expect "type 'yes' to accept" {send "yes\r";exp_continue}
EOF
#优化
cat >>/etc/rc.d/rc.local<<EOF
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
EOF
chmod +x /etc/rc.d/rc.local
echo "net.core.somaxconn=2048" >> /etc/sysctl.conf
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
sysctl -p
#设置关闭脚本
echo "ps aux|grep redis |grep -v 'grep' |awk '{print\$2}'|xargs kill -9" >> /opt/redis_cluster/shutdown.sh
chmod u+x /opt/redis_cluster/shutdown.sh
执行:
#redis_install.sh文件修改IPADDR1为你要部署的服务器ip,PASSWORD为redis密码
vi redis_install.sh
IPADDR1="192.168.228.141"
PASSWORD="123456"
nohup bash redis_install.sh &
两台服务器六个节点的redis安装
redis_cluster.sh
#!/bin/bash
source /opt/nodes.sh
export LANG=zh_CN.UTF-8
wget http://software.moedu.net/download/ssh_key/ssh_key.sh -O /opt/ssh_key.sh
bash /opt/ssh_key.sh
wget http://software.moedu.net/download/redis/install.sh -O /opt/install.sh
bash /opt/install.sh $IPADDR1
scp /opt/install.sh root@$IPADDR2:/opt
ssh root@$IPADDR2 -C "bash /opt/install.sh $IPADDR2"
#配置集群
/usr/bin/expect <<EOF
spawn /opt/redis-4.0.9/src/redis-trib.rb create --replicas 1 $IPADDR1:$PORT1 $IPADDR1:$PORT2 $IPADDR1:$PORT3 $IPADDR2:$PORT1 $IPADDR2:$PORT2 $IPADDR2:$PORT3
expect "type 'yes' to accept" {send "yes\r";exp_continue}
EOF
执行:
#在两台服务器上编辑如下文件,IPADDR1、IPADDR2为两台服务器的ip,PASSWORD为redis密码,PORT*为两台服务器使用的端口号。
vi /opt/nodes.sh
#!/bin/bash
IPADDR1="192.168.228.141"
IPADDR2="192.168.228.131"
PORT1="7000"
PORT2="7001"
PORT3="7002"
PASSWORD="123456"
#redis两台服务器做免密登录,在第一台服务器编辑如下文件
vi /opt/user.txt
#格式:服务器IP地址 用户名 密码
192.168.228.141 root 123456
192.168.228.131 root 123456
nohup bash redis_cluster.sh &