When This Error Pops Up
You're sitting there writing a simple CREATE TABLE statement in MySQL, something like:
CREATE TABLE mydb.users (id INT);
And MySQL throws back:
ERROR 1046 (3D000): Unknown database 'mydb'
This happens when you've got the database name spelled wrong, or you never created that database in the first place. I've seen this a hundred times with new devs who copy-paste code from a tutorial and the database name doesn't match what's on their server.
Root Cause – Plain English
MySQL is strict. When you say mydb.users, it looks for a database called mydb. If that database doesn't exist, MySQL doesn't guess – it stops with error 1046. You either:
- Typed the database name wrong (common with uppercase vs lowercase on Linux – MySQL is case-sensitive there)
- Haven't created the database yet (forgot to run CREATE DATABASE first)
- Are in the wrong server (you're on a different MySQL instance that doesn't have that database)
The fix is simple: check the database exists, then use it correctly.
The Fix – Step by Step
- Connect to MySQL
Open your MySQL client (command line, Workbench, or whatever you use). Type your password when asked.
Expected: You'll see themysql>prompt. - List all databases
Run:
SHOW DATABASES;
Expected: A list of database names. Look for the one you tried to use, likemydb. If it's not there, that's your problem. - If the database is missing, create it
Type:
CREATE DATABASE mydb;
(Replacemydbwith the exact name you want.)
Expected:Query OK, 1 row affected. - Now tell MySQL to use that database
Run:
USE mydb;
Expected:Database changed. Now MySQL knows you're working insidemydb. - Create your table without the database prefix
Now you can just write:
CREATE TABLE users (id INT);
Or if you really want to include the database name, it'll work now because the database exists:
CREATE TABLE mydb.users (id INT);
Expected:Query OK, 0 rows affected.
If It Still Fails – Check These
- Check for typos – Did you write
mydbormy-db? Hyphens in database names are trouble unless you escape them with backticks. Avoid hyphens. - Case sensitivity on Linux – MySQL on Linux treats
MyDBandmydbas different. On Windows they're the same. If your server is Linux, use lowercase consistently. - You're connected to the right server – Run
SELECT @@hostname;to see which machine you're on. Maybe you're on a staging server that doesn't have that database. - Did someone drop the database? – Run
SHOW DATABASESagain. If the database was there a minute ago but now it's gone, someone deleted it. - Privileges issue – Run
SHOW GRANTS;to see what you're allowed to do. If you can't see any databases exceptinformation_schema, you probably don't have permission to see or create the one you need. Talk to your DBA.
That's it. Error 1046 is almost always a missing database or a spelling mistake. Create the database, use it, and the table creation will work.