Bitbucket 管道导入 mysql 架构

Bitbucket pipeline import mysql schema

我正在尝试通过以下语句将数据库架构导入 mysql 服务

mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE < DB_Schema.sql

它 return mysql: 未找到。我什至尝试了以下命令

docker exec -i mysql mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE < DB_Schema.sql

即使收到错误 + docker exec -i mysql mysql --user=$DB_USERNAME --密码=$DB_PASSWORD 5i < DB_Schema.sql 错误:没有这样的容器:mysql

使用 mysql 的最佳方式是什么,以便我可以将 DB 的姿态导入其中以进行测试以及如何使用?

请在下面找到 .yml 文件。

# This is a sample build configuration for PHP.
# Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----

# Specify a docker image from Docker Hub as your build environment.
# All of your pipeline scripts will be executed within this docker image.

image: php:8.0-fpm-alpine

# All of your Pipelines will be defined in the `pipelines` section.
# You can have any number of Pipelines, but they must all have unique
# names. The default Pipeline is simply named `default`.

pipelines:
  default:
  # Each Pipeline consists of one or more steps which each execute
  # sequentially in separate docker containers.
  # name: optional name for this step
  # script: the commands you wish to execute in this step, in order
    - parallel:
      - step:
          name: Installing Dependancies and Composer
          caches:
            - composer
          script:
            # Your Pipeline automatically contains a copy of your code in its working
            # directory; however, the docker image may not be preconfigured with all
            # of the PHP/Laravel extensions your project requires. You may need to install
            # them yourself, as shown below.
            - apt-get update && apt-get install -qy git curl libmcrypt-dev unzip libzip-dev libpng-dev zip git gnupg gnupg2 php-mysql
            - docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg --with-webp && \
            - docker-php-ext-install gd && \
            - docker-php-ext-install exif && \
            - docker-php-ext-install zip && \
            - docker-php-ext-install pdo pdo_mysql
            - rm -rf ./vendor
            - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
            - composer install --ignore-platform-reqs
            - composer dump-autoload
            # Here we create link between the .env.pipelines file and the .env file
            # so that our database can retreieve all the variables inside .env.pipelines
            - ln -f -s .env.pipelines .env
          artifacts:
            - vendor/**

      - step:
          name: Installing and Running npm
          image: node:16
          caches:
            - node
          script:
            - npm install -g grunt-cli
            - npm install
            - npm run dev
          artifacts:
            - node_modules/**

    - step:
        name: Running Test
        deployment: local
        script:
          # Start up the php server so that we can test against it
          - php artisan serve &
          # # Give the server some time to start
          - sleep 5
          # - php artisan migrate
          - docker ps
          - docker container ls
          - mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD $DB_DATABASE < DB_Schema.sql
          # - docker exec -i mysql mysql -h 127.0.0.1 -u $DB_USERNAME -p$DB_PASSWORD -e "SHOW DATABASES"
          - php artisan optimize
          - php artisan test
        services:
          - mysql
          - docker
          
# You might want to create and access a service (like a database) as part
# of your Pipeline workflow. You can do so by defining it as a service here.
definitions:
  services:
    mysql:
      image: mysql:latest
      environment:
        MYSQL_DATABASE: $DB_DATABASE
        MYSQL_USER: $DB_USERNAME
        MYSQL_PASSWORD: $DB_PASSWORD
        MYSQL_ROOT_PASSWORD: $DB_PASSWORD
        SERVICE_TAGS: mysql
        SERVICE_NAME: mysql

您不能 install/update/change 在第一步中使用主图像,以便它们在最后一步中出现。使用所有这些安装制作您的自定义 Docker 图像,这将使 运行 管道更快,并允许您在管道中使用您需要的其他工具。

我更喜欢在外部 Docker使用“mysql”客户端,并让它根据port 映射设置。然后,从概念上讲,它就像在单独的“服务器”上读取“mysqld”服务器。

LOAD DATA INFILEINSERT,包括 mysql ... < dump.sql 的使用都很好。