zoukankan      html  css  js  c++  java
  • 2020系统综合实践 第3次实践作业

    1.完成Docker-compose的安装

    • 下载Docker Compose的当前稳定版本
        curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    
    • 将可执行权限应用于二进制文件
          chmod +x /usr/local/bin/docker-compose
    
    • 创建指向/usr/bin目录的符号链接
          ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
    
    • 测试安装
          docker-compose --version
    

    2.Dockerfile编写

    • 目录结构
    • dockerfile_nginx
          #基础镜像
          FROM nginx
          #作者信息
          MAINTAINER fwh<https://www.cnblogs.com/fwhdbk/>
          #声明暴露的端口
          EXPOSE 2400
    
    • dockerfile_mysql
          #基础镜像
          FROM mysql
          #作者信息
          MAINTAINER fwh<https://www.cnblogs.com/fwhdbk/>
          #设置root密码
          ENV MYSQL_ROOT_PASSWORD 123456
          #设置不可免密登录
          ENV MYSQL_ALLOW_EMPTY_PASSWORD no
    
    • dockerfile_php
        #基础镜像
        FROM php:7.4-fpm
        #作者信息
        MAINTAINER fwh<https://www.cnblogs.com/fwhdbk/>
        RUN apt-get update && apt-get install -y 
                    libfreetype6-dev 
                    libjpeg62-turbo-dev 
                    libpng-dev 
              && docker-php-ext-install pdo_mysql 
              && docker-php-ext-configure gd --with-freetype --with-jpeg 
              && docker-php-ext-install -j$(nproc) gd
    
    • index.html
          Wecome
    
    • index.php
          <?php phpinfo();?>
    
    • default.conf
        server {
              listen       2400;        #修改映射端口
              server_name  localhost;
    
              location / {
                  root   /web/html;     #修改工作目录
                  index  index.html index.htm;
              }
    
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
            root   /usr/share/nginx/html;
              }
          
              location ~ .php$ {
                  root           /web/php;          #修改工作目录
                  fastcgi_pass   myphp:9000;        #修改为容器名
                  fastcgi_index  index.php;
                  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
                  include        fastcgi_params;
              }
          }
    
    • docker-compose.yml
          version: "3"
          services:
              nginx:
                    image: mynginx_image                #指定镜像名
                    container_name: mynginx             #指定容器名
                    build:
                          context: .
                          dockerfile: dockerfile_nginx      #指定dockerfile文件
                    ports:
                          - "80:2400"                       #修改端口映射
                    volumes:
                          - ./web:/web/html               
                          - ./default.conf:/etc/nginx/conf.d/default.conf     #配置文件
              php:
                    image: myphp_image 
                    container_name: myphp 
                    build:
                          context: .
                          dockerfile: dockerfile_php 
                    environment:
                          MYSQL_PASSWORD: 123456           
                    volumes:
                          - ./web:/web/php                
    
              mysql:
                    image: mysql_image 
                    container_name: mysql
                    build:
                          context: .
                          dockerfile: dockerfile_mysql
                    ports:
                          - "3306:3306"
                    volumes:
                          - ./mysql_data:/var/lib/mysql       
    
    

    3.使用Compose实现多容器运行机制

    • 运行docker-compose
          docker-compose up -d --build
    

    • 查看容器和镜像
    • 访问

    4.服务测试

    • 连接测试
      • 修改index.php
              <?php
              $servername = "mysql";
              $username = "username";
              $password = "123456";
      
              try {
                  $conn = new PDO("mysql:host=$servername;", $username, $password);
                  echo "连接成功"; 
              }
              catch(PDOException $e)
              {
                  echo $e->getMessage();
              }
              ?>
      
    • 创建数据库
      • 修改index.php
      	<?php
      	$servername = "mysql";
      	$username = "root";
      	$password = "123456";
      
      	try {
      	    $conn = new PDO("mysql:host=$servername", $username, $password);
      
      	    // 设置 PDO 错误模式为异常
      	    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      	    $sql = "CREATE DATABASE test";
      
      	    // 使用 exec() ,因为没有结果返回
      	    $conn->exec($sql);
      
      	    echo "数据库创建成功<br>";
      	}
      	catch(PDOException $e)
      	{
      	    echo $sql . "<br>" . $e->getMessage();
      	}
      	$conn = null;
      	?>
      
      • 查看数据库信息
            docker exec -it mysql /bin/bash
            mysql -u root -p
            show databases;
      
    • 创建数据表
      • 修改index.php
                  <?php
      	$servername = "mysql";
      	$username = "root";
      	$password = "123456";
      	$dbname="test";    
      
      	try {
      	    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      	    // 设置 PDO 错误模式,用于抛出异常
      	    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      	    // 使用 sql 创建数据表
      	    $sql = "CREATE TABLE test (
      	 	id bigint(20) NOT NULL,
      	 	name varchar(255) NOT NULL,
      	 	email varchar(255) NOT NULL
      		)";
      	    // 使用 exec() ,没有结果返回 
      	    $conn->exec($sql);
      	    echo "数据表 user 创建成功";
      	}
      	catch(PDOException $e)
      	{
      	    echo $sql . "<br>" . $e->getMessage();
      	}
      	$conn = null;
      	?>
      
      • 查看数据表
              use test
              show tables;
      
    • 插入数据
      • 修改index.php
      	<?php
      	$servername = "mysql";
      	$username = "root";
      	$password = "123456";
      	$dbname="test";    
      
      	try {
      	    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      	    // set the PDO error mode to exception
      	    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      
      	    // 开始事务
      	    $conn->beginTransaction();
      	    // SQL 语句
      	    $conn->exec("INSERT INTO test (`id`, `name`,`email`)
      		VALUES(100,'john','www.john@example.com');");
      	    $conn->exec("INSERT INTO test (`id`, `name`,`email`)
      		VALUES(102,'jack','www.jack@example.com');");
      
      	    // 提交事务
      	    $conn->commit();
      	    echo "新记录插入成功";
      	}    
      	catch(PDOException $e)
      	{
      	    // 如果执行失败回滚
      	    $conn->rollback();
      	    echo $sql . "<br>" . $e->getMessage();
      	}
      
      	$conn = null;
      	?>
      

    • 删除数据
      • 修改index.php
      	<?php
      	$servername = "mysql";
      	$username = "root";
      	$password = "123456";
      	$dbname="test";    
      
      	try {
      	    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      	    // set the PDO error mode to exception
      	    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      
      	    // 开始事务
      	    $conn->beginTransaction();
      	    // SQL 语句
      	    $conn->exec("delete from test where id=100");
      
      	    // 提交事务
      	    $conn->commit();
      	    echo "记录删除成功";
      	}    
      	catch(PDOException $e)
      	{
      	    // 如果执行失败回滚
      	    $conn->rollback();
      	    echo $sql . "<br>" . $e->getMessage();
      	}
      
      	$conn = null;
      	?>
      

    • 修改数据
      • 修改index.php
      	<?php
      	$servername = "mysql";
      	$username = "root";
      	$password = "123456";
      	$dbname="test";    
      
      	try {
      	    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
      	    // set the PDO error mode to exception
      	    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      
      	    // 开始事务
      	    $conn->beginTransaction();
      	    // SQL 语句
      	    $conn->exec("update test set id=666 where name='jack'");
      
      	    // 提交事务
      	    $conn->commit();
      	    echo "记录修改成功";
      	}    
      	catch(PDOException $e)
      	{
      	    // 如果执行失败回滚
      	    $conn->rollback();
      	    echo $sql . "<br>" . $e->getMessage();
      	}
      
      	$conn = null;
      	?>
      

    5.增加一个phpmyadmin容器,实现web端的数据库管理

    • 在docker-compose.yml文件中增加
        phpmyadmin:
            image: myphpmyadmin_image
            container_name: myphpmyadmin
            build: 
                context: .
                dockerfile: dockerfile_phpmyadmin
            ports: 
                - "8080:80" # phpmyadmin默认监听80
            environment:
                PMA_HOST: mysql                     #指定mysql服务所在的host
    
    • dockerfile_phpmyadmin
    	#基础镜像
    	FROM phpmyadmin/phpmyadmin            
    	#维护者信息
    	MAINTAINER fwh<https://www.cnblogs.com/fwhdbk/>
    	#暴露端口
    	EXPOSE 8080
    
    • 访问网址localhost:8080
      • 用root用户和yml中设置密码登录
      • 新建数据库
      • 查看表格
      • 修改表
      • 插入数据
      • 查询数据

    6.小结

  • 相关阅读:
    ArrayList源码解析
    HashSet的实现原理
    Spark Streaming之四:Spark Streaming 与 Kafka 集成分析
    Spark Streaming之三:DStream解析
    Spark Streaming之二:StreamingContext解析
    curl与wget区别
    MongoDB之二(增删查改)
    Thread之九:stop
    mysql实战优化之九:MySQL查询缓存总结
    PBE加密算法
  • 原文地址:https://www.cnblogs.com/fwhdbk/p/12828149.html
Copyright © 2011-2022 走看看