gitlab ci: mysql 构建和恢复数据库转储

gitlab ci: mysql build and restore db dump

在 gitlab.yml 我有

mysql_build:
  stage: build
  variables:
    MYSQL_DATABASE: rates
    MYSQL_ROOT_PASSWORD: root
  services:
  - mysql:latest
  image: mysql
  before_script:
  - mysql --version
  script:
  - echo "SELECT 'OK';" | mysql --user=root --password="${MYSQL_ROOT_PASSWORD}" --host=mysql "${MYSQL_DATABASE}"
  - mysql --user=root --password="${MYSQL_ROOT_PASSWORD}" rates < db/rates_db.sql

安装 mysql 成功,但我想恢复 sql 转储文件以便在下一阶段访问它。

开始这部分时,我有异常:

 - mysql --user=root --password="${MYSQL_ROOT_PASSWORD}" rates < db/rates_db.sql

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

真实应用位于此处

https://gitlab.com/armdev/exchange-rates

如何恢复 mysql 中的数据库?

Initializing a fresh instance

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

试试这个

Docker 文件

FROM mysql

ENV MYSQL_DATABASE rates
ENV MYSQL_ROOT_PASSWORD root

COPY db/rates_db.sql /docker-entrypoint-initdb.d/

替换成gitlab-ci.yml

mysql_build:
  stage: build
  script:
    - docker build . -t <Image name>