CentOS7部署 JDK+Tomcat+MySQL

一、安装系统CentOS7(选择默认的最小安装)

clip_image0012dc865c1199440f2a3d3a790fad1e76e.png

二、下载 jdk、tomcat、mysql

clip_image00283c466d529314716a6cc0f21c676c1e3.png

三、开始安装

1、安装JDK

  1. 通过终端在/usr/local目录下新建java文件夹,命令行:
sudo mkdir /usr/local/java
  1. 然后将下载到压缩包拷贝到java文件夹中,命令行:
cp jdk-7u79-linux-x64.tar.gz /usr/local/java
  1. 然后进入java目录,解压压缩包,命令行:
cd /usr/local/java
sudo tar xvf jdk-7u79-linux-x64.tar.gz
  1. 然后可以把压缩包删除,命令行:
sudo rm jdk-7u79-linux-x64.tar.gz
  1. 移动解压好的/usr/local/java/jdk1.7.0_79文件夹中得所有文件到java文件夹中,删除jdk1.7.0_79文件夹
mv /usr/local/java/jdk1.7.0_79/* /usr/local/java/
rm -rf /usr/local/java/jdk1.7.0_79

2、配置JDK环境变量

  1. 打开/etc/bashrc文件,在文件的末尾添加设置
vi /etc/bashrc
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export TOMCAT_HOME=/usr/local/tomcat
export MYSQL_HOME=/usr/local/mysql
export PATH=$JAVA_HOME/bin:$TOMCAT_HOME/bin:$MYSQL_HOME/bin:$PATH

  1. 使环境变量立即生效
source ~/.bashrc
  1. 查看是否配置成功
java -version

3、安装Tomcat

  1. 通过终端在/usr/local目录下新建tomcat文件夹,命令行:
sudo mkdir /usr/local/tomcat
  1. 然后将下载到压缩包拷贝到tomcat文件夹中,命令行:
cp apache-tomcat-7.0.70.tar.gz /usr/local/tomcat
  1. 然后进入tomcat目录,解压压缩包,命令行:
cd /usr/local/tomcat
sudo tar zxvf apache-tomcat-7.0.70.tar.gz
  1. 然后可以把压缩包删除,命令行:
rm -rf /usr/local/tomcat/apache-tomcat-7.0.70

4、添加Tomcat到防火墙

  1. 在/etc/firewalld/services/目录下新建一个名为tomcat.xml的文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>Tomcat Webserver</short>
  <description>HTTPS is a modified HTTP used to serve Web pages when security is important. Examples are sites that require logins like stores or web mail. This option is not required for viewing pages locally or developing Web pages. You need the httpd package installed for this option to be useful.</description>
  <port protocol="tcp" port="8080"/>
</service>
  1. 然后把此服务加入防火墙规则中
firewall-cmd --reload
firewall-cmd --add-service=tomcat
firewall-cmd --permanent --add-service=tomcat
  1. 启动Tomcat:运行命令startup.sh (已经配置tomcat/bin到环境变量中了)

5、安装MySQL

  1. 通过终端在/usr/local目录下新建mysql文件夹,命令行:
sudo mkdir /usr/local/mysql
  1. 然后将下载到压缩包拷贝到mysql文件夹中,命令行:
cp mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz /usr/local/mysql
  1. 然后进入mysql目录,解压压缩包,命令行:
cd /usr/local/mysql
sudo tar xvf mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
  1. 然后可以把压缩包删除,命令行:
sudo rm mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
  1. 移动解压好的mysql-5.7.15-linux-glibc2.5-x86_64文件夹中的所有文件到mysql文件夹中
mv /usr/local/mysql/mysql-5.7.15-linux-glibc2.5-x86_64/* /usr/local/mysql/
  1. 删除mysql-5.7.15-linux-glibc2.5-x86_64文件夹
rm -rf /usr/local/mysql/mysql-5.7.15-linux-glibc2.5-x86_64
  1. 创建data、temp目录
mkdir /usr/local/mysql/data
mkdir /usr/local/mysql/tmp

6、添加MySQL用户,配置文件及访问权限

  1. 添加mysql组和mysql用户,用于设置mysql安装目录文件所有者和所属组。
 groupadd mysql
 useradd -r -g mysql mysql

#  useradd -r参数表示mysql用户是系统用户,不可用于登录系统。
#  useradd -g参数表示把mysql用户添加到mysql用户组中。

  1. cd mysql 进入mysql文件夹,也就是mysql所在的目录,更改mysql目录所属的组和用户。更改权限
chown -R mysql:mysql /usr/local/mysql/
chmod 777 /usr/local/mysql/
  1. 拷贝mysql的配置文件
sudo cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
  1. 修改配置my.cnf文件(后续根据需要设置别的项)
[mysqld]
#默认mysql用户
user=mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
basedir = /usr/local/mysql/
datadir = /usr/local/mysql/data/
port = 3306
socket = /usr/local/mysql/tmp/mysql.sock

#解决密码失效问题
default_password_lifetime=0 

#不区分表名大小写
lower_case_table_names=1
[mysql]
socket = /usr/local/mysql/tmp/mysql.sock
[mysqladmin]
socket = /usr/local/mysql/tmp/mysql.sock
[mysqldump]
socket = /usr/local/mysql/tmp/mysql.sock

[client]
socket = /usr/local/mysql/tmp/mysql.sock
[mysqld_safe]
socket = /usr/local/mysql/tmp/mysql.sock

7、安装MySQL

  1. 初始化
mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
  1. 可能遇到的问题
  • 报错:缺少依赖包,
    mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
  • 解决方案
# 安装liebao
yum install libaio
# 然后重新执行命令
mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
  1. 成功,记录一下初始密码
    5602954ecef44c70990695ac9da783a2.png
  2. 将mysql/目录下除了data/目录的所有文件,改回root用户所有,mysql用户只需作为mysql/data/目录下所有文件的所有者。
chown -R root /usr/local/mysql
chown -R mysql /usr/local/mysql/data
  1. 注意事项
  • 注意,tmp目录不存在,请创建之。否则会出错 创建后要赋予mysql权限,chown -R mysql:mysql tmp
  • 如果mysql.sock指定到/tmp以外的目录,需要在my.cnf中添加[client]并且指定socket位置,
    否则登录mysql时会报错:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
    应该是,默认会找tmp目录下的sock文件

8、添加MySQL到防火墙

  1. 在/etc/firewalld/services/目录下新建一个名为mysql.xml的文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>MySQL Webserver</short>
  <description>HTTPS is a modified HTTP used to serve Web pages when security is important. Examples are sites that require logins like stores or web mail. This option is not required for viewing pages locally or developing Web pages. You need the httpd package installed for this option to be useful.</description>
  <port protocol="tcp" port="3306"/>
</service>
  1. 然后把此服务加入防火墙规则中
 firewall-cmd --reload
 firewall-cmd --add-service=mysql
 firewall-cmd --permanent --add-service=mysql

9、 部署系统到Tomcat

  1. 修改tomcat/conf/service.xml文件
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               executor="tomcatThreadPool"
               URIEncoding="UTF-8"
           	   maxPostSize="-1"
               maxParameterCount="-1"
               redirectPort="8443" />
  1. 设置jvm运行内存
# 修改TOMCAT_HOME/bin/catalina.sh 
位置:在# OS specific support.  $var _must_ be set to either true or false.上面:
JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:PermSize=256M -XX:MaxNewSize=256m -XX:MaxPermSize=256m -Djava.awt.headless=true"

37cfad47633649a6bf07ebe36e056592.png
3. 重启Tomcat即可

10、部署完毕!!!

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×