Symfony5项目执行命令时PDO MySQL驱动未找到问题求助
问题:Symfony5 命令行执行会员导入时出现PDO驱动未找到错误
场景说明
某会员管理Web应用支持通过XLS文件仓库导入会员数据并保存至数据库。导入命令在生产环境后台自动运行,开发环境需手动执行,仅处理状态为“待导入”的文件。手动执行命令时触发PDO驱动未找到的错误。
错误信息
In AbstractMySQLDriver.php line 128: An exception occurred in driver: could not find driver In Exception.php line 18: could not find driver In PDOConnection.php line 40: could not find driver
Symfony5 config.yml 配置
doctrine: dbal: driver: pdo_mysql host: '%database_host%' port: '%database_port%' dbname: '%database_name%' user: '%database_user%' password: '%database_password%' charset: UTF8 types: phone_number: Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType server_version: "%doctrine_server_version%" options: 1002: 'SET sql_mode=(SELECT REPLACE(@@sql_mode, "ONLY_FULL_GROUP_BY", ""))' orm: auto_generate_proxy_classes: '%kernel.debug%' naming_strategy: doctrine.orm.naming_strategy.underscore auto_mapping: true dql: string_functions: MONTH: DoctrineExtensions\Query\Mysql\Month YEAR: DoctrineExtensions\Query\Mysql\Year regexp: DoctrineExtensions\Query\Mysql\Regexp replace: DoctrineExtensions\Query\Mysql\Replace DATE_FORMAT: DoctrineExtensions\Query\Mysql\DateFormat mappings: gedmo_tree: type: annotation prefix: Gedmo\Tree\Entity dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity" alias: GedmoTree # (optional) it will default to the name set for the mapping is_bundle: false
可能的问题原因
- PHP CLI环境缺失PDO MySQL驱动:Web环境和CLI环境使用的PHP配置通常相互独立,即使Web端能正常连接数据库,CLI模式下可能未安装pdo_mysql扩展。执行
php -m命令查看已加载扩展列表,确认是否包含pdo_mysql。 - PHP CLI配置未启用pdo_mysql:检查CLI对应的php.ini文件(路径通常与Web环境的php.ini不同),确认
extension=pdo_mysql一行未被注释,且扩展文件路径配置正确。 - 依赖未正确加载:通过Composer确认doctrine/dbal等核心依赖已完整安装,可执行
composer install重新加载依赖包。 - 环境变量加载异常:开发环境CLI执行命令时,可能未正确加载数据库配置相关的环境变量,但此场景下错误明确指向驱动问题,优先级低于前三点。
内容的提问来源于stack exchange,提问作者Borloo
相关产品推荐
相关产品推荐

