CentOS7.9安装JDK-Tomcat-MySQL

文章目录

          • 1 JDK
            • 1.1 卸载系统自带JDK
            • 1.2查看yum中的JDK安装包
            • 1.3 选一个需要的版本安装
          • 2 Tomcat
            • 2.1下载Tomcat tar.gz或zip
            • 2.2 解压安装
            • 2.3 配置环境变量
            • 2.4 启动tomcat
          • 3 MySQL
            • 3.1 卸载MariaDB
            • 3.2 下载MySQL
            • 3.2 检查是否已安装MySQL
            • 3.3 解压
            • 3.4 创建用户和用户组
            • 3.5 创建数据目录
            • 3.6 配置参数
            • 3.7 初始化
            • 3.8 启动服务
            • 3.9 登录
            • 3.10 修改密码
            • 3.11 创建用户

1 JDK
1.1 卸载系统自带JDK
//查找系统jdk
[root@3slartrmskwuaydn ~]#  rpm -qa|grep java 
java-1.6.0-openjdk-1.6.0.45
//卸载
[root@3slartrmskwuaydn ~]# rpm -e --allmatches --nodeps java-1.6.0-openjdk-1.6.0.45
//检查是否卸载干净
[root@3slartrmskwuaydn ~]#  rpm -qa|grep java
1.2查看yum中的JDK安装包
  • yum -y list java*
[root@3slartrmskwuaydn ~]# yum -y list java*
java-1.8.0-openjdk-headless.x86_64                                                              1:1.8.0.322.b06-1.el7_9                                                            @updates
javapackages-tools.noarch                                                                       3.4.1-11.el7                                                                       @base   
Available Packages                                                  1:1.7.0.261-2.6.22.2.el7_8                                                         base    
java-1.8.0-openjdk.i686                                                                         1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-accessibility.i686                                                           1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-accessibility.x86_64                                                         1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-demo.i686                                                                    1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-demo.x86_64                                                                  1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-devel.i686                                                                   1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-devel.x86_64                                                                 1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-headless.i686                                                                1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-javadoc.noarch                                                               1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-javadoc-zip.noarch                                                           1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-src.i686                                                                     1:1.8.0.322.b06-1.el7_9                                                            updates 
java-1.8.0-openjdk-src.x86_64                                                                   1:1.8.0.322.b06-1.el7_9                                                            updates 
java-11-openjdk.i686                                                                            1:11.0.14.1.1-1.el7_9                                                              updates 
java-11-openjdk.x86_64                                                                          1:11.0.14.1.1-1.el7_9                                                              updates 
java-11-openjdk-demo.i686                                                                       1:11.0.14.1.1-1.el7_9                                                              updates 
java-11-openjdk-demo.x86_64                                                                     1:11.0.14.1.1-1.el7_9                                                              updates 
java-11-openjdk-devel.i686                                                                      1:11.0.14.1.1-1.el7_9                                                              updates 
java-11-openjdk-devel.x86_64  
1.3 选一个需要的版本安装
  • yum install -y java-1.8.0-openjdk.x86_64

出现Complete! 就安装好了,yum安装JDK路径一般在/usr/lib/jvm/,要是找不到可以用 find / -name ‘java’ 命令查看.

....                         
  libtiff.x86_64 0:4.0.3-35.el7                             libwayland-client.x86_64 0:1.15.0-1.el7                   libwayland-server.x86_64 0:1.15.0-1.el7                              
  libxcb.x86_64 0:1.13-1.el7                                libxshmfence.x86_64 0:1.2-1.el7                           libxslt.x86_64 0:1.1.28-6.el7                                        
  lksctp-tools.x86_64 0:1.0.17-2.el7                        mesa-libEGL.x86_64 0:18.3.4-12.el7_9                      mesa-libGL.x86_64 0:18.3.4-12.el7_9                                  
  mesa-libgbm.x86_64 0:18.3.4-12.el7_9                      mesa-libglapi.x86_64 0:18.3.4-12.el7_9                    pango.x86_64 0:1.42.4-4.el7_7                                        
  pcsc-lite-libs.x86_64 0:1.8.8-8.el7                       pixman.x86_64 0:0.34.0-1.el7                              python-javapackages.noarch 0:3.4.1-11.el7                            
  python-lxml.x86_64 0:3.2.1-4.el7                          ttmkfdir.x86_64 0:3.0.9-42.el7                            tzdata-java.noarch 0:2022a-1.el7                                     
  xorg-x11-font-utils.x86_64 1:7.5-21.el7                   xorg-x11-fonts-Type1.noarch 0:7.5-9.el7                  

Complete!
2 Tomcat
2.1下载Tomcat tar.gz或zip

官网地址:https://tomcat.apache.org/download-90.cgi
CentOS7.9安装JDK-Tomcat-MySQL_第1张图片

2.2 解压安装

上传到服务器/usr/local/tmp并解压

//切换到上传目录
[root@3slartrmskwuaydn tmp]# tar zxvf apache-tomcat-9.0.62.tar.gz
//解压之后复制到/usr/local/tomcat目录下
[root@3slartrmskwuaydn tmp]# cp -r apache-tomcat-9.0.62 /usr/local/tomcat
2.3 配置环境变量

vim /etc/profile

[root@3slartrmskwuaydn bin]# vi /etc/profile

//最下面添加内容  :wq 保存 
export TOMCAT_HOME=/usr/local/tomcat
export CATALINA_HOME=/usr/local/tomcat

//执行source /etc/profile
[root@3slartrmskwuaydn tmp]# source /etc/profile
2.4 启动tomcat

切换到tomcat/bin

//启动tomcat
[root@3slartrmskwuaydn bin]# ./startup.sh

//停止tomcat
[root@3slartrmskwuaydn bin]# ./shutdown.sh

遇到了个问题
浏览器访问http://ip:8080,访问不了.确定防火墙,安全组都开了8080端口,也没端口占用.
查了一圈资料,发现天翼云 80,443,8080 需要备案才能访问…
后面把tomcat默认端口改下,就能正常访问了.

防火墙相关命令

1.查看默认防火墙状态
firewall-cmd --state

2.查看防火墙的状态
systemctl list-unit-files|grep firewalld.service
如果显示disabled,执行第三步
如果显示masked,取消masked状态
systemctl unmask firewalld

3.开启防火墙
systemctl start firewalld.service

4.添加开放的端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent

5.关闭开放的端口
firewall-cmd --zone= public --remove-port=8080/tcp --permanent

6.重新加载
firewall-cmd --reload

7.查看开放的端口
firewall-cmd --list-ports
3 MySQL

CentOS 7及以上版本已经不再使用MySQL数据库,而是使用MariaDB数据库,MariaDB是MySQL的一个分支,主要由开源社区维护,如果直接安装MySQL,会和MariaDB的文件冲突,所以需要先卸载自带的MariaDB,再安装MySQL.

3.1 卸载MariaDB
  • 查看是否存在:    rpm -qa|grep mariadb
  • 卸载:            rpm -e --nodeps 文件名
  • 检查是否卸载完成:rpm -qa|grep mariadb
//查看系统版本
[root@3slartrmskwuaydn ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
//卸载
[root@3slartrmskwuaydn ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
//检查是否卸载干净
[root@3slartrmskwuaydn ~]# rpm -qa|grep mariadb
[root@3slartrmskwuaydn ~]# 
3.2 下载MySQL

官网地址:https://dev.mysql.com/downloads/mysql/
Xftp上传到服务器/usr/local/tmp/
CentOS7.9安装JDK-Tomcat-MySQL_第2张图片

3.2 检查是否已安装MySQL

//查看系统是否已安装mysql,要是没输出就就说明没有安装
[root@3slartrmskwuaydn ~]# rpm -qa|grep mysql
[root@3slartrmskwuaydn ~]#

//要是有输出,并且不是自己想要的版本就卸载
 [root@3slartrmskwuaydn ~]# rpm -e --nodeps mysql文件名

3.3 解压
  • tar.gz后缀:tar -zxvf 文件名
  • tar.xz后缀:tar -Jxvf 文件名
//解压
[root@3slartrmskwuaydn tmp]# tar -Jxvf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz

mysql-8.0.28-linux-glibc2.12-x86_64/bin/
mysql-8.0.28-linux-glibc2.12-x86_64/bin/myisam_ftdump
mysql-8.0.28-linux-glibc2.12-x86_64/bin/myisamchk
mysql-8.0.28-linux-glibc2.12-x86_64/bin/myisamlog
mysql-8.0.28-linux-glibc2.12-x86_64/bin/myisampack
mysql-8.0.28-linux-glibc2.12-x86_64/bin/mysql
mysql-8.0.28-linux-glibc2.12-x86_64/bin/mysql_config_editor
mysql-8.0.28-linux-glibc2.12-x86_64/bin/mysql_migrate_keyring
mysql-8.0.28-linux-glibc2.12-x86_64/bin/mysql_secure_installation
mysql-8.0.28-linux-glibc2.12-x86_64/bin/mysql_ssl_rsa_setup
//# cp -r mysql-8.0.28-linux-glibc2.12-x86_64 /usr/local/mysql8
//解压之后复制到 /usr/local/mysql8
[root@3slartrmskwuaydn tmp]# cp -r mysql-8.0.28-linux-glibc2.12-x86_64 /usr/local/mysql8

3.4 创建用户和用户组
  • 创建用户组:groupadd
  • 创建用户:useradd
  • -r:创建系统用户
  • -g:指定用户组

#创建一个用户组:mysql
[root@3slartrmskwuaydn ~]# groupadd mysq
# 创建一个系统用户:mysql,指定用户组为mysql
[root@3slartrmskwuaydn ~]# useradd -r -g mysql mysql

3.5 创建数据目录
#创建目录
[root@3slartrmskwuaydn /]# mkdir -p /data/mysql8_data/
#更改属性和组
[root@3slartrmskwuaydn /]# chown -R mysql:mysql /data/mysql8_data
#更改权限
[root@3slartrmskwuaydn /]# chmod -R 750 /data/mysql8_data/

3.6 配置参数

在/usr/local/etc/下,创建my.cnf配置文件,初始化MySQL数据库
使用vim编辑器复制以下内容.

[mysql]
# 默认字符集
default-character-set=utf8mb4
[client]
port       = 3306
socket     = /tmp/mysql.sock

[mysqld]
port       = 3306
server-id  = 3306
user       = mysql
socket     = /tmp/mysql.sock
# 安装目录
basedir    = /usr/local/mysql8
# 数据存放目录
datadir    = /data/mysql8_data/mysql
log-bin    = /data/mysql8_data/mysql/mysql-bin
innodb_data_home_dir      =/data/mysql8_data/mysql
innodb_log_group_home_dir =/data/mysql8_data/mysql
#日志及进程数据的存放目录
log-error =/data/mysql8_data/mysql/mysql.log
pid-file  =/data/mysql8_data/mysql/mysql.pid
# 服务端使用的字符集默认为8比特编码
character-set-server=utf8mb4
lower_case_table_names=1
autocommit =1
 
##################以上要修改的########################
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
 
#query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
 
binlog_format=mixed
 
binlog_expire_logs_seconds =864000
 
# 创建新表时将使用的默认存储引擎
default_storage_engine = InnoDB
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED
 
[mysqldump]
quick
max_allowed_packet = 16M
 
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 4M
read_buffer = 2M
write_buffer = 2M
 
[mysqlhotcopy]
interactive-timeout

3.7 初始化

参数

  • --defaults-file:指定配置文件(要放在–initialize 前面)
  • --user: 指定用户
  • --basedir:指定安装目录
  • --datadir:指定初始化数据目录
  • --intialize-insecure:初始化无密码
    初始化数据库
mysqld --defaults-file=/usr/local/etc/my.cnf --basedir=/usr/local/mysql8 --datadir=/data/mysql8_data/mysql --user=mysql --initialize-insecure

注意:如果出现mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory 那可能是没有安装了libaio,


#查看下是否安装libaio,没输出就是装
[root@3slartrmskwuaydn etc]# rpm -qa|grep libaio
#yum 安装libaio
[root@3slartrmskwuaydn etc]# yum install -y libaio

3.8 启动服务
  • 启动 $mysql_dir/bin/mysqld_safe &
  • 关闭 $mysql_dir/bin/mysqladmin -uroot -p shutdown
#启动MySQL
[root@3slartrmskwuaydn bin]# mysqld_safe --defaults-file=/usr/local/etc/my.cnf &
[1] 28418
[root@3slartrmskwuaydn bin]# 2022-04-13T08:31:14.020658Z mysqld_safe Logging to '/data/mysql8_data/mysql/mysql.log'.
2022-04-13T08:31:14.063712Z mysqld_safe Starting mysqld daemon with databases from /data/mysql8_data/mysql
#查看是否启动
ps -ef|grep mysql
root     28418 24942  0 16:31 pts/2    00:00:00 /bin/sh /usr/local/mysql8/bin/mysqld_safe --defaults-file=/usr/local/etc/my.cnf
mysql    28956 28418  7 16:31 pts/2    00:00:01 /usr/local/mysql8/bin/mysqld --defaults-file=/usr/local/etc/my.cnf --basedir=/usr/local/mysql8 --datadir=/data/mysql8_data/mysql --plugin-dir=/usr/local/mysql8/lib/plugin --user=mysql --log-error=/data/mysql8_data/mysql/mysql.log --open-files-limit=65535 --pid-file=/data/mysql8_data/mysql/mysql.pid --socket=/tmp/mysql.sock --port=3306
root     29027 24942  0 16:31 pts/2    00:00:00 grep --color=auto mysql
3.9 登录
# 无密码登录方式
mysql -u root --skip-password
# 有密码登录方式(初始的随机密码在/data/mysql8_data/mysql/mysql.log下)
mysql -u root -p
password:密码

注意: 如果出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 说明MySQL服务没开启.

3.10 修改密码
  • 首次修改
# 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
# 刷新权限
FLUSH PRIVILEGES;
  • 日常修改
#Linux命令行
mysqladmin -u用户名 -p旧密码 password 新密码

#MySQL命令行 设置密码
SET PASSWORD FOR '用户名'@'主机' = PASSWORD(‘密码');
# 刷新权限
FLUSH PRIVILEGES;
3.11 创建用户
  • 查看当前用户
#选择mysql数据库
mysql> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
#查看当前用户
mysql> SELECT user,host,plugin,authentication_string FROM user;
+------------------+-----------+-----------------------+------------------------------------------------------------------------+
| user             | host      | plugin                | authentication_string                                                  |
+------------------+-----------+-----------------------+------------------------------------------------------------------------+
| mysql.infoschema | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.session    | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.sys        | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| root             | localhost | mysql_native_password | *8E7F219B9C5AC6251152DD189EBD8CBE9C92D1CE                              |
+------------------+-----------+-----------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)

  • 创建新用户远程访问
#创建远程root用户
mysql> CREATE user 'root'@'%';
# 设置密码
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'qwerasdf';
# 授权用户所有权限
mysql> GRANT ALL PRIVILEGES ON *.* TO "root"@"%";
# 刷新权限
mysql> FLUSH PRIVILEGES;
#查看用户列表
mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

end

你可能感兴趣的:(java,java,mysql,tomcat)