Amazon RDS多库连接异常:PHP(CakePHP3.9)二次连接报1045错误
背景
在Amazon RDS实例中创建了多个数据库,Elastic Beanstalk环境下通过PHP(CakePHP 3.9)访问这些数据库原本均无问题。实例拥有一个可访问所有数据库的主用户,同时每个数据库也配置了独立用户。
问题表现
最初误以为是新创建的数据库在PHP连接时返回1045权限错误,但使用相同凭据可通过SequelPro等数据库管理工具正常连接,且主用户与该数据库独立用户均出现此错误。同一实例下其他数据库均可正常访问,仅该新库(后续排查排除特定库因素)出现异常,错误日志如下:
default-log-channel: ERROR: 2022-09-15 04:19:57 Error: [PDOException] SQLSTATE[HY000] [1045] Access denied for user 'myuser'@'172.61.15.102' (using password: YES)
配置对比
报错的数据库连接配置
DSN:mysql:host=project-testing-1.xxxxx.eu-west-2.rds.amazonaws.com;port=3306;dbname=my_db;charset=utf8
array (size=15) 'driver' => string 'Cake\Database\Driver\Mysql' (length=26) 'persistent' => boolean false 'host' => string 'project-testing-1.xxxxx.eu-west-2.rds.amazonaws.com' (length=55) 'port' => string '3306' (length=4) 'username' => string 'root' (length=13) 'password' => string 'xxxx^sdsdsdsd@j&sdsd&' (length=30) 'database' => string 'my_db' (length=17) 'encoding' => string 'utf8' (length=4) 'timezone' => string '+0:00' (length=5) 'cacheMetadata' => boolean true 'log' => boolean false 'quoteIdentifiers' => boolean false 'name' => string 'my_db' (length=17) 'flags' => array (size=3) 12 => boolean false 1000 => boolean true 3 => int 2 'init' => array (size=2) 0 => string 'SET time_zone = '+0:00'' (length=23) 1 => string 'SET NAMES utf8' (length=14)
可正常连接的数据库配置
DSN:mysql:host=project-testing-1.xxxxx.eu-west-2.rds.amazonaws.com;port=3306;dbname=testing-db;charset=utf8
array (size=15) 'driver' => string 'Cake\Database\Driver\Mysql' (length=26) 'persistent' => boolean false 'host' => string 'project-testing-1.xxxxx.eu-west-2.rds.amazonaws.com' (length=55) 'port' => string '3306' (length=4) 'username' => string 'root' (length=15) 'password' => string 'xxxx^sdsdsdsd@j&sdsd&' (length=40) 'database' => string 'testing-db' (length=24) 'encoding' => string 'utf8' (length=4) 'timezone' => string '+0:00' (length=5) 'cacheMetadata' => boolean true 'log' => boolean false 'quoteIdentifiers' => boolean false 'name' => string 'testing-db' (length=7) 'flags' => array (size=3) 12 => boolean false 1000 => boolean true 3 => int 2 'init' => array (size=2) 0 => string 'SET time_zone = '+0:00'' (length=23) 1 => string 'SET NAMES utf8' (length=14)
排查更新
经进一步排查,问题并非特定数据库导致,而是第二次数据库连接时必现1045权限错误,首次连接始终正常,即使使用root用户也会报错。相关DSN示例:
mysql://root:xxxxxxx@project-testing-1.xxxxx.eu-west-2.rds.amazonaws.com/testing-db
请求协助
请帮忙排查此问题。
内容的提问来源于stack exchange,提问作者R0bertinski

