一、学习使用docker,并完成以下任务
    完成这个任务前现在自己机器上创建一个http服务,感受下这个服务的含义。

    1. 利用docker在单机上创建两个httpd服务,能够提供web服务。分别是8081端口和8082端口。在8081服务的根目录上放置readme.html文件。在8082服务的根目录上放置readme.html文件,这两个html文件要有区别。 里面内容可以随便写(比如8081这个写abc,8082这个写123)。

    创建两个html文件

    1. [root@localhost exam2]# echo "abc" >> readme1.html
    2. [root@localhost exam2]# echo "123" >> readme2.html

    拉取 httpd docker 镜像,并且获取镜像中的httpd的配置

    1. [root@localhost exam2]# docker pull httpd
    2. [root@localhost exam2]# docker run --rm httpd:2.4 cat /usr/local/apache2/conf/httpd.conf > my-httpd.conf

    修改 my-httpd.conf , 添加内容

    1. Listen 80
    2. Listen 81
    3. <VirtualHost *:80>
    4. DocumentRoot /usr/local/apache2/htdocs/home1
    5. ServerName localhost:80
    6. </VirtualHost>
    7. <VirtualHost *:81>
    8. DocumentRoot /usr/local/apache2/htdocs/home2
    9. ServerName localhost:81
    10. </VirtualHost>

    创建Dockerfile 内容为

    1. FROM httpd:2.4
    2. COPY ./my-httpd.conf /usr/local/apache2/conf/httpd.conf
    3. COPY ./readme1.html /usr/local/apache2/htdocs/home1/readme.html
    4. COPY ./readme2.html /usr/local/apache2/htdocs/home2/readme.html

    编译Dockerfile并运行

    1. [root@localhost exam2]# docker build -t my-apache2 .
    2. [root@localhost exam2]# docker run -dit -p 8081:80 -p 8082:81 my-apache2

    查看8080端口和8081端口的内容

    1. [root@localhost exam2]# curl localhost:8081/readme.html
    2. abc
    3. [root@localhost exam2]# curl localhost:8082/readme.html
    4. 123
    1. iptables封掉8081端口。使得http://127.0.0.1:8081/readme.html 无法访问而http://127.0.0.1:8082/readme.html 可以访问。

    添加新的规则,drop所有目的地为8081 端口的请求

    1. [root@localhost exam2]# iptables -t filter -I INPUT -p tcp --dport 8081 -j DROP
    2. [root@localhost exam2]# iptables -L DOCKER-USER -n --line-number
    3. Chain DOCKER-USER (1 references)
    4. num target prot opt source destination
    5. 1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8081
    6. 2 RETURN all -- 0.0.0.0/0 0.0.0.0/0

    验证是否封掉8081端口

    1. [root@localhost ~]# curl http://127.0.0.1:8081/readme.html --connect-timeout 1
    2. curl: (28) Connection timed out after 1002 milliseconds
    3. [root@localhost ~]# curl http://127.0.0.1:8082/readme.html --connect-timeout 1
    4. 123
    1. iptables 做流量均衡(nat),将所有80端口导入的流量50%分配给8081端口,50%的流量分配给8082端口。配置后,刷10次http://127.0.0.1:80/readme.html 次。如果4~5次结果是abc,另外的显示123。说明配置正确。
    1. [root@localhost ~]# iptables -t nat -I PREROUTING 1 -p tcp --dport 80 -j REDIRECT --to-ports 8082 -m statistic --mode random --probability 0.5
    2. [root@localhost ~]# iptables -t nat -I PREROUTING 1 -p tcp --dport 80 -j REDIRECT --to-ports 8081 -m statistic --mode random --probability 0.5

    二:安装mysql数据库,并完成以下任务

    1. 安装好后,创建自己的数据库test,然后创建一个表,字段是id(整形),name(255个字符长度的字符串型),ts(时间戳型,默认为当前时间戳),并手动写入三行记录,并删除其中的一条记录,查询是否正确删除,然后利用mysqldump 命令将剩余两条记录的数据库表dump出来,dump的文件叫test.sql, 然后vim test.sql 看看这个文件长什么样子。写出相应的命令。
    1. [root@localhost ~]# mysql -uroot -p
    2. mysql> CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8;
    3. Query OK, 1 row affected (0.00 sec)
    4. mysql> use test
    5. Database changed
    6. mysql> CREATE TABLE IF NOT EXISTS `test` (
    7. -> id INT,
    8. -> name VARCHAR(255),
    9. -> ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    10. -> );
    11. Query OK, 0 rows affected (0.02 sec)
    12. mysql> desc test;
    13. +-------+--------------+------+-----+-------------------+-------+
    14. | Field | Type | Null | Key | Default | Extra |
    15. +-------+--------------+------+-----+-------------------+-------+
    16. | id | int(11) | YES | | NULL | |
    17. | name | varchar(255) | YES | | NULL | |
    18. | ts | timestamp | NO | | CURRENT_TIMESTAMP | |
    19. +-------+--------------+------+-----+-------------------+-------+
    20. 3 rows in set (0.03 sec)
    21. mysql> INSERT INTO test (id, name, ts) VALUES (1, 'user1', 20021114), (2, 'user2', 20181011), (3, 'user3', 20200301);
    22. Query OK, 3 rows affected (0.00 sec)
    23. Records: 3 Duplicates: 0 Warnings: 0
    24. mysql> DELETE FROM test where id = 2;
    25. Query OK, 1 row affected (0.00 sec)
    26. mysql> select * from test;
    27. +------+-------+---------------------+
    28. | id | name | ts |
    29. +------+-------+---------------------+
    30. | 1 | user1 | 2002-11-14 00:00:00 |
    31. | 3 | user3 | 2020-03-01 00:00:00 |
    32. +------+-------+---------------------+
    33. 2 rows in set (0.00 sec)

    利用mysqldump 命令将剩余两条记录的数据库表dump出来,-n的目的

    1. [root@localhost ~]# mysqldump -uroot -p --databases test --tables test > test.sql

    test.sql 文件内容

    1. [root@localhost ~]# cat test.sql
    2. -- MySQL dump 10.13 Distrib 5.7.29, for Linux (x86_64)
    3. --
    4. -- Host: localhost Database: test
    5. -- ------------------------------------------------------
    6. -- Server version 5.7.29
    7. /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    8. /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    9. /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    10. /*!40101 SET NAMES utf8 */;
    11. /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
    12. /*!40103 SET TIME_ZONE='+00:00' */;
    13. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    14. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    15. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    16. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    17. --
    18. -- Table structure for table `test`
    19. --
    20. DROP TABLE IF EXISTS `test`;
    21. /*!40101 SET @saved_cs_client = @@character_set_client */;
    22. /*!40101 SET character_set_client = utf8 */;
    23. CREATE TABLE `test` (
    24. `id` int(11) DEFAULT NULL,
    25. `name` varchar(255) DEFAULT NULL,
    26. `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
    27. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    28. /*!40101 SET character_set_client = @saved_cs_client */;
    29. --
    30. -- Dumping data for table `test`
    31. --
    32. LOCK TABLES `test` WRITE;
    33. /*!40000 ALTER TABLE `test` DISABLE KEYS */;
    34. INSERT INTO `test` VALUES (1,'user1','2002-11-14 05:00:00'),(3,'user3','2020-03-01 05:00:00');
    35. /*!40000 ALTER TABLE `test` ENABLE KEYS */;
    36. UNLOCK TABLES;
    37. /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
    38. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    39. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    40. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    41. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    42. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    43. /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    44. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
    45. -- Dump completed on 2020-04-10 8:42:13
    1. 将下面mysqldump出来的数据库表weibo.sql还原到数据库中,http://117.50.100.6:12345/linux/weibo.sql。并写出语句查询这个表的记录数。写出相应的过程。
    1. [root@localhost ~]# mysql -uroot -p test < weibo.sql
    2. [root@localhost ~]# mysql -uroot -p
    3. mysql> use test;
    4. mysql> select count(1) from fresh_data;
    5. +----------+
    6. | count(1) |
    7. +----------+
    8. | 500 |
    9. +----------+
    10. 1 row in set (0.00 sec)
    1. 这个数据库表中有一个字段叫user_type表明微博用户的类型,找到其中是黄V的用户,并用mysqldump语句将记录中的user_type是黄V的记录导出来。(带条件的mysqldump),导出后vim 这个dump文件看看是否正确。
    1. mysql> select distinct user_type from fresh_data;
    2. +--------------+
    3. | user_type |
    4. +--------------+
    5. | 普通用户 |
    6. | 达人 |
    7. | V |
    8. | 微博女郎 |
    9. | V |
    10. | V |
    11. +--------------+
    12. 6 rows in set (0.01 sec)
    13. [root@localhost ~]# mysqldump -uroot -p --databases test --tables fresh_data --where='user_type="黄V"' > test.sql