第3次系统综合实践

完成Docker-compose的安装

dockerfile的编写

按照LNMP编写

编写deault.conf配置文件

1.nginx

FROM nginx
MAINTAINER by zyj(278476914@qq.com)
EXPOSE 1106

2.php

FROM php:7.4-fpm
MAINTAINER by zyj(278476914@qq.com)
FROM php:7.4-fpm
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

3.mysql

FROM mysql
作者信息
MAINTAINER by zyj(278476914@qq.com)
设置root密码
ENV MYSQL_ROOT_PASSWORD 123456
设置不可免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD no

4.phpmyadmin

FROM phpmyadmin/phpmyadmin            
MAINTAINER by zyj(278476914@qq.com)

EXPOSE 8080

docker-compose的编写

version: "3"
services:
 nginx:
    image: mynginx_image                #指定镜像名
    container_name: mynginx             #指定容器名
    build:
      context: .
      dockerfile: dockerfile_nginx      #指定dockerfile文件
    ports:
      - "80:1106"                       #修改端口映射
    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            #设置好环境变量,在php代码中使用变量名直接引用
    volumes:
      - ./web:/web/php                  #挂载工作目录到本机web目录

 mysql:
   image: mysql_image 
   container_name: mysql
   build:
      context: .
      dockerfile: dockerfile_mysql
   ports:
     - "3306:3306"
   volumes:
     - ./mysql_data:/var/lib/mysql       #挂载容器卷,实现数据同步,防止数据丢失

 phpmyadmin:
    image: myphpmyadmin_image
    container_name: myphpmyadmin
    build: 
      context: .
      dockerfile: dockerfile_phpmyadmin
    ports: 
      - "8080:80" # phpmyadmin默认监听80
    environment:
      PMA_HOST: mysql                     #指定mysql服务所在的host

查看docker-compose文件夹的树状结构

执行docker-compose文件

sudo docker-compose up -d --build

查看镜像和容器


访问localhost/index.html和localhost/index.php


服务测试

1.测试php能不能连接mysql数据库

修改index.php文件

<?php
$servername = "mysql";    #注意这里要用自己自定义的Mysql的容器名
$username = "root";
$password = "123456";     #密码也可以直接使用yml中的变量名
 
try {
    $conn = new PDO("mysql:host=$servername", $username, $password);
    echo "连接成功"; 
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

2.在index.php文件中创建数据库

<?php
$servername = "mysql";
$username = "root";
$password = "123456";
 
try {
    $conn = new PDO("mysql:host=$servername", $username, $password);
    echo "连接成功"; 

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "CREATE DATABASE myDBPDO";

    // 使用 exec() ,因为没有结果返回
    $conn->exec($sql);

    echo "数据库创建成功<br>";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

3.进去mysql容器查询数据库可以发现已经创建了 myDBPDO数据库

4.在myDBPDO数据库中创建Student表


可以发现已经创建的student表

5.向student表中插入数据

<?php
$servername = "mysql";
$username = "root";
$password = "123456";
$dbname="myDBPDO";
 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // 开始事务
    $conn->beginTransaction();
    // SQL 语句
    $conn->exec("INSERT INTO Student (id,name) 
    VALUES (031702336, 'Ljw')");
    $conn->exec("INSERT INTO Student (id,name) 
    VALUES (031702340, 'Zyj')");
    $conn->exec("INSERT INTO Student (id,name) 
    VALUES (031702341, 'Hbh')");
 
    // 提交事务
    $conn->commit();
    echo "新记录插入成功";
}
catch(PDOException $e)
{
    // 如果执行失败回滚
    $conn->rollback();
    echo $sql . "<br>" . $e->getMessage();
}
 
$conn = null;
?>


6.删除与修改数据

<?php
$servername = "mysql";
$username = "root";
$password = "123456";
$dbname="myDBPDO";
 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // 开始事务
    $conn->beginTransaction();
    // SQL UPDATE 语句
    $conn->exec("UPDATE Student SET name='fzl' WHERE id=031702336");
    //SQL  DELETE语句
    $conn->exec("DELETE FROM Student where id=031702341");
    // 提交事务
    $conn->commit();
    echo "修改,删除成功";
}
catch(PDOException $e)
{
    // 如果执行失败回滚
    $conn->rollback();
    echo $sql . "<br>" . $e->getMessage();
}
 
$conn = null;
?>


遇到的问题


网络连接用问题,从用手机直连共享网络换成家里常用的wifi就解决了

posted @ 2020-05-07 11:07  夜色怡人、  阅读(260)  评论(0编辑  收藏  举报