PHP cannot log in because the database says your host is not allowed
Warning: mysqli::__construct(): (HY000/1130): Host is not allowed to connect to this MariaDB serverShort answer
Error 1130 means the server has no account whose host part matches the machine PHP connects from. It is not a wrong password. Run SELECT USER() from the failing connection to see the host the server sees, then create or change an account for that user and host.
Fix it fast
1Find out which cause you have
Log in to the database from the database machine as an admin and list which hosts each account name may connect from.
mariadb -u root -p -e "SELECT user, host FROM mysql.user ORDER BY user, host;"2Match what you see, then jump to its fix
- 1
The user name is listed in mysql.user, but only with host localhost or another address, never the address of the web server.No account exists for the client's hostFix - 2
You created 'app'@'%' and the error stays, or you connect but with the wrong privileges.A more specific account row wins over the one you createdFix - 3
The account is defined as 'app'@'web1.example.com' and the server runs with skip_name_resolve enabled.Host-name accounts stop matching when name resolution is offFix
- 1
What does the error look like?
Warning: mysqli::__construct(): (HY000/1130): Host is not allowed to connect to this MariaDB serverHost is not allowed to connect to this MySQL server
Cause 1 of 3
No account exists for the client's host
1Check
An account is a pair of user name and host. The manual says this error means no row in the user table has a Host value that matches the client host. Compare the host column from the first step with the address of the machine that runs PHP.
-- run on the database server
SELECT user, host FROM mysql.user WHERE user = 'app';2Fix
Create an account for the exact client host (or a netmask range) and grant it the privileges it needs. Use '%' only when you cannot know the address, and narrow it later.
CREATE USER 'app'@'203.0.113.10' IDENTIFIED BY 'a-long-random-password';
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'app'@'203.0.113.10';3Verify
Connect from the PHP machine with the same account. SELECT USER() then shows the user and host as the server sees them.
mariadb -h db.example.com -u app -p -e "SELECT USER(), CURRENT_USER();"Confirmed by dev.mysql.com
Cause 2 of 3
A more specific account row wins over the one you created
1Check
The server sorts account rows and uses the first match: literal hosts first, '%' later. For the same host, a named user sorts before an anonymous (blank) user. So an anonymous row for a specific host, or an account with the same name and a narrower host, can be picked instead of the '%' row. CURRENT_USER() shows which row matched.
SELECT user, host FROM mysql.user WHERE user = '' OR user = 'app';
SELECT CURRENT_USER();2Fix
Remove the anonymous accounts you do not need, or create the account for the exact host that matches, so the intended row is the first match.
DROP USER ''@'localhost';
CREATE USER 'app'@'localhost' IDENTIFIED BY 'a-long-random-password';3Verify
Reconnect and confirm CURRENT_USER() returns the account you meant.
mariadb -u app -p -e "SELECT CURRENT_USER();"Confirmed by dev.mysql.com
Cause 3 of 3
Host-name accounts stop matching when name resolution is off
1Check
With skip_name_resolve on, the server matches clients by IP address only, so accounts defined with host names cannot be used. Check the variable.
SHOW VARIABLES LIKE 'skip_name_resolve';2Fix
Define the account with the client's IP address (or an IP pattern) instead of a host name.
CREATE USER 'app'@'192.0.2.15' IDENTIFIED BY 'a-long-random-password';
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'app'@'192.0.2.15';3Verify
Connect again from the PHP host.
mariadb -h db.example.com -u app -p -e "SELECT 1;"Confirmed by dev.mysql.com
How do I stop it coming back?
- Create one account per application with the exact client address, not a root login from anywhere.
- Keep a short list of accounts in your deploy notes so a new web server gets its account before it goes live.
- Remove anonymous accounts on every new database server.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
Laravel bulk insert fails with 'too many placeholders'
SQLSTATE[HY000]: General error: 1390 Prepared statement contains too many placeholders
Laravel sends one placeholder per value, so one insert of many rows times many columns, or a whereIn with a huge list, creates a statement MySQL refuses.
3 causes, each with a fixDatabases
Laravel query fails: conversion from one collation into another is impossible
SQLSTATE[HY000]: General error: 3988 Conversion from collation utf8_unicode_ci into utf8mb4_0900_ai_ci impossible for parameter
MySQL is comparing or binding a value whose character set cannot be converted into the one on the other side.
3 causes, each with a fixDatabases
PHP cannot connect to MySQL: the connection attempt failed
Error 2003
Error 2003 means the TCP connection to MySQL never opened.
4 causes, each with a fixDatabases
The client cannot reach the MySQL or MariaDB server through its Unix socket file
ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2) (MariaDB client wording)
Error 2002 means the client tried the Unix socket file named in the message and found nothing it could talk to.
4 causes, each with a fixDatabases
MySQL or MariaDB: Column count of mysql.proc is wrong, table probably corrupted
Error 1558: Column count of mysql.proc is wrong
The mysql.proc system table has a different number of columns than the running server expects.
3 causes, each with a fixDatabases
Saving an empty date from PHP into a MySQL DATETIME column fails
The column holds 0000-00-00 00:00:00 instead of NULL
An empty string is not NULL.
4 causes, each with a fix