Manuals
RU EN

Start · 23

MySQL Database Connection Error

Start 6 min read

A database connection error during installation almost never means "the script is broken". It means the host, login, password, database name, or permissions do not match. The installer only shows what mysqli/PDO returned.

Below are real error lines from tickets. Move from top to bottom; do not jump straight to "reinstall PHP".

Typical error texts

SQLSTATE[HY000] [1045] Access denied for user ... means the login or password is wrong, or the user cannot connect from this host. On shared hosting the user is often like u12345_shop, while the form gets root "like locally".

Unknown database 'xxx' means no database with that name exists. It may be a typo, a different name encoding, or a database created under another panel account.

SQLSTATE[HY000] [2002] Connection refused / No connection could be made means MySQL is not listening on the port, the service is down, or the host/port is wrong. Locally: MicroServer/MariaDB is not running. On a VDS: mysqld did not start after reboot.

Can't connect to local MySQL server through socket means PHP is looking for the socket in the wrong place. Try 127.0.0.1 instead of localhost to use TCP. The reverse can also happen.

Access denied ... to database with a valid login means the user exists but is not attached to this database. In phpMyAdmin or the panel, grant privileges specifically on the required database.

Check order

  1. Database name: copy it from the panel, do not type from memory.
  2. The user is attached to this database, not "all databases" by guess and not a neighboring one.
  3. Host: locally 127.0.0.1; on Beget/Timeweb, use the database card; sometimes it is a separate hostname.
  4. Port: usually 3306. If the panel shows another port, enter it in the config if the script supports it.
  5. Password without a leading or trailing space. Recreate it in the panel and paste again.

Check outside the installer: log in to phpMyAdmin with the same details or run a simple CLI connection. If the panel accepts them but the script does not, check whether you entered different values into the installer form.

Local Windows / MicroServer

MariaDB listens on 127.0.0.1:3306. If MicroServer is not running, connection refused is expected. Create the database in advance: local phpMyAdmin. A root user with an empty password still exists on older stands; on newer ones, create a separate user for the project.

Do not confuse the site folder name shop.loc with the database name. They are different things.

Shared hosting

On shared hosting it is common that:

  • a prefix on database and user names is mandatory;
  • remote MySQL access from your home IP is disabled, which is correct; a site on the same server needs localhost;
  • after changing the password in the panel, the old password remains in the script config and the site "worked yesterday".

If you moved a site from local and left root with no password in config, production will almost certainly return 1045. Migration order: script migration.

VDS and doing everything yourself

Service not running:

systemctl status mysql
# or
systemctl status mariadb

Listening only on localhost is normal for a site on the same machine. If PHP runs in Docker or another container and MySQL runs on the host, 127.0.0.1 inside the container points to the wrong place. Use the correct network hostname, not a shared-hosting manual value.

After importing a dump, config credentials must match the new server: dump migration.

Permissions and "the database exists, tables are not created"

The connection works, but the installer fails on CREATE TABLE: the user lacks CREATE/ALTER/INDEX. With minimal "SELECT/INSERT only" grants, the site may later "open", but installation will not finish. Grant the full set on this database for installation; then reduce privileges if security policy requires it.

Charset

A connection error is rarely about charset. But immediately after a successful connection, broken text means the database is not utf8mb4. Requirements: PHP and MySQL. Recreating an empty database in utf8mb4 is easier than repairing mixed collation later.

Do not expose passwords in tickets. A masked host, database name, username, and error code (1045/2002/...) are enough. Recreate the password if it has already appeared in correspondence.

Related symptoms

Sometimes a "database error" appears as HTTP 500: display_errors is off, and the log contains the same Access denied. Check error_log: 500 error. If after migration there is a white screen and only DB errors in the log, the config was not updated.

Fresh installation after fixing access: script installation. Backup before import experiments: backup.

One-line connection check

On a server with PHP CLI:

php -r "mysqli_connect('127.0.0.1','user','pass','db') or die(mysqli_connect_error()); echo 'ok';"

If this returns ok but the installer complains, you are entering different data into the form. If this returns 1045, fix the user, not the script. On Windows, the PHP path comes from MicroServer: local server.

"It worked yesterday"

The hosting panel password changed, MySQL was updated, the host moved the database to another container, or disk space ran out and MySQL stopped. Disk: monitoring. After changing the password, update the script config everywhere; sometimes there is a duplicate in local.php.

Reading GRANTs

SHOW GRANTS FOR 'user'@'localhost';

The required database must be there. user@'%' and user@'localhost' are different accounts. The installer connects one way, phpMyAdmin tests another, and confusion begins.

Socket vs TCP again

A socket error on shared hosting after changing plans means you should try the host from the provider documentation. Sometimes it is written as localhost:/var/run/mysqld/mysqld.sock. In script configs with one host field, 127.0.0.1 is often enough.

After dump import

The connection works and the site opens, but some sections fail: the import may have stopped halfway. Compare the number of tables. Full cycle: dump migration, migration. Broken text is not 1045, but a nearby pain: the database should be utf8mb4 from creation: requirements, locally: phpMyAdmin.

Firewall and external MySQL

Closing 3306 on a VDS is correct. If PHP is on another machine, which is rare in our layouts, use private network access, not a public opening. Otherwise scanners will find it. See firewall, VDS.

When the connection is fixed but the page is still 500, the log now contains another error: 500 error. File permissions are unrelated to the database, but they are often fixed in parallel: chmod.

FAQ

Database connection error during script installation?

Most often the host, login, database name, or user permissions are wrong, not the script.

How long does “MySQL Database Connection Error” take?

About 6 minutes to read. In practice it depends on your hosting and database setup.

Do I need a dedicated server?

For most scripts, shared hosting or a VDS with PHP and MySQL is enough. See the VDS section and PHP/MySQL requirements.

How to install a php script on hosting?

See the related manual for this query. how to install a php script on hosting

Php mysql requirements for a script?

See the related manual for this query. php mysql requirements for a script

Section
Installing a PHP script

From server requirements to SSL and cron — every manual for launching a ready-made PHP script.