Error
Error Code: 1049

MySQL Error 1049: Unknown Database Name

📦 MySQL
📋

Description

This error signifies that MySQL cannot find or access the database name specified in your connection attempt or SQL query. It commonly occurs when the database name is misspelled, the database has not yet been created, or it has been deleted from the server.
💬

Error Message

Unknown database '%s'
🔍

Known Causes

3 known causes
⚠️
Typo in Database Name
The database name provided in your connection string or SQL statement does not exactly match an existing database on the server.
⚠️
Database Does Not Exist
The specified database has not been created on the MySQL server or was previously deleted.
⚠️
Connected to Wrong Server
Your application or client might be connecting to a different MySQL server instance where the intended database does not reside.
🛠️

Solutions

5 solutions available

1. Verify Database Name easy

Check the database name is spelled correctly

1
List all databases
SHOW DATABASES;
2
Search for similar database names
SHOW DATABASES LIKE '%mydb%';

2. Create the Database easy

Create database if it doesn't exist

1
Create database
CREATE DATABASE mydb;
2
Create only if not exists
CREATE DATABASE IF NOT EXISTS mydb;
3
Create with specific charset
CREATE DATABASE mydb
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

3. Fix Connection String easy

Update database name in application config

1
Check and fix connection string format
# Standard MySQL connection string:
mysql://username:password@hostname:3306/database_name

# PHP PDO:
$dsn = 'mysql:host=localhost;dbname=correct_database';

# Node.js:
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'user',
  database: 'correct_database'  // Check this!
});

4. Restore from Backup medium

Restore database if accidentally dropped

1
Create empty database first
CREATE DATABASE mydb;
2
Restore from SQL dump
mysql -u username -p mydb < backup.sql
3
Or restore specific tables
mysql -u username -p mydb < tables_backup.sql

5. Check User Privileges medium

User may not have access to see the database

1
Check user grants
SHOW GRANTS FOR 'username'@'localhost';
2
Grant access to database
GRANT ALL PRIVILEGES ON mydb.* TO 'username'@'localhost';
FLUSH PRIVILEGES;
🔗

Related Errors

5 related errors