You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.8部署至Azure Web Apps+MySQL数据库连接权限问题求助

Fixing Laravel 5.8 Database Connection Errors on Azure Web Apps with MySQL-in-App

Great to hear your static pages are working, and that you've confirmed the raw PHP connection works—this narrows the issue down to Laravel not correctly picking up Azure's MySQL connection details. Let's get this sorted:

1. Update Your Database Configuration to Parse Azure's Environment Variables

Instead of hardcoding values in .env or config/database.php, we'll have Laravel dynamically read the connection string Azure injects into the server environment. Open config/database.php and modify the mysql connection block like this:

// Grab the MySQL connection string from Azure's environment variables
$mysqlConnStr = collect($_SERVER)->filter(function ($value, $key) {
    return strpos($key, 'MYSQLCONNSTR_') === 0;
})->first();

// Parse the connection string into individual components
$dsn = preg_replace('/^.*Data Source=(.+?);.*$/', "\\1", $mysqlConnStr);
[$dbHost, $dbPort] = explode(':', $dsn);
$dbName = preg_replace('/^.*Database=(.+?);.*$/', "\\1", $mysqlConnStr);
$dbUser = preg_replace('/^.*User Id=(.+?);.*$/', "\\1", $mysqlConnStr);
$dbPass = preg_replace('/^.*Password=(.+?)$/', "\\1", $mysqlConnStr);

return [
    // ... keep other existing config sections
    'connections' => [
        // ... other connections (sqlite, pgsql, etc.)
        'mysql' => [
            'driver' => 'mysql',
            'host' => $dbHost,
            'port' => $dbPort ?? '3306',
            'database' => $dbName,
            'username' => $dbUser,
            'password' => $dbPass,
            'unix_socket' => '',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
    ],
    // ... rest of the config
];

This ensures Laravel uses the exact connection details Azure provides, no manual .env edits needed.

2. Clear Laravel's Configuration Cache

Laravel caches config files for performance, so we need to clear that cache to apply our changes. Head to the Kudu console (https://<your-app-name>.scm.azurewebsites.net/) and run these commands:

php artisan config:clear
php artisan cache:clear

If you have a deployment script set up, add these commands to it so the cache is automatically cleared on each deploy.

3. Verify Environment Variable Priority

Azure's injected environment variables take precedence over values in your .env file. You can safely remove the DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, and DB_PASSWORD lines from .env to avoid any conflicts—Laravel will use the dynamically parsed values instead.

4. Test Your Database Operations

Now try running your migration again via Kudu or your deployment pipeline:

php artisan migrate

This should connect successfully and run your migrations without the SQLSTATE[HY000] [1045] error.


内容的提问来源于stack exchange,提问作者Ricardo Matos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:46:59