Laravel部署cPanel后遇SQLSTATE[HY000][1044]权限错误求解决
Hey there, let’s troubleshoot this database access denied error together—it’s a super common issue when deploying Laravel projects to cPanel, and we’ll get it sorted out quickly.
Step 1: Verify Database User Permissions in cPanel
The most frequent culprit here is missing permissions for your database user. Here’s how to check:
- Log into your cPanel account and navigate to the MySQL Databases section.
- Locate the user
tfs_userand look for the section listing databases associated with this user. - Ensure
tfs_useris granted all necessary privileges (at minimumSELECT,INSERT,UPDATE,DELETE—or justALL PRIVILEGES) for thetfsdatabase. If not, use the "Add User to Database" tool to assign the correct permissions.
Step 2: Clear Laravel Configuration Cache
Laravel caches configuration files to boost performance, which means your updated .env settings might not be taking effect yet. To fix this:
- If you have SSH access to your cPanel server, run these commands in your project root directory:
php artisan config:clear php artisan cache:clear - If you don’t have SSH access, manually delete the files in the
bootstrap/cachedirectory (specificallyconfig.phpand any other cache files). This will force Laravel to read your fresh.envsettings.
Step 3: Double-Check Database Credentials (Watch for Prefixes!)
Many cPanel hosts automatically add your cPanel username as a prefix to databases and users. For example:
- If your cPanel username is
mycpanel, your database might actually bemycpanel_tfs(not justtfs). - Similarly, your username could be
mycpanel_tfs_userinstead oftfs_user.
Go back to the MySQL Databases section in cPanel to confirm the exact, full names of your database and user, then update your.envfile accordingly.
Step 4: Confirm the Database Host Address
Sometimes 127.0.0.1 doesn’t work with cPanel’s MySQL setup—try using localhost instead. Alternatively, check the MySQL Databases page for a specific host address (some hosts use something like db.yourdomain.com). Update DB_HOST in your .env file to match this address.
Step 5: Test the Connection Outside Laravel
To rule out Laravel-specific issues, create a simple test file named test_db.php in your project’s public directory with this code:
<?php $servername = "127.0.0.1"; // Replace with your actual DB host $username = "tfs_user"; // Replace with your actual DB user $password = "tfsdemo01"; // Replace with your actual DB password $dbname = "tfs"; // Replace with your actual DB name $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Database connection successful!"; $conn->close(); ?>
Visit this file in your browser (e.g., https://yourdomain.com/test_db.php). If it still throws an access denied error, the problem is definitely with your database user/permissions (not Laravel’s config). If it connects successfully, revisit the Laravel cache steps and double-check your database.php file.
内容的提问来源于stack exchange,提问作者Md. Amin Hossain

