带有docker和laravel的GitLab CI无法连接到数据库

时间:2018-04-12 15:36:21

标签: php laravel docker gitlab

我有Gitlab CI设置,但在尝试运行数据库迁移时仍然失败:

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = test_db and table_name = migrations)    

我的gitlab.yml文件是:

before_script:
  - bash .gitlab-ci.sh
  - export APP_ENV=testing

services:
  - mysql:8.0

variables:
  MYSQL_DATABASE: test_db
  MYSQL_ROOT_PASSWORD: testdb
  DB_HOST: mysql
  DB_USERNAME: root
  DOCKER_DRIVER: overlay

cache:
  paths:
  - vendor/
  - node_modules/

stages:
  - test
  - deploy

phpunit:php7.1:mysql8.0:
  stage: test
  image: php:7.1
  services:
    - mysql:8.0
  script:
    - echo "Running PHP Unit - php 7.1 mysql 8.0"
    - php vendor/bin/phpunit --colors

deploy_production:
  stage: deploy
  script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

    - ~/.composer/vendor/bin/envoy run deploy
  environment:
    name: production
    url: http://167.99.202.64

  when: manual
  only:
    - master

我之前的bash脚本如下:

#!/bin/bash

# Install dependencies only for Docker.
[[ ! -e /.dockerinit ]] && [[ ! -e  /.dockerenv ]] && exit 0
set -xe

# Update packages and install composer and PHP dependencies.
apt-get update -yqq
apt-get install git libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev -yqq

# Compile PHP, include these extensions.
docker-php-ext-install mbstring mcrypt pdo_mysql curl json intl gd xml zip bz2 opcache

# Install Composer and project dependencies
echo 'Installing composer'
curl -sS https://getcomposer.org/installer | php
php composer.phar install --no-plugins --no-scripts --dev

# Copy over testing configuration.
cp .env.testing .env

# Generate an application key. Re-cache
php artisan key:generate
php artisan config:cache

# Run database migrations
php artisan migrate

最后我的env.testing文件:

APP_ENV=testing
APP_DEBUG=true
APP_KEY=key

DB_HOST=mysql
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=testdb

CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
MAIL_DRIVER=log

我猜测Laravel里面的PHP7.1容器看不到MySQL容器看错误信息?在gitlab.yml中将它声明为服务还不够吗?

3 个答案:

答案 0 :(得分:2)

最后,我遇到了同样的问题

service: -mysql:latest 

(因为构建失败的三周,因为最新的标签已移至版本8)

问题是:

MySQL 8

在7.1.16之前运行PHP版本或在7.2.4之前运行PHP 7.2时,将MySQL 8 Server的默认密码插件设置为mysql_native_password,否则您将看到类似于客户端未知的服务器请求的身份验证方法的错误[caching_sha2_password]即使没有使用caching_sha2_password。 [1]

答案 1 :(得分:1)

更新您的.gitlab-ci.yml文件并将mysql版本设置为5.7

services:
  - mysql:5.7

答案 2 :(得分:1)

您可以像这样重新声明MySql服务的入口点:

services:
        - name: mysql:8.0
          alias: mysql-server
          entrypoint: ['/entrypoint.sh', '--default-authentication-plugin=mysql_native_password']