Introduction
If you’ve been building Laravel applications long enough, you know the pain of production errors that come and go without warning. One such frustrating issue is:
SQLSTATE[HY000] [1130] Host 'localhost' is not allowed to connect to this MariaDB server
(Connection: mysql, SQL: select * from sessions where id = 0kAgFm9WZKSzpeZmtJ8vsiT8nzoa2CLJtbu19B6x limit 1)
What makes this error even more annoying is its intermittent nature — one request fails, you reload the page, and suddenly everything works fine again.
In today’s post, we’ll break down why this happens, how to diagnose it, and every possible fix — from quick configuration tweaks to deeper MySQL-level solutions.
Understanding the Error
The error message is coming from MariaDB/MySQL, not from Laravel itself. It means:
The database user exists.
But MariaDB does not have permission to allow that user to connect from the host it’s connecting from.
For example:
user@localhostmight exist and work fine.- But if PHP decides to connect over
127.0.0.1or::1, MariaDB sees it as a different host (user@127.0.0.1) — and denies access.
This explains the intermittent behavior — if sometimes the app connects via a UNIX socket and sometimes via TCP (IPv4 or IPv6), it will succeed in some requests and fail in others.
Step-by-Step Solutions
Below are the most common causes and all possible solutions to fix the error for good.
1. Stop Using localhost (Use a Fixed Host)
The simplest and most reliable fix is to force Laravel to always connect in the same way.
Edit your .env:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_SOCKET=
Then clear cached configurations:
php artisan config:clear
php artisan cache:clear
php artisan config:cache
Why this works:localhost can resolve to:
127.0.0.1(IPv4 TCP)::1(IPv6 TCP)- A UNIX socket path
By using 127.0.0.1, you force a TCP connection over IPv4 every time.
2. Grant Permissions for All Possible Hosts
If you must keep using localhost, make sure the database user has permissions for all possible hosts.
Connect to MariaDB and run:
SELECT user, host FROM mysql.user;
Then create the necessary users:
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON yourdb.* TO 'appuser'@'localhost';
CREATE USER 'appuser'@'127.0.0.1' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON yourdb.* TO 'appuser'@'127.0.0.1';
CREATE USER 'appuser'@'::1' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON yourdb.* TO 'appuser'@'::1';
FLUSH PRIVILEGES;
If your host has skip-name-resolve enabled (common on managed servers), hostname-based grants won’t work. Use IP-based grants only (127.0.0.1 or %).
Check this with:
SHOW VARIABLES LIKE 'skip_name_resolve';
3. Use a UNIX Socket Explicitly (Optional Optimization)
On the same server, connecting via a socket is faster and avoids network stack issues.
First, find your socket path:
mysql_config --socket
# Or commonly:
# /var/run/mysqld/mysqld.sock
Then update your .env:
DB_HOST=localhost
DB_SOCKET=/var/run/mysqld/mysqld.sock
4. Clear Laravel Cache (Very Important!)
Laravel caches configuration aggressively. If .env changes are not reflected, old values might still be used by some workers — causing random errors.
Run these commands after every deployment:
php artisan config:clear
php artisan cache:clear
php artisan config:cache
Also consider restarting PHP-FPM:
sudo systemctl reload php-fpm
5. Check for IPv6 Resolution Issues
If /etc/hosts includes:
127.0.0.1 localhost
::1 localhost
Then localhost might resolve to IPv6 sometimes. Again, forcing 127.0.0.1 in .env solves this instantly.
6. Optimize Session Handling (Optional but Helpful)
This error often appears during session queries (like select * from sessions). If your site has traffic bursts or is on shared hosting, connections can spike.
Two ways to reduce load:
- Switch to Redis or Memcached for sessions:
SESSION_DRIVER=redis
- Or temporarily switch to file sessions:
SESSION_DRIVER=file
7. Double-Check Your Hosting Environment
If your database is on a remote server (not localhost), ensure:
- The remote host/IP is allowed in MariaDB grants (
'appuser'@'your-server-ip'). - Any firewall or security group allows traffic on port
3306. - Connection pooling is properly configured if using tools like Octane or Swoole.
Quick Troubleshooting Checklist
- Force
DB_HOST=127.0.0.1 - Grant privileges for
127.0.0.1,localhost, and::1 - Clear Laravel config and cache
- Restart PHP-FPM
- Verify
skip-name-resolvesetting - Use socket or TCP consistently
- Consider alternative session drivers
Test Your Fix
Before deploying, always test database connectivity from the command line:
mysql -h 127.0.0.1 -u appuser -p yourdb
or with the socket:
mysql --socket=/var/run/mysqld/mysqld.sock -u appuser -p yourdb
If these succeed, Laravel will too.
Final Thoughts
This “Host not allowed” error is one of those frustrating problems that feels random but actually follows strict logic. Once you understand that MySQL permissions are tied to hostnames and connection methods, fixing it becomes straightforward.
By sticking to a single connection method (TCP or socket), granting proper privileges, and clearing Laravel’s cache, you’ll eliminate this issue for good.

Comments