How to Fix SQLSTATE[HY000] [1045] Access denied for user in Laravel and phpMyAdmin
This error usually looks like: SQLSTATE[HY000] [1045] Access denied for user ‘myuser’@’localhost’ (using password: YES)
You might see it in:
- Laravel logs (
storage/logs/laravel.log) - CLI when running
php artisan migrate - phpMyAdmin when trying to log in
The meaning is simple: MySQL rejected the username/password/host combination. Let’s sort out why and fix it for both Laravel and phpMyAdmin.
1. Verify the MySQL user and password actually work
First confirm the credentials outside of Laravel and phpMyAdmin.
From terminal/SSH (if you have access):
mysql -u myuser -p
# then type the password
If login succeeds, the problem is in Laravel/phpMyAdmin configuration.
If MySQL says Access denied for user, the credentials or host are wrong or the user has no privileges.
On shared hosting, check your control panel (cPanel, DirectAdmin, Plesk):
- MySQL Users
- MySQL Databases
- Note the exact username, password, and database name.
Many panels prefix them, likecpaneluser_mydbandcpaneluser_myuser.
Update your notes to match those exact values.
2. Fix Laravel .env database settings
Open Laravel’s .env file in the project root and set:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password
Common mistakes:
- Using
localhostwhen host should be something like127.0.0.1ormysql.yourhost.com - Forgetting the cPanel prefix on the DB name and username
- Extra spaces or hidden characters in the password
After editing .env, clear config cache so Laravel picks it up:
php artisan config:clear
php artisan cache:clear
Then test:
php artisan migrate
If it runs without 1045, Laravel is fixed.
3. Fix phpMyAdmin configuration (if self-hosted)
On local setups (XAMPP, WAMP, custom servers), phpMyAdmin uses config.inc.php.
Look for:
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'secret';
Adjust these to match your MySQL user. If you installed MySQL with a root password, add it here.
On cPanel-based hosting you usually do not edit phpMyAdmin config directly; instead:
- Use the same username/password you created under “MySQL Users”
- Use that user when logging into phpMyAdmin through cPanel
If phpMyAdmin shows 1045, the credentials you are typing are wrong or that user does not have permission to access the selected database.
4. Grant proper privileges to the MySQL user
If you created the user manually, make sure it has permission on the database.
Log in as root (or a privileged user):
mysql -u root -p
Then run:
GRANT ALL PRIVILEGES ON your_database_name.*
TO 'your_database_user'@'localhost'
IDENTIFIED BY 'your_database_password';
FLUSH PRIVILEGES;
If your app connects from another host (rare on shared hosting):
GRANT ALL PRIVILEGES ON your_database_name.*
TO 'your_database_user'@'%'
IDENTIFIED BY 'your_database_password';
Be careful with % in production; it allows access from any host. Restrict it if possible.
5. Check the MySQL host value
localhost and 127.0.0.1 are not always interchangeable.
On some systems:
localhostuses a socket file (like/var/run/mysqld/mysqld.sock)127.0.0.1uses TCP on port 3306
If MySQL is listening only on socket, localhost works better.
If it is bound to the network interface, 127.0.0.1 or the server IP works better.
Try both in .env:
DB_HOST=127.0.0.1
# or
DB_HOST=localhost
On shared hosting, your provider might give you a custom host like mysql123.hosting.com. Use that exactly.
6. Special case: root user on local dev
On local machines (XAMPP, MAMP, WAMP), MySQL root often has:
- No password, or
- A password set during installation
Example Laravel .env for XAMPP default:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
If you changed the root password in phpMyAdmin, update DB_PASSWORD accordingly.
7. Confirm the database actually exists
If the database name in .env or phpMyAdmin does not exist, some setups can also throw access errors.
Check with:
SHOW DATABASES LIKE 'your_database_name';
or via cPanel / phpMyAdmin GUI.
If it is missing, create it:
CREATE DATABASE your_database_name
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Then give your user privileges on it as shown earlier.
8. Laravel migration / artisan still failing?
If Laravel continues to show the same 1045 error even after you confirmed credentials:
- Make sure you are editing the correct
.envfile (not.env.example). - Check file permissions so PHP can read
.env. - Run:
php artisan tinker >>> config('database.connections.mysql')Verify that the printed config matches what you set in.env.
If it doesn’t, runphp artisan config:clearagain or checkconfig/database.phpfor hard-coded values.
9. Quick checklist
When you hit SQLSTATE[HY000] [1045] Access denied for user in Laravel or phpMyAdmin:
- Confirm the MySQL user and password by logging in with the
mysqlCLI. - Double check cPanel prefixes on database and username.
- Update Laravel
.envwith the correct DB name, user, password, and host. - Run
php artisan config:clearand test withphp artisan migrate. - For phpMyAdmin, log in with the same MySQL user or update
config.inc.phpif self-hosted. - Grant privileges with
GRANT ALL PRIVILEGESif the user lacks access. - Match the
DB_HOSTto whatever your provider or server uses (localhost,127.0.0.1, or a custom host). - Ensure the database actually exists.
Once credentials, host, and privileges line up, the 1045 access denied error disappears and both Laravel and phpMyAdmin connect to MySQL without issues.