Laravel 11升级后Bitbucket流水线无法找到MySQL驱动求助
解决Laravel 11升级后Bitbucket流水线MySQL驱动缺失问题
我们的Bitbucket构建流水线在Laravel 9-10版本可正常运行,升级到Laravel 11后执行迁移/测试时提示找不到MySQL数据库驱动,报错如下:
In Connection.php line 825: could not find driver (Connection: mysql, SQL: select exists (select 1 from information_schema.tables where table_schema = 'homestead' and table_name = 'migrations' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED')) as `ex ists`) In Connector.php line 66: could not find driver
此前Laravel9-10可用的bitbucket-pipelines.yml配置如下:
image: php:8.2-fpm definitions: services: mysql: image: mysql:8.0 environment: MYSQL_DATABASE: 'homestead' MYSQL_RANDOM_ROOT_PASSWORD: 'yes' MYSQL_USER: 'homestead' MYSQL_PASSWORD: 'secret' steps: - step: &install-dependencies name: Setup caches: - composer - node script: - docker-php-ext-install pcntl - apt-get update && apt-get install -qy git curl unzip - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer - composer install --no-progress --no-scripts - curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - - apt-get install -y nodejs - npm i --no-progress - npm run build artifacts: - vendor/** - node_modules/** - public/** .scripts: - &setup-tests | apt-get update && apt-get install -qy libmcrypt-dev default-mysql-client echo "memory_limit = 256M" > $PHP_INI_DIR/conf.d/php-memory-limits.ini cp .env.pipelines .env php artisan key:generate php artisan storage:link pipelines: # default pipeline branches: '{develop,staging,master}': - step: *install-dependencies - parallel: - step: name: Unit script: - *setup-tests - php artisan test tests/Unit services: - mysql - step: name: Feature script: - *setup-tests - php artisan test tests/Feature services: - mysql
本地环境可正常运行测试,推测问题出在Bitbucket使用的PHP镜像缺少MySQL驱动扩展。
解决方案
修改.scripts中的setup-tests脚本,添加安装pdo_mysql扩展的命令:
.scripts: - &setup-tests | apt-get update && apt-get install -qy libmcrypt-dev default-mysql-client docker-php-ext-install pdo_mysql echo "memory_limit = 256M" > $PHP_INI_DIR/conf.d/php-memory-limits.ini cp .env.pipelines .env php artisan key:generate php artisan storage:link
说明
docker-php-ext-install pdo_mysql是官方PHP镜像中安装MySQL PDO驱动的标准命令,确保PHP能正常连接MySQL数据库。- 如果需要同时支持mysqli扩展,可将命令改为
docker-php-ext-install pdo_mysql mysqli。
内容的提问来源于stack exchange,提问作者Stetzon
相关产品推荐
相关产品推荐

