Linux系统搭建Java环境【JDK、Tomcat、MySQL】一篇就够

  前言:所有项目在完成开发后都会部署上线的,一般都是用Linux系统作为服务器的,很少使用Windows Server(大多数项目的开发都是在Windows桌面系统完成的),一般有专门负责上线的人员。作为一个开发人员当然也能玩转Linux,毕竟Linux是很多IT人员的喜好。
  下面就使用纯净的Linux系统完成Java Web项目的部署。

  1. 安装一个VMware workstation
  2. 在虚拟机上面安装CentOS系统 (这里使用的CentOS6.7 64位)
  3. 安装Java开发环境JDK 1.8 , 注意系统的架构,华为云使用的是ARM
  4. Tomcat8.5 其他服务器也可以
  5. 数据库安装MySQL5.7 (Oracle、MS Server也可以)

在这里插入图片描述在这里插入图片描述

一、搭建Linux环境

  1.下载并安装一个VMware workstation, 这个是虚拟机的平台(自行度娘下载~),虚拟机是在后面要在里面搭建Linux系统。

  2.下载一个centos安装包,linux版本有多种,比如说redhat、ubuntu、deepin、BT3,个人比较习惯使用centos,这里下载一个
在这里插入图片描述
版本的。

  3.新建一个虚拟机,并把这个安装包导入进去,记住期间步骤有一步是需要建一个用户和设置密码,这里的用户是linux环境的普通账户,但是密码是根账户root和这个普通用户共用的密码(这节的教程也在我的博客上一节有详细的步骤)

  4.安装完成后,是图形化界面,一般我们要用到的是命令行界面,所以这里可以用快捷键 CTRL+ALT+F2,就可以切换到命令界面了。(命令行界面有几种方式可以进入,这边gqzdev推荐的是Xshell5、6 比较方便使用。需要的可以留言!)
在这里插入图片描述

XShell5 Xftp5工具(其他工具可以!)
在这里插入图片描述 XShell 5

  5.用命令ifconfig查看IP 地址,然后可以考虑用Xshell来连接虚拟机,这样操作命令会比较方便,不用频繁的切换出来或者切换出去。

  上面的安装及配置有问题的可以自行百度,网上资源很多。主要是搭建好Linux系统环境…

二、JDK安装

  1. 首先下载一个JDK版本,我这里下载的是jdk-8u221-linux-x64.gz 这个版本。(JDK1.8目前用得比较多!)
    也可以自行到JDK官网下载

  2. 用xftp上传到linux环境中去。上传的路径为: /usr/java【这个目标你想上传那个都想,/opt/…】在这里插入图片描述
    将刚才下载好的 压缩包解压,得到一个jdk1.8.0_221的文件夹。使用tar -xzvf命令解压
    然后用Xftp上传至linux环境中去。有两种方式
    第一种,通过功能栏红框内的“向右传输” 传过去
    第二种,直接拖动文件夹,拖过去。
    非常重要的一点,linux环境中的路径 是 /usr/java…在这里插入图片描述
    JDK传过去之后,现在就开始着手配置JDK环境了

  3. 配置JDK环境,需要给予这个文件夹最高的权限,为了后续的方便,这边直接赋予最高权限
    首先,通过XFTP打开Xshell(打开方式上一节有讲到)
    1)切换到"/“目录下 cd …/…
    2)切换到local路径下 cd /usr/java
    3)赋予JDK文件最高权限 chmod 777 -R jdk1.8.0_221

  4. 配置JDK环境变量:
    1)切换到"/“目录下 cd …/…
    2)切换到etc路径下 cd etc
    3)编辑profile文件 vim profile
    4)按下键盘的 insert 键,进入编辑模式
    5)配置JDK的环境变量,在profile中输入如下内容(空白位置填入即可,添加在最后):建议配置变量参考下面的配置方式
    特别说明:这个就是你的jdk的安装路径!!!不要弄错了!要以你自己的路径为准!】
    6) 按住键盘的ESC键,然后输入 :wq,就保存了你刚刚设置的环境变量
    7)让你刚刚设置的环境变量生效 source profile
    8)如何查看你的JDK是否配置完成呢?输入命令 java -version,看到版本信息就表示成功了!
    在etc下面的profile里面最后面配置export…

export JAVA_HOME=/usr/java/jdk1.8.0_221
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin

在这里插入图片描述在这里插入图片描述
通过java -version检查是否安装成功! 到此,JDK的配置就算完成了
在这里插入图片描述
注意:,如果是其他的架构,比如我最近在华为云服务器上面,安装jdk,就出现问题了。
其他架构,安装JDK

下面是在华为云服务器安装jdk时,遇到的问题!

也可以去如下地址自行选择对应版本:
https://adoptopenjdk.net/releases.html
官方下载地址:【推荐去这个下载,可选版本】
https://www.oracle.com/technetwork/java/javase/downloads/index.html

  1. 下载OpenJDK8U-jdk_aarch64_linux_hotspot_8u252b09.tar.gz
    在这里插入图片描述

  2. 解压 tar -zxvf

[root@huawei-gqzdev opt]# tar -zxvf OpenJDK8U-jdk_aarch64_linux_hotspot_8u252b09.tar.gz

处理解压后的文件,使用mv 移到/opt/jdk下面

  1. 配置环境环境
    在这里插入图片描述
  2. 测试
    java -version
    在这里插入图片描述

三、服务器安装

Tomcat8.5下载 https://tomcat.apache.org/download-80.cgi
在这里插入图片描述

下面来介绍下Tomcat的配置

  1. 首先下载 一个tomcat版本,我这里用的是apache-tomcat-6.0.37版本apache-tomcat-6.0.37.tar.gz是对应的压缩包。

  2. 可以用XFTP 直接把已经解压的apache-tomcat-6.0.37上传到 /usr/local路径,上传完毕,然后这里就需要对环境变量进行配置,然后后面的tomcat才会顺利启动

  3. 配置tomcat环境变量:
    1)切换到"/“目录下 cd …/…
    2)换到etc路径下 cd etc
    3)编辑profile文件 vi profile
    4)按下键盘的 i 键,进入编辑模式
    5)配置tomcat的环境变量,在profile中输入如下内容(配置JDK环境变量后面添加多这行即可):建议配置变量参考下面红色字体的配置方式
    export CATALINA_HOME=/usr/local/apache-tomcat-6.0.37 【特别注意:这里就是就是配置你的tomcat的安装路径!不要弄错了!】,如下图所示:
    7)编辑完毕后,按住键盘的ESC键,然后输入 :wq,就保存了你刚刚设置的tomcat环境变量
    8)让你刚刚设置的环境变量生效 source profile

注意:上面的JDK配置及tomcat路径的配置可能存在点问题 ,建议启用下面的配置方式

配置JDK环境变量及配置tomcat路径:
1)首先回到home路径,即顶级目录,命令: cd ~
2)然后打开并编辑环境变量的文件,输入命令: vi .bashrc
3)进入环境变量编辑环境,加入如下环境变量:

export JAVA_HOME=/usr/local/jdk1.6.0_45
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin 
export CATALINA_HOME=/usr/local/apache-tomcat-6.0.37

4)编辑完毕,就保存内容,命令: 先按ESC,然后输入 ”:wq“就可以保存了。
5)配置完毕后要让配置生效,用命令:source ~/.bashrc

4.可以启动一下tomcat,看是否配置成功了:
进入到tomat的 /bin/下面
./startup.sh,
出现如下截图,则表明启动成功。
在浏览器输入IP地址加端口号,如果看到tomcat 的首页,则表明成功了,如下所示:
在这里插入图片描述

四、数据库

安装开源数据库MySQL

  1. 选择yum安装
  2. 通过tar.gz压缩包进行安装

4.1 yum安装

  校验当系统是否安装mysql:

rpm -qa | grep mysql

  卸载mysql:

// 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
rpm -e --nodeps

  三行命令:

yum install mysql
yum install mysql-server
yum install mysql-devel

  也可以一行:

yum install -y mysql mysql-server mysql-devel

在这里插入图片描述

4.2 通过tar.gz压缩包进行安装

操作系统:Centos6.4 64位
工具:Xftp5、Xshell5
安装软件:mysql5.7
说明:使用官方编译好的二进制文件进行安装

在这里插入图片描述

  1. 移动安装文件到指定目录

在这里插入图片描述

  1. 检查是否安装Mysql
rpm -qa | grep mysql
  1. 解压
tar -xvzf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
#重命名mysql57
[root@VM_0_17_centos mysql]# mv mysql-5.7.27-linux-glibc2.12-x86_64 mysql57

在这里插入图片描述

进行到这里说明:
 将mysql的压缩包解压到/usr/mysql/目录下面,并且rm重命名为mysql57
  此时数据库的文件目录为 /usr/mysql/mysql57

  1. 设置mysql目录访问权限,用户组
#将mysql目录访问权限赋为myql用户
[root@VM_0_17_centos mysql57]# chown -R mysql /usr/mysql/mysql57

#改变mysql目录的用户组属于mysql组
[root@VM_0_17_centos mysql57]# chgrp -R mysql /usr/mysql/mysql57

#查看mysql目录下所有的目录及文件夹所属组合用户
[root@VM_0_17_centos mysql57]# cd /usr/mysql/mysql57

[root@VM_0_17_centos mysql57]# ll
total 56
drwxr-xr-x  2 mysql mysql  4096 Aug 11 21:24 bin
-rw-r--r--  1 mysql mysql 17987 Dec 28  2017 COPYING
drwxr-xr-x  2 mysql mysql  4096 Aug 11 21:40 data
drwxr-xr-x  2 mysql mysql  4096 Aug 11 21:24 docs
drwxr-xr-x  3 mysql mysql  4096 Aug 11 21:23 include
drwxr-xr-x  5 mysql mysql  4096 Aug 11 21:24 lib
drwxr-xr-x  4 mysql mysql  4096 Aug 11 21:23 man
-rw-r--r--  1 mysql mysql  2478 Dec 28  2017 README
drwxr-xr-x 28 mysql mysql  4096 Aug 11 21:24 share
drwxr-xr-x  2 mysql mysql  4096 Aug 11 21:24 support-files

权限被修改

  1. 配置mysql(重点部分)

创建以下文件,设置访问权限,用于mysql配置中

第一步:创建文件/tmp/mysql.sock。并设置权限

创建文件


[root@VM_0_17_centos mysql57]# mkdir tmp
[root@VM_0_17_centos mysql57]# cd tmp

[root@VM_0_17_centos tmp]# ll
total 0

[root@VM_0_17_centos tmp]# touch mysql.sock

[root@VM_0_17_centos tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 11 21:59 mysql.sock

设置权限

[root@VM_0_17_centos tmp]# chown -R mysql:mysql /usr/mysql/mysql57/tmp/mysql.sock

[root@VM_0_17_centos tmp]# chmod 755 /usr/mysql/mysql57/tmp/mysql.sock

第二步:创建/log/mysqld.log。并设置权限

[root@VM_0_17_centos mysql57]# mkdir log
[root@VM_0_17_centos mysql57]# cd log

[root@VM_0_17_centos log]# ll
total 0

[root@VM_0_17_centos log]# touch mysqld.log
[root@VM_0_17_centos log]# chown -R mysql:mysql /usr/mysql/mysql57/log/mysqld.log
[root@VM_0_17_centos log]# chmod 755 /usr/mysql/mysql57/log/mysqld.log

如果出错,说明路径没有写全,要写绝对路径

第三步:创建/tmp/mysqld.pid。并设置权限

[root@VM_0_17_centos log]# cd ../tmp
[root@VM_0_17_centos tmp]# touch mysqld.pid
[root@VM_0_17_centos tmp]# chown -R mysql:mysql /usr/local/mysql57/tmp/mysqld.pid 
[root@VM_0_17_centos tmp]# chmod 755 /usr/local/mysql57/tmp/mysqld.pid 
  1. 初始化mysql
[root@VM_0_17_centos mysql57]# bin/mysqld --initialize --user=mysql --basedir=/usr/mysql/mysql57/ --datadir=/usr/local/mysql57/data/

可能会报错。报错信息

 bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

  解决方法:
原因:
yum安装的是libnuma.so.1,但安装时默认安装的是32的,而db2需要的是64位的

  • 1.如果已经安装了libnuma.so.1,先yum remove libnuma.so.1
[root@VM_0_17_centos mysql57]# yum remove libnuma.so.1
  • 2.安装依赖包 yum -y install numactl.x86_64
[root@VM_0_17_centos mysql57]# yum -y install numactl.x86_64
  • 3.安装完成后重新,执行
[root@VM_0_17_centos mysql57]# bin/mysqld --initialize --user=mysql --basedir=/usr/mysql/mysql57/ --datadir=/usr/local/mysql57/data/

  安装成功 临时密码
在这里插入图片描述

 配置SSL参数(在mysql目录下)

[root@VM_0_17_centos mysql57]# bin/mysql_ssl_rsa_setup --datadir=/usr/mysql/mysql57/data/

 由于mysql-5.7.23版本my.cnf不在/support-files下,故我们创建my.cnf文件。

[root@VM_0_17_centos mysql57]# touch /etc/my.cnf
[root@VM_0_17_centos mysql57]# vim /etc/my.cnf

  复制如下内容(每个人安装路径可能不一样,需要修改):

[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql57
datadir=/usr/local/mysql57/data
port = 3306
socket=/tmp/mysql.sock
log-error=/usr/local/mysql57/log/mysqld.log
pid-file=/usr/local/mysql57/tmp/mysqld.pid
#表名不区分大小写
lower_case_table_names = 1
max_connections=5000
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

配置完成

  1. 启动mysql
  • 方式一
[root@VM_0_17_centos mysql57]# bin/mysqld_safe --user=mysql &

查看mysql的运行情况

[root@VM_0_17_centos mysql57]# ps -ef |grep mysql

第一我没有成功,因为我有个地方出问题了。我在方式二配置成功。

  • 方式二
    配置mysql自动启动(可根据需要配置)
[root@VM_0_17_centos mysql57]# cp support-files/mysql.server /etc/init.d/mysql
[root@VM_0_17_centos mysql57]# vim /etc/init.d/mysql

添加配置(i 进入编辑;esc–> :wq保存退出)

若配置了mysql自启动方式则可以使用服务方式启动mysql

#查看mysql状态
/etc/init.d/mysql status 或者 service mysql status
#启动mysql
/etc/init.d/mysql start 或者 service mysql start
#停止mysql
/etc/init.d/mysql stop 或者 service mysql stop
#重新启动mysql
/etc/init.d/mysql restart 或者 service mysql restart
查看mysql服务说明启动成功
ps -ef|grep mysql
 启动mysql
[root@VM_0_17_centos tmp]# service mysql start

Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql57/tmp/mysqld.pid).
报错了,上面说没有/usr/local/mysql57/tmp/mysqld.pid。

  解决方案:

a) 创建文件/usr/local/mysql57/tmp/mysqld.pid

b) 修改权限

修改存放mysqld.pid文件目录的权限

chown -R mysql /usr/local/mysql57/tmp
chgrp -R mysql /usr/local/mysql57/tmp
chmod 777 /usr/local/mysql57/tmp

重新启动成功(如果还不行,就是中间某个步骤写错了。或者直接把mysql目录权限赋为777)

[root@VM_0_17_centos tmp]# /etc/init.d/mysql start
Starting MySQL. SUCCESS! 
  1. 配置mysql到环境变量
[root@VM_0_17_centos tmp]# vim /etc/profile

在这里插入图片描述

export MYSQL57_HOME=/usr/mysql/mysql57/bin 
export PATH=$PATH:${MYSQ57_HOME}

这个地方是冒号 ( :)

设置环境变量立即生效

[root@VM_0_17_centos tmp]# source /etc/profile
  1. mysql忘记密码

切换到mysql下的bin目录

[root@localhost bin]# ./mysql -u root -p
Enter password: 

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

密码错误。。而且之前安装的密码也忘记了

  解决方案:

  • 第一步:跳过MySQL的密码认证过程

(注:windows下修改的是my.ini)

[root@VM_0_17_centos bin]# vim /etc/my.cnf

在[mysqld]后面任意一行添加“skip-grant-tables ”用来跳过密码验证的过程,如下图所示:
在这里插入图片描述

保存并退出(esc–> :wq)

  • 第二步:重启mysql
[root@VM_0_17_centos bin]# /etc/init.d/mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!
  • 第三步:登录mysql

进入mysql/bin目录,启动mysql

[root@VM_0_17_centos bin]# ./mysql

启动成功

  • 第四步:使用sql语句修改密码
mysql> use mysql;
mysql> update user set authentication_string=password(" 你的新密码 ") where user="root";
mysql> flush privileges;
mysql> quit
  • 第五步:重新编辑my.cnf

去掉[mysqld]后面的“skip-grant-tables”

[root@VM_0_17_centos bin]# vim /etc/my.cnf 

在这里插入图片描述

重启mysql

[root@VM_0_17_centos bin]# /etc/init.d/mysql restart
##注意有的版本需要使用mysqld命令
[root@VM_0_17_centos bin]# /etc/init.d/mysqld restart
  1. 设置mysql远程登录

先本地登录mysql

[root@localhost bin]# ./mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  • 1.报错

突然报错

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

解决方案:需要重新修改一下密码

mysql> alter user 'root'@'localhost' identified by '修改的密码';
mysql> flush privileges;
mysql> quit;
  • 2.继续配置
mysql> use mysql;
#改表法
mysql> update  user  set host='%' where user='root';
#授权法
mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypwd' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES; 
mysql> quit;
  • 3.重启mysql
[root@VM_0_17_centos bin]# service mysql restart;
##注意有的版本需要使用mysqld命令
[root@VM_0_17_centos bin]# /etc/init.d/mysqld restart
  • 4.设置防火墙

a)配置防火墙开启3306端口

[root@VM_0_17_centos bin]# /sbin/iptables -I INPUT -p tcp --dport 3306-j ACCEPT

[root@VM_0_17_centos bin]# /etc/rc.d/init.d/iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:   [  OK  ]

[root@VM_0_17_centos bin]# /etc/rc.d/init.d/iptables restart
iptables: Setting chains to policy ACCEPT: filter         	[  OK  ]
iptables: Flushing firewall rules:                     	[  OK  ]
iptables: Unloading modules:                       	[  OK  ]
iptables: Applying firewall rules:                    	[  OK  ]

b)临时关闭防火墙

[root@VM_0_17_centos bin]# service iptables stop

c)永久关闭防火墙

重启后永久生效

[root@VM_0_17_centos  bin]# chkconfig iptables off

4.3 通过Docker安装

其实上面的tomcat,mysql都可以通过docker快速安装,这也是docker容器化的一个好处。
这也是今后的一个主流。可以看这个文档,快速上手。

docker run -p 3306:3306 --name mysql01 -e MYSQL_ROOT_PASSWORD=你的密码   -d mysql

# 说明
#-e MYSQL_ROOT_PASSWORD   指定MySQL的密码
  • 通过docker exec进入容器中
[root@huawei-gqzdev ~]# docker exec -it f5d1962c9493 bash
  • 登录mysql
[root@huawei-gqzdev ~]# docker exec -it f5d1962c9493 bash
bash-4.2# 
bash-4.2# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

完成mysql远程登录的授权操作

mysql> use mysql;
#改表法
mysql> update  user  set host='%' where user='root';

#授权法
mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypwd' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES; 
mysql> quit;

使用Navicat工具连接即可
在这里插入图片描述

5.部署项目

下面是将项目打包部署到服务器上面了,有空的话我会接着写下面的内容!!

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值