Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

PHP cannot log in because the database says your host is not allowed

ErrorWarning: mysqli::__construct(): (HY000/1130): Host is not allowed to connect to this MariaDB server

Short 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.

  • Applies to PHP 8.3, MariaDB 11.4, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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;"
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2You 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. 3The 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

What does the error look like?

  • Warning: mysqli::__construct(): (HY000/1130): Host is not allowed to connect to this MariaDB server
  • Host 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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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:

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.