CentOS系统一键部署jdk,maven,tomcat,mysql

CentOS系统一键部署jdk,maven,tomcat,mysql

#!/bin/bash

####使用方法###############
# chmod a+x JdTomK-Auto.sh
# source JdTomK-Auto.sh
###########################


echo "1.开始安装Java环境:"
sleep 3

tar -zxv -f jdk-8u231-linux-x64.tar.gz -C /usr/local

cat >> /etc/profile.d/java.sh << "EOF"
#!/bin/bash

export JAVA_HOME=/usr/local/jdk1.8.0_231
export JRE_HOME=${JAVA_HOME}/jre  
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib  
export PATH=${JAVA_HOME}/bin:$PATH
EOF

source /etc/profile.d/java.sh

echo "验证java环境"
echo "JAVA_HOME ====" ${JAVA_HOME}
java -version

echo "2.开始安装Maven环境:"
sleep 3

tar -zxv -f apache-maven-3.6.3-bin.tar.gz -C /usr/local

cat >> /etc/profile.d/maven.sh << "EOF"
#!/bin/bash 

export MAVEN_HOME=/usr/local/apache-maven-3.6.3
export PATH=${MAVEN_HOME}/bin:${PATH}
EOF

source /etc/profile.d/maven.sh

echo "使用阿里云源"
mkdir -p /data/maven/repository

# \cp表示不使用别名cp == cp -i
\cp -rf settings.xml /usr/local/apache-maven-3.6.3/conf

echo "验证maven环境"
echo "MAVEN_HOME ====" ${MAVEN_HOME}
mvn -version


echo "3.开始安装Tomcat:"
sleep 3

cat >> /etc/sysctl.conf << "EOF"
fs.file-max = 262144
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.rmem_default = 1048576
net.core.wmem_default = 524288
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.netdev_max_backlog = 2500
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fin_timeout = 30
net.nf_conntrack_max = 655360
net.netfilter.nf_conntrack_tcp_timeout_established = 1200
EOF

sysctl -p

echo "*    soft    nofile     65535" >> /etc/security/limits.conf
echo "*    hard    nofile     65535" >> /etc/security/limits.conf

INFO=`getconf LONG_BIT`
if [ ${INFO} = "64" ] ; then
    echo "session required /lib64/security/pam_limits.so" >> /etc/pam.d/login
else
    echo "session required /lib/security/pam_limits.so" >> /etc/pam.d/login
fi

tar -zxv -f apache-tomcat-8.5.64.tar.gz -C /usr/local

#register server
cp -p /usr/local/apache-tomcat-8.5.64/bin/catalina.sh /etc/init.d/tomcat

sed -i ‘3a #chkconfig: 112 63 37‘ /etc/init.d/tomcat
sed -i ‘4a #description: tomcat server init script‘ /etc/init.d/tomcat
sed -i ‘5a #Source Function Library‘ /etc/init.d/tomcat
sed -i ‘6a . /etc/init.d/functions‘ /etc/init.d/tomcat
sed -i ‘7a JAVA_HOME=/usr/local/jdk1.8.0_231/‘ /etc/init.d/tomcat
sed -i ‘8a CATALINA_HOME=/usr/local/apache-tomcat-8.5.64‘ /etc/init.d/tomcat

#chmod
chmod 755 /etc/init.d/tomcat
#add to service
chkconfig --add tomcat
#auto run
chkconfig tomcat on
service tomcat start


echo "4.开始安装mysql:"

rpm -qa |grep mysql
rpm -qa |grep mariadb
yum remove mysql mysql-server mysql-libs mariadb mariadb-libs -y

groupadd mysql
useradd -g mysql mysql -s /sbin/nologin

tar -zxv -f mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz -C /data
cd /data/
mv mysql-5.7.32-linux-glibc2.12-x86_64 mysql
ln -sv /data/mysql /usr/local/mysql 

mkdir -pv /data/mysql/data  
mkdir -pv /data/mysql/dblog/iblog 
mkdir -pv /data/mysql/dblog/binlog 
mkdir -pv /data/mysql/dblog/relaylog
mkdir -pv /data/mysql/dblog/errlog
mkdir -pv /data/mysql/dblog/slowlog
mkdir -pv /data/mysql/run
mkdir -pv /data/mysql/tmp
mkdir -pv /data/mysql/lock/subsys
chown -R mysql:mysql /data/mysql
chmod -R 755 /data/mysql

cat >> /etc/my.cnf << "EOF"
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

#[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

[mysql]

# CLIENT #
port = 3306
socket = /data/mysql/run/mysql.sock
disable-auto-rehash
default-character-set=utf8mb4
prompt=(\\u@\\h) [\\d]>\\_

[mysqld]

# GENERAL #
server_id = 128
port = 3306
user = mysql
explicit_defaults_for_timestamp=true

default-storage-engine = InnoDB
character_set_server = utf8mb4
auto_increment_increment = 2
auto_increment_offset = 1
lower_case_table_names = 1
socket = /data/mysql/run/mysql.sock
pid_file = /data/mysql/run/mysqld.pid

# MyISAM #
key-buffer-size                = 32M
myisam-recover-options         = FORCE,BACKUP

# SAFETY #
max_allowed_packet = 134217728
max_connections = 2048
max_user_connections = 1024
open_files_limit = 65535
skip-name-resolve
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
sysdate-is-now                 = 1

# DATA STORAGE #
basedir = /data/mysql
datadir = /data/mysql/data/
tmpdir = /data/mysql/tmp

# BINARY LOGGING #
log-bin = /data/mysql/dblog/binlog/master-bin
log-bin-index = /data/mysql/dblog/binlog/master-bin.index
expire-logs-days = 15
sync-binlog = 1
binlog_format = ROW

#RELAY LOGGING
relay-log=/data/mysql/dblog/relaylog/master-relay-bin
relay-log-index=/data/mysql/dblog/relaylog/master-relay-bin.index
# sync_relay_log=1 最安全,崩溃后最多丢失一个事件,但是会引起同步延迟

# CACHES AND LIMITS #
tmp-table-size                 = 32M
max-heap-table-size            = 32M
query-cache-type               = 0
query-cache-size               = 0
thread-cache-size              = 50
open-files-limit               = 65535
table-definition-cache         = 1024
table-open-cache               = 2048

# INNODB #
innodb_log_group_home_dir = /data/mysql/dblog/iblog
innodb_data_home_dir = /data/mysql/dblog/iblog
innodb-flush-method = O_DIRECT
innodb-log-files-in-group      = 2
innodb-log-file-size           = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table          = 1
innodb-buffer-pool-size        = 6G

# LOGGING #
log_timestamps=system
general_log = off
log-error                      = /data/mysql/dblog/errlog/mysql-error.log
log-queries-not-using-indexes  = 1
slow-query-log                 = 1
slow-query-log-file            = /data/mysql/dblog/slowlog/mysql-slow.log
log_slave_updates=ON
EOF

cd /data/mysql/bin
./mysqld  --defaults-extra-file=/etc/my.cnf --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql --initialize-insecure
./mysql_ssl_rsa_setup --basedir=/data/mysql --datadir=/data/mysql/data

cat >> /etc/profile.d/mysql.sh << "EOF"
#!/bin/bash

PATH=/data/mysql/bin:$PATH
EOF

source /etc/profile.d/mysql.sh

cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld


sed -i ‘s/^mysqld_pid_file_path=.*$/mysqld_pid_file_path=\/data\/mysql\/run\/mysqld.pid/‘ /etc/init.d/mysqld
sed -i "s/^lockdir=.*$/lockdir=‘\/data\/mysql\/lock\/subsys‘/" /etc/init.d/mysqld

chkconfig --add mysqld
# /etc/init.d/mysqld start 
# /etc/init.d/mysqld stop
service mysqld start
# service mysqld restart

echo "等待MySQL启动"
sleep 5
service mysqld status

echo "默认没有密码"

if [ $? -eq 0 ]
then
    mysql -e "use mysql;DELETE FROM mysql.user WHERE User=‘‘ or host=‘localhost‘;"
else
    echo "failed"
fi

if [ $? -eq 0 ]
then
    mysql -e "GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘127.0.0.1‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;FLUSH PRIVILEGES;"
else
    echo "failed"
fi


echo "若需要创建数据库,用户等在这儿增加"

service mysqld restart

echo "mysql root用户密码:password"

echo "##########################################################################"
echo -e "\033[32m安装完成\n启动Tomcat: service tomcat start\n关闭Tomcat: service tomcat stop\nTomcat目录:/usr/local/apache-tomcat-8.5.64/webapps/ROOT\033[0m"
echo -e "\033[32m安装完成\n启动mysql: service mysqld start\n关闭Tomcat: service mysqld stop\n\033[0m"
echo -e "\033"
echo "##########################################################################"

CentOS系统一键部署jdk,maven,tomcat,mysql

上一篇:SqlServer技巧:数据展开与合并


下一篇:数据库基础