1. 程式人生 > 其它 >第四節 伺服器端應用程式安裝

第四節 伺服器端應用程式安裝

1、友情提示:退路

①備份配置檔案

Linux 系統環境下配置檔案通常內容很多。如果不小心修改了不該修改的地方,自己有不記得做了修改,那麼將來報錯很難找到錯誤位置。

為了避免這樣的問題,我們可以在修改任何配置檔案之前都多複製一份:把原始、純淨、正確的配置檔案執行了備份。將來萬一發生問題,拿原始配置檔案覆蓋錯誤配置檔案,瞬間恢復到正確狀態可以重新開始。

②拍攝快照

[1]第一種用法

針對需要通過安裝過程來安裝的程式,如果安裝失敗需要解除安裝後重新安裝,那最好是解除安裝乾淨再重新執行安裝。Linux 環境下不方便執行『清理殘留』。

所以為了避免安裝失敗以後沒有退路可以重新開始,建議在執行相關安裝之前拍攝快照。一旦發生問題,可以恢復快照重新開始。

[2]第二種用法

對於已經安裝好的程式(特別是系統當中安裝了很多程式都已成功),強烈建議通過拍攝快照的方式保留這個正確的狀態。

[3]操作

  • 儲存快照
  • 恢復快照
  • 刪除快照

2、安裝JDK

①rpm命令

rpm是Redhat package management縮寫,實質上來說,通過rpm可以管理Linux環境下的安裝包。

引數名 作用
-qa 查詢系統中已經安裝的程式,通常配合管道,使用grep精確匹配想要查詢的包
-ivh 執行rpm包安裝操作
-e 解除安裝rpm包
--nodeps 在解除安裝過程中忽略依賴關係

注:Linux 系統中命令的引數往往有這樣的特點:

  • 引數如果是一個或多個完整的單詞那麼前面是兩個“-”
  • 引數如果是單詞的縮寫,是一個字母,那麼前面是一個“-”

②解除安裝系統預裝的JDK

查詢系統中已經安裝的JDK

[root@apple ~]# rpm -qa | grep openjdk
java-1.8.0-openjdk-headless-1.8.0.161-2.b14.el7.x86_64
java-1.7.0-openjdk-1.7.0.171-2.6.13.2.el7.x86_64
java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64
java-1.7.0-openjdk-headless-1.7.0.171-2.6.13.2.el7.x86_64

執行解除安裝,解除安裝過程中使用--nodeps忽略依賴關係

[root@apple ~]# rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.161-2.b14.el7.x86_64
[root@apple ~]# rpm -e --nodeps java-1.7.0-openjdk-1.7.0.171-2.6.13.2.el7.x86_64
[root@apple ~]# rpm -e --nodeps java-1.8.0-openjdk-1.8.0.161-2.b14.el7.x86_64
[root@apple ~]# rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.171-2.6.13.2.el7.x86_64

重啟系統生效

③解壓JDK的tar包配置環境變數

[1]解壓JDK的tar包

cd /opt
tar -zxvf /opt/jdk-8u121-linux-x64.tar.gz

[2]配置JDK相關環境變數

配置環境變數的配置檔案:/etc/profile

為了避免配置錯誤導致執行失敗,提前複製一份/etc/profile

cp /etc/profile /etc/profile.bak

編輯/etc/profile檔案

# 宣告JAVA_HOME環境變數,等號兩邊不能有空格
JAVA_HOME=/opt/jdk1.8.0_121

# 在PATH環境變數原有的值基礎上附加JDK的path值,$JAVA_HOME是對JAVA_HOME環境變數的引用
PATH=$JAVA_HOME/bin:$PATH

# 將JAVA_HOME和PATH釋出為全域性變數
export JAVA_HOME PATH

儲存退出vim後,使用source命令執行/etc/profile指令碼,讓指令碼中釋出的環境變數生效。但是僅限於當前程序,如果想讓新環境變數全域性生效,可以reboot。

source /etc/profile

驗證一下:

[root@apple jdk1.8.0_121]# echo $JAVA_HOME
/opt/jdk1.8.0_121

[root@apple jdk1.8.0_121]# echo $PATH
/opt/jdk1.8.0_121/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@apple jdk1.8.0_121]# java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

3、安裝Tomcat

①解壓

tar -zxvf /opt/apache-tomcat-7.0.75.tar.gz

②操控

啟動Tomcat

/opt/apache-tomcat-7.0.75/bin/startup.sh

停止Tomcat

/opt/apache-tomcat-7.0.75/bin/shutdown.sh

檢視日誌

tail -f /opt/apache-tomcat-7.0.75/logs/catalina.out

部署war包放在webapps目錄下

/opt/apache-tomcat-7.0.75/webapps

③訪問

4、MySQL安裝步驟

①解除安裝系統預裝mariadb

rpm -e --nodeps mariadb-libs-1:5.5.56-2.el7.x86_64

②安裝伺服器端程式

rpm -ivh /opt/MySQL-server-5.5.52-1.el6.x86_64.rpm

驗證

[root@apple opt]# id mysql
uid=988(mysql) gid=982(mysql) 組=982(mysql)

③安裝客戶端程式

rpm -ivh /opt/MySQL-client-5.5.52-1.el6.x86_64.rpm

④啟動MySQL服務

systemctl start mysql.service

⑤設定root使用者密碼

注意:這裡的root使用者是MySQL的root使用者,不是Linux的root使用者

[root@apple opt]# mysqladmin -u root password
New password: 
Confirm new password:

⑥登入MySQL

[root@apple opt]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.52 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

⑦客戶端登入MySQL伺服器

[1]被防火牆攔截的錯誤提示

通過關閉防火牆伺服器解決

systemctl stop firewalld.service
systemctl disable firewalld.service

[2]被MySQL自己拒絕連線

[3]在MySQL伺服器端設定允許任何主機地址訪問

執行SQL語句

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'atguigu' WITH GRANT OPTION;

重啟MySQL服務

[root@apple opt]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.52 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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;
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 host,user,password from user;
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *453FDE92DF58E2DE1A51D27869CF3F1A69984B1B |
| apple     | root |                                           |
| 127.0.0.1 | root |                                           |
| ::1       | root |                                           |
| localhost |      |                                           |
| apple     |      |                                           |
+-----------+------+-------------------------------------------+
6 rows in set (0.01 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'atguigu' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

mysql> select host,user,password,select_priv,update_priv from user;
+-----------+------+-------------------------------------------+-------------+-------------+
| host      | user | password                                  | select_priv | update_priv |
+-----------+------+-------------------------------------------+-------------+-------------+
| localhost | root | *453FDE92DF58E2DE1A51D27869CF3F1A69984B1B | Y           | Y           |
| apple     | root |                                           | Y           | Y           |
| 127.0.0.1 | root |                                           | Y           | Y           |
| ::1       | root |                                           | Y           | Y           |
| localhost |      |                                           | N           | N           |
| apple     |      |                                           | N           | N           |
| %         | root | *453FDE92DF58E2DE1A51D27869CF3F1A69984B1B | Y           | Y           |
+-----------+------+-------------------------------------------+-------------+-------------+
7 rows in set (0.00 sec)

重啟MySQL服務

systemctl restart mysql.service

⑧解決字元亂碼問題

[1]檢視字元相關變數

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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

[2]準備MySQL配置檔案

cp /usr/share/mysql/my-small.cnf /etc/my.cnf
[mysqld]
port            = 3306
socket          = /var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K
character-set-server=utf8

在[mysqld]部分的配置的最後新增character-set-server=utf8

重啟MySQL服務

systemctl restart mysql.service

檢視字符集相關變數

mysql> show variables like "%char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

重新建立資料庫、建立資料庫表、插入中文字元資料驗證。