繁体   English   中英

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

[英]GitLab CI with docker and laravel cannot connect to database

我有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

我猜PHP7.1容器内的Laravel看不到MySQL容器正在查看错误消息? 在gitlab.yml中将其声明为服务还不够吗?

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

services:
  - mysql:5.7

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

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]

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM