一、CentOS7下MySQL-5.7使用yum方式安装:
Centos7的MySQL安装
卸载mariaDb
mysql被oracle收购后为了防止mysql有可能变成闭源,因此mysql创始人maria就开源做了一个mariaDb, centos7是自带使用了这个数据库的.因此安装mysql之前,应当首先卸载mariadb数据库:
1 2 3 4 5 6 |
$ sudo yum list installed | grep mariadb #检查mariadb是否已安装 $ sudo yum list installed | grep mariadb mariadb-libs.x86_64 1:5.5.56-2.el7 @anaconda $ sudo yum -y remove mariadb* #全部卸载 |
同理如果是已经安装过其他版本的MySQL,安装新的版本之前也需要按照如上方法进行卸载。
安装MySQL
下载mysql的YUM源: https://dev.mysql.com/downloads/repo/yum/
YUM源的官方安装说明:https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/
下载源mysql的YUM源,官网提供的是最新的MySQL8.0的源,内含其他较低版本的源
1 |
wget -P /home/hadoop/softwares https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm --no-check-certificate |
由于我们是下载到/home/hadoop/softwares
目录下,所以先切换到该目录下:
1 |
cd /home/hadoop/softwares |
安装mysql的YUM源:
1 |
sudo rpm -ivh mysql80-community-release-el7-3.noarch.rpm |
检查mysql的YUM源是否安装成功:
1 2 3 4 5 |
sudo yum repolist enabled | grep mysql mysql-connectors-community/x86_64 MySQL Connectors Community 175 mysql-tools-community/x86_64 MySQL Tools Community 120 mysql80-community/x86_64 MySQL 8.0 Community Server 211 |
选择要启用的mysql版本
查看源里包含的mysql版本,执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
sudo yum repolist all | grep mysql mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community 禁用 mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - So 禁用 mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community 禁用 mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - So 禁用 mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community 禁用 mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - So 禁用 mysql-connectors-community/x86_64 MySQL Connectors Community 启用: 175 mysql-connectors-community-source MySQL Connectors Community - Sou 禁用 mysql-tools-community/x86_64 MySQL Tools Community 启用: 120 mysql-tools-community-source MySQL Tools Community - Source 禁用 mysql-tools-preview/x86_64 MySQL Tools Preview 禁用 mysql-tools-preview-source MySQL Tools Preview - Source 禁用 mysql55-community/x86_64 MySQL 5.5 Community Server 禁用 mysql55-community-source MySQL 5.5 Community Server - Sou 禁用 mysql56-community/x86_64 MySQL 5.6 Community Server 禁用 mysql56-community-source MySQL 5.6 Community Server - Sou 禁用 mysql57-community/x86_64 MySQL 5.7 Community Server 禁用 mysql57-community-source MySQL 5.7 Community Server - Sou 禁用 mysql80-community/x86_64 MySQL 8.0 Community Server 启用: 211 mysql80-community-source MySQL 8.0 Community Server - Sou 禁用 |
可以通过类似下面的语句来启动和禁用某些版本,比如这里是禁用默认启用的MySQL8.0的源,转而启用我们需要安装的MySQL5.7的源。
或者通过修改
/etc/yum.repos.d/mysql-community.repo
文件,改变默认安装的mysql版本。比如要安装5.7版本,将8.0源的enabled=1改成enabled=0,然后再将5.7源的enabled=0改成enabled=1即可。
1 2 |
sudo yum-config-manager --enable mysql57-community sudo yum-config-manager --disable mysql80-community |
要使用yum-config-manager命令, 需要先安装yum-utils: sudo yum install yum-utils
注意: 同一时间只允许enable一个MySQL版本。
如果只需要安装MySQL5.7的版本,也可以不看上面,直接本地安装指定的远程MySQL5.7源
1 yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
查看当前的启用的 MySQL 版本:
1 2 3 4 5 |
yum repolist enabled | grep mysql mysql-connectors-community/x86_64 MySQL Connectors Community 175 mysql-tools-community/x86_64 MySQL Tools Community 120 mysql57-community/x86_64 MySQL 5.7 Community Server 464 |
安装MySQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
sudo yum install -y mysql-community-server 已加载插件:fastestmirror mysql-connectors-community | 2.6 kB 00:00:00 mysql-tools-community | 2.6 kB 00:00:00 mysql57-community | 2.6 kB 00:00:00 mysql57-community/x86_64/prima FAILED http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/repodata/cae8c564a11e5fca11dbe958c273358f206f1bba-primary.sqlite.bz2: [Errno 14] curl#7 - "Failed connect to repo.mysql.com:80; Connection refused" 正在尝试其它镜像。 mysql57-community/x86_64/primary_db | 247 kB 00:00:00 Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 正在解决依赖关系 --> 正在检查事务 ---> 软件包 mysql-community-server.x86_64.0.5.7.32-1.el7 将被 安装 --> 正在处理依赖关系 mysql-community-common(x86-64) = 5.7.32-1.el7,它被软件包 mysql-community-server-5.7.32-1.el7.x86_64 需要 --> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.7.9,它被软件包 mysql-community-server-5.7.32-1.el7.x86_64 需要 --> 正在检查事务 ---> 软件包 mysql-community-client.x86_64.0.5.7.32-1.el7 将被 安装 --> 正在处理依赖关系 mysql-community-libs(x86-64) >= 5.7.9,它被软件包 mysql-community-client-5.7.32-1.el7.x86_64 需要 ---> 软件包 mysql-community-common.x86_64.0.5.7.32-1.el7 将被 安装 --> 正在检查事务 ---> 软件包 mysql-community-libs.x86_64.0.5.7.32-1.el7 将被 安装 --> 解决依赖关系完成 依赖关系解决 ============================================================================================== Package 架构 版本 源 大小 ============================================================================================== 正在安装: mysql-community-server x86_64 5.7.32-1.el7 mysql57-community 173 M 为依赖而安装: mysql-community-client x86_64 5.7.32-1.el7 mysql57-community 25 M mysql-community-common x86_64 5.7.32-1.el7 mysql57-community 308 k mysql-community-libs x86_64 5.7.32-1.el7 mysql57-community 2.3 M 事务概要 ============================================================================================== 安装 1 软件包 (+3 依赖软件包) 总下载量:201 M 安装大小:875 M .... 已安装: mysql-community-server.x86_64 0:5.7.32-1.el7 作为依赖被安装: mysql-community-client.x86_64 0:5.7.32-1.el7 mysql-community-common.x86_64 0:5.7.32-1.el7 mysql-community-libs.x86_64 0:5.7.32-1.el7 |
启动MySQL服务
1 |
systemctl start mysqld.service # 或 service mysqld start |
配置开机启动
1 |
systemctl enable mysqld.service |
查看运行状态
1 2 3 4 |
systemctl status mysqld.service mysql 2574 1 1 23:49 ? 00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid |
查看到进程信息
1 2 |
netstat -anpl | grep mysql |
查看端口,可以看出mysql server的进程mysqld所使用的默认端口即3306
1 2 3 4 5 6 7 8 |
$ sudo netstat -anpl | grep tcp tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1412/sshd tcp 0 52 192.168.186.103:22 192.168.186.1:54058 ESTABLISHED 2287/sshd: hadoop tcp6 0 0 :::3306 :::* LISTEN 2574/mysqld tcp6 0 0 192.168.186.103:3888 :::* LISTEN 2060/java tcp6 0 0 :::22 :::* LISTEN 1412/sshd tcp6 0 0 :::37791 :::* LISTEN 2060/java tcp6 0 0 :::2181 :::* LISTEN 2060/java |
找到临时密码
但是要登陆mysql,我们需要root密码,这个密码是安装时随机生成在MySQL的服务器日志中的
1 2 3 4 |
grep "temporary password" /var/log/mysqld.log 2020-02-26T17:05:45.104999Z 1 [Note] A temporary password is generated for root@localhost: bl/!6qaU.wuX |
登录MySQL
1 2 |
# 回车后输入在日志中找到的临时root登录密码登录 mysql -u roop -p |
问题: 日志里没有找到临时密码, 或者密码不能登录,可能的原因就是之前安装过MySQL
解决办法:
1 sudo rm -rf /var/lib/mysql再重启服务后就可以在日志文件中找到重新生成的临时密码了
12345 systemctl restart mysqld.service$ cat /var/log/mysqld.log | grep "temporary password"2020-02-26T17:05:45.104999Z 1 [Note] A temporary password is generated for root@localhost: bl/!6qaU.wuX2020-08-01T16:07:26.941916Z 1 [Note] A temporary password is generated for root@localhost: x/ttqh=)_6Z/也可以直接使用明文密码登陆,像这样
1 mysql -uroop -p"x/ttqh=)_6Z/"初始密码我们可以这样操作,因为很快我们会将将其改为其他的密码。
但是正常使用时不建议这样 不安全
黑客可以使用history命令查看到你在命令行的输入获取到你的MySQL的root密码
另外MySQL报错在/var下面的log目录下可以查看
如果是虚拟机环境为了方便可以选择配置跳过密码验证
1 vim /etc/my.cnf添加以下代码
123 default-authentication-plugin=mysql_native_password #配置文件本来就有去掉注释即可symbolic-links=0skip-grant-tables #跳过密码验证
第一次登陆后系统会很快提示你修改掉默认密码
1 2 |
mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. |
尝试修改密码(虚拟机可以设置简单密码,如果是购买的服务器则需设置复杂密码并修改默认端口号)
1 2 |
mysql> alter user 'root'@'localhost' identified by 'niit1234'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements |
密码安全策略
但是基于密码策略,所设置的密码必须要包含大小写字母、数字和字符。
修改策略并设置简单密码
将策略要求置为0(LOW),长度要求置为1
1 2 |
mysql> set global validate_password_policy=0; mysql> set global validate_password_length=1; |
关于密码安全策略:
通过show命令查看当前的策略
1234567891011121314 mysql> show variables like 'validate_password%';+--------------------------------------+--------+| Variable_name | Value |+--------------------------------------+--------+| validate_password_check_user_name | OFF || validate_password_dictionary_file | || validate_password_length | 8 || validate_password_mixed_case_count | 1 || validate_password_number_count | 1 || validate_password_policy | MEDIUM || validate_password_special_char_count | 1 |+--------------------------------------+--------+7 rows in set (0.00 sec)1) validate_password_policy:密码安全策略,默认MEDIUM策略
策略 检查规则 0 or LOW Length 1 or MEDIUM Length; numeric, lowercase/uppercase, and special characters 2 or STRONG Length; numeric, lowercase/uppercase, and special characters; dictionary file 2)validate_password_dictionary_file:密码策略文件,策略为STRONG才需要
3)validate_password_length:密码最少长度
4)validate_password_mixed_case_count:大小写字符长度,至少1个
5)validate_password_number_count :数字至少1个
6)validate_password_special_char_count:特殊字符至少1个
这样以后就可以使用新密码登陆了
开启远程登陆
mysql命令的-h选项可以设置需要远程登陆mysql的服务器的ip地址
如果是本地的话可以使用127.0.0.1或者直接省略
但是远程访问因为安全原因默认是关闭的
1 2 3 4 5 6 7 8 9 10 |
mysql> select Host,User from user; +-----------+---------------+ | Host | User | +-----------+---------------+ | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+---------------+ 3 rows in set (0.00 sec) |
这里可以看到host都是localhost,这就是因为这些账号只有本地访问的权限
我们需要将root的访问权限扩大都允许任意方式访问(包括远程访问)
1 |
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'niit1234' WITH GRANT OPTION; |
也可以创建一个专门用来支持远程访问的单独用户
1 |
GRANT ALL PRIVILEGES ON *.* TO 'remote'@'%' IDENTIFIED BY 'niit1234' WITH GRANT OPTION; |
如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码
若你想限制能连接到mysql服务器的客户端所在的IP地址为’192.168.1.6′
并使用mypassword作为密码,则命令如下:
1 GRANT ALL PRIVILEGES ON . TO 'mysqluser'@'192.168.1.6'IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
使修改生效
现在虽然修改了远程访问权限,但是还没有生效
我们需要刷新权限(或者重启服务),当然直接在mysql执行环境刷新权限是更好的方法
1 |
mysql>FLUSH PRIVILEGES; |
然后我们可以试试使用navicat远程连接虚拟机中mysql试试
navicat虽然是收费的还是可以下载到破解的版本
最后一点
如果你的服务器开启了防火墙也可能导致远程无法连接
因为mysql默认使用3306端口,我们需要开放这个端口给mysql客户端连接
修改编码
默认安装的设置对中文的支持不好,一般需要修改成utf8,但这里我们修改成utf8mb4编码,因为它是uft8的超集,减少更多乱码的问题,也是推荐使用的编码,比如需要插入emoji字符在数据库中,utf8的显示就会出现问题,而utf8mb4则没有问题。
首先查看当前的编码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
mysql> show variables like '%char%'; +--------------------------------------+----------------------------+ | Variable_name | Value | +--------------------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | | validate_password_special_char_count | 1 | +--------------------------------------+----------------------------+ 9 rows in set (0.00 sec) |
为了修改编码,我们需要修改mysql的核心配置文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
[hadoop@hadoop000 download]$ sudo vim /etc/my.cnf # For advice on how to change settings please see # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [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 # # 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 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-error=/var/log/mysqld.log # 默认服务器内部操作字符集 character-set-server=utf8mb4 # 默认服务器内部操作字符集校对规则 collation-server=utf8mb4_general_ci # 默认的存储引擎 default-storage-engine=InnoDB # 初始化连接时设置以下字符集: # character_set_client # character_set_results # character_set_connection init_connect='set names utf8mb4' [client] default-character-set=utf8mb4 [mysql] default-character-set=utf8mb4 |
- 30行以下为手动添加的配置
验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
mysql> show variables like '%char%'; +--------------------------------------+----------------------------+ | Variable_name | Value | +--------------------------------------+----------------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | | validate_password_special_char_count | 1 | +--------------------------------------+----------------------------+ 9 rows in set (0.00 sec) |
- character_set_client 是指客户端发送过来的语句的编码
- character_set_database 是指服务器内部使用的编码
- character_set_connection 是指mysqld收到客户端的语句后,要转换到的编码
- character_set_results 是指server执行语句后,返回给客户端的数据的编码
- character_set_system是元数据编码,无需修改
- character_set_filesystem是文件系统的编码,2进制存储最有效,不能修改
rpm-bundle的离线安装
下载rpm-bundle包
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.33-1.el7.x86_64.rpm-bundle.tar $ tar xvf mysql-5.7.33-1.el7.x86_64.rpm-bundle.tar $ mkdir mysql-jars $ mv mysql-comm*.rpm mysql-jars/ $ cd mysql-jars/ $ ls mysql-community-client-5.7.33-1.el7.x86_64.rpm mysql-community-common-5.7.33-1.el7.x86_64.rpm mysql-community-libs-5.7.33-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.33-1.el7.x86_64.rpm mysql-community-server-5.7.33-1.el7.x86_64.rpm |
依次手动安装
1 2 3 4 5 |
sudo rpm -ivh mysql-community-common-5.7.33-1.el7.x86_64.rpm sudo rpm -ivh mysql-community-libs-5.7.33-1.el7.x86_64.rpm sudo rpm -ivh mysql-community-libs-compat-5.7.33-1.el7.x86_64.rpm sudo rpm -ivh mysql-community-client-5.7.33-1.el7.x86_64.rpm sudo rpm -ivh mysql-community-server-5.7.33-1.el7.x86_64.rpm |
CentOS7下MySQL-5.7使用yum方式卸载
1、停止MySQL
命令:systemctl stop mysqld
2、查看已安装的mysql
命令:rpm -qa | grep -i mysql
3、卸载mysql,依次卸载第2步骤所列出的有关MySQL的安装包,如
命令:yum remove -y mysql-community-server-5.6.36-2.el7.x86_64
4、删除mysql相关目录
1)使用命令查看mysql相关的文件目录:find / -name mysql
2)依次删除所查到的目录,命令:rm -rf /xxx/xxx/mysql
二、CentOS7下Nginx的安装:
Nginx安装
安装前确认是否已经安装过
1 |
sudo yum search nginx |
Nginx文档: http://nginx.org/en/docs/
Installation on Linux, nginx packages from nginx.org can be used.
Installation instructions
RHEL/CentOS
Debian
Ubuntu
SLES
Alpine
以 CentOS为例进行安装
Install the prerequisites:
1 sudo yum install yum-utils
To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo
with the following contents:
1 |
sudo vi /etc/yum.repos.d/nginx.repo |
内容如下:
123456789101112131415 [nginx-stable]name=nginx stable repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx_signing.keymodule_hotfixes=true[nginx-mainline]name=nginx mainline repobaseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/gpgcheck=1enabled=0gpgkey=https://nginx.org/keys/nginx_signing.keymodule_hotfixes=true
By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:
1 sudo yum-config-manager --enable nginx-mainline
To install nginx, run the following command:
1 sudo yum install -y nginx
When prompted to accept the GPG key, verify that the fingerprint matches 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62
, and if so, accept it.
常用命令
解释 | 命令 |
---|---|
安装服务 | yum install nginx |
启动服务 | service nginx start |
停止服务 | service nginx stop |
重载服务 | service nginx reload |
配置完成后启动服务
1 |
sudo service nginx start |
如果服务器已经启动,当配置发生变化可以直接使用重载服务来更新配置,运维常用,因为不需要停止服务就可以重载新的配置。
三、部署前端网站到nginx下
电商网站的静态文件都放在shop文件夹中,将此文件夹上传到虚拟机的/opt/data/
目录下,
对nginx默认配置文件进行改名
1 2 |
$ cd /etc/nginx/conf.d/ $ mv default.conf shop.conf |
修改如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
server { listen 80; server_name hadoop100; root /opt/data/; location /shop/ { index index.html; access_log /var/log/nginx/access.log main; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } |
重新加载nginx配置,执行:
1 |
$ sudo nginx -s reload |
在浏览器中验证下是否可以访问到网站,输入:
1 |
http://hadoop100/shop/ |
效果如下:
四、Tomcat的安装
准备:安装Java JDK1.8
这里安装openjdk是因为比较简单, 工作场合一定要使用oracle提供的jdk1.8
1 |
sudo yum -y install java-1.8.0-openjdk* |
这样安装的好处就是环境变量都配好了
可以直接查看版本 java -version
Tomcat安装
下载页面: https://tomcat.apache.org/download-90.cgi
文档:https://tomcat.apache.org/tomcat-9.0-doc/index.html
1 |
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.41/bin/apache-tomcat-9.0.41.tar.gz |
解压到后改名tomcat9
1 2 |
tar -zxvf apache-tomcat-9.0.41.tar.gz mv apache-tomcat-9.0.41 /opt/pkg/tomcat9 |
由于tomcat的端口8080和大数据环境中的jetty端口冲突,这里修改tomcat的端口位18080
1 2 3 4 5 6 |
[hadoop@hadoop100 ~]$ cat /opt/pkg/tomcat9/conf/server.xml ... <Connector port="18080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> |
启动 进入 /bin
目录下 运行startup.sh
脚本文件
1 2 3 |
bin]$ ./startup.sh ... Tomcat started. |
检查tomcat进程信息
1 2 3 |
$ bin]$ ps -ef | grep tomcat hadoop 15142 1 1 14:41 pts/0 00:00:05 /home/hadoop/app/jdk1.8.0_211/bin/java -Djava.util.logging.config.file=/home/hadoop/app/tomcat9/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /home/hadoop/app/tomcat9/bin/bootstrap.jar:/home/hadoop/app/tomcat9/bin/tomcat-juli.jar -Dcatalina.base=/home/hadoop/app/tomcat9 -Dcatalina.home=/home/hadoop/app/tomcat9 -Djava.io.tmpdir=/home/hadoop/app/tomcat9/temp org.apache.catalina.startup.Bootstrap start hadoop 15296 15064 0 14:47 pts/0 00:00:00 grep --color=auto tomcat |
检查对应的监听端口信息
1 |
netstat -anpt | grep 15142 |
修改conf/tomcat-users.xml添加tomcat用户
1 2 3 4 5 6 7 8 9 10 11 12 |
添加如下内容 <!-- <role rolename="tomcat"/> <role rolename="role1"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user username="both" password="tomcat" roles="tomcat,role1"/> <user username="role1" password="tomcat" roles="role1"/> --> <role rolename="manager-gui"/> <role rolename="manager-script" /> <user username="admin" password="123123" roles="manager-gui,manager-script" /> </tomcat-users> |
为了能够正常访问tomcat管理界面, 修改webapps/manager/META-INF
目录下的context.xml
,在allow行的末尾加上\d+.\d+.\d+.\d+
表示允许所有主机访问。
1 2 3 4 5 |
<Context antiResourceLocking="false" privileged="true" > <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|\d+\.\d+\.\d+\.\d+" /> <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/> </Context> |
重启tomcat9生效
1 2 3 |
bin/shutdown.sh ... bin/startup.sh |
1 |
6)验证,在浏览器输入: <code>http://hadoop100:8080/</code> |
或者在命令行里面输入:netstat -anop|grep 18080
五、电商后台系统部署到Tomcat服务器上
在MySQL创建数据库shop,将项目所需的sql数据导入,如:
1 |
mysql> source /opt/data/shop.sql |
将代码导入到eclipse或者idea中
将代码数据库连接相关配置hibernate.properties
做如下修改:
- 修改数据库名及地址为服务器的数据库名及地址
- 修改数据库用户明、密码为服务器的用户密码
选择export->war
上传war包至Linux服务器
修改war包名位university.war
将war包移动到Tomcat安装目录下的webapp目录下
启动或重启Tomcat
打开浏览器进行验证:
配置nginx反向代理
18080端口的访问方式不太方便,另外也需要解决nginx下部署的静态页面调用后台服务的跨域问题,这里需要设置nginx方向代理,修改nginx配置文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
server { listen 80; server_name hadoop100; root /opt/data/; location /shop/ { index index.html; access_log /var/log/nginx/access.log main; } location /university/ { proxy_pass http://hadoop100:18080/university/; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } |
重新加载nginx配置
1 |
sudo nginx -s reload |
现在可以通过http://hadoop100/university/
访问后台界面
如果访问失败可以查看错误日志
1 |
sudo vi /var/log/nginx/error.log |
如果发现502错误:
1 |
2020/11/18 16:12:39 [crit] 16376#16376: *1 connect() to 127.0.0.1:8080 failed (13: Permission denied) while connecting to upstream, client: 192.168.186.1, server: hadoop000, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8080/favicon.ico", host: "hadoop000", referrer: "http://hadoop000/" |
此时需要考虑把linux操作系统默认的强制访问安全限制设置为禁用。
关闭SElinux即可
-
临时关闭 SElinux
1sudo setenforce 0 -
永久关闭 SElinux
12sudo vim /etc/selinux/configSELINUX=disabled
修改之后就可以正常访问了
通过http://hadoop100/shop
访问前端界面,可以看见数据可以正常加载了.
至此商城项目的前后端就配置完了, 商城的nginx服务器日志保存在/var/log/nginx/access.log
,接下来我们将使用httpd提供的ab压测工具来快速生成大量访问日志.
Views: 137