I got error CodeIgniter\Database\Exceptions\DatabaseException #8 when trying to connect to database

I got error CodeIgniter\Database\Exceptions\DatabaseException #8 when trying to connect to database

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing CodeIgniter 4 Database Connection Errors

When you encounter CodeIgniter\Database\Exceptions\DatabaseException #8 in your application, it is almost immediately recognizable as a MySQL-specific error code indicating "No such table". However, if this exception appears during the initial connection attempt before any query executes, the root cause shifts from a missing schema element to a fundamental misconfiguration of your database credentials or server environment.

In the scenario involving CodeIgniter 4 and XAMPP, the error manifests when instantiating a model that attempts to connect to the default database group defined in .env. The stack trace points to CodeIgniter\Database\Exceptions\DatabaseException, which wraps the underlying PDO or MySQLi driver errors. While the specific message "No such table" is technically accurate for query execution failures, seeing this during a connection phase often signals that the application cannot resolve the target database name provided in the configuration file.

Decoding the .env Configuration

The core of the issue lies in how CodeIgniter 4 constructs the connection string. Your environment variables define the connection parameters explicitly:

database.default.hostname = localhost
database.default.database = ci4
database.default.username = root
database.default.password =
database.default.DBDriver = MySQLi

Here, the application is instructed to connect to a database named ci4. If your XAMPP MySQL server is running and the user root exists with no password (as implied by the empty password field), the connection handshake itself should succeed. The failure usually occurs because the specific database ci4 does not exist on the server, or the user root lacks privileges to access it without a password set in the session state.

When CodeIgniter initializes the database connection via Config\Database::connect(), it attempts to execute an equivalent of SELECT 1 FROM ci4. If the database ci4 is missing from the information_schema.databases, the MySQL server returns error code 8. CodeIgniter then catches this driver-level exception and re-raises it as a DatabaseException.

Resolving the Missing Database Issue

The most common resolution is ensuring the target database exists before attempting to query it. If you are using XAMPP, you should access phpMyAdmin or run commands directly in the MySQL shell to verify the schema.

To create the missing database manually:

mysql -u root
CREATE DATABASE ci4 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
exit;

Once the database exists, retry your application. If the error persists despite the database existing, check the user privileges. The default root user in XAMPP might require a password after installation updates, or it might not have access to the newly created database if specific permissions were revoked. Ensure that root can connect to ci4 without a password if your .env specifies an empty string.

Handling the Exception Gracefully

While fixing the configuration resolves the immediate error, it is worth noting how CodeIgniter 4 handles these exceptions programmatically. The DatabaseException class provides access to the underlying driver exception via the getMessage() and getCode() methods.

If you are debugging this in a production-like environment where you want to distinguish between connection failures and query logic errors, consider catching the exception explicitly:

use CodeIgniter\Database\Exceptions\DatabaseException;

public function index()
{
    try {
        $komik = $this->komikModel->findAll();
        return view('komik/index', ['data' => $komik]);
    } catch (DatabaseException $e) {
        // Log the specific error code and message for debugging
        logger()->error("Database connection failed: {$e->getMessage()}");
        
        // Optionally redirect or show a generic error page
        return redirect()->to('/');
    }
}

This approach prevents the raw exception from halting the request stack abruptly, allowing you to log the specific getCode() (which should be 8 in your case) for further analysis without exposing sensitive details to the end user.

Verifying Environment Consistency

Another potential pitfall involves the order of environment loading. In CodeIgniter 4, the .env file is merged with .env.example. Ensure that no conflicting entries exist in a config/database.php file that might override your local settings. While hardcoding credentials in config files is generally discouraged for security reasons, accidental overrides can lead to unexpected connection behaviors if the config keys do not match the expected structure defined by the framework's migration system.

Finally, always verify that the database.default group name matches exactly what your model or service uses when fetching the database instance. If your model constructor relies on a specific factory method or direct instantiation using a different group name, mismatched configurations will result in connection failures regardless of the underlying credentials.

Our network