How to fix ERROR 1102: Incorrect database name in MySQL
ERROR 1102 occurs when MySQL encounters an invalid or non-existent database name in your query. This typically happens due to typos, special characters, reserved words, or databases that no longer exist.
Quick fix
Fixes 1102
SHOW DATABASES;- Verify the database exists
- Check for typos in the database name
- Handle special characters with backticks
- Use backticks for reserved keywords
SHOW DATABASES;ERROR 1102: Incorrect database name
What this error means
ERROR 1102 (SQLSTATE: 42000) with symbol ER_WRONG_DB_NAME is a MySQL server error that indicates the database name specified in your SQL statement is invalid or does not exist. MySQL has strict naming rules for database identifiers: they must be 64 characters or less, cannot contain ASCII NUL or supplementary characters, and cannot end with spaces. When MySQL encounters a database name that violates these rules or doesn't match any existing database on the server, it raises ERROR 1102. This error is a syntax/access validation error that prevents the query from executing, making it impossible to select or manipulate data in that database. The error message format is "ERROR 1102: Incorrect database name '%s'" where '%s' shows the problematic database name. This helps identify exactly which name was invalid.
Typical symptoms
- Database queries fail immediately with "ERROR 1102" before executing
- Connection attempts fail when specifying an invalid default database
- Migration tools or backup scripts report ERROR 1102 for certain databases
- MySQL upgrade commands fail with ERROR 1102 referencing system directories like "#mysql50#"
- Applications cannot connect when database name contains special characters or spaces
- Scripts work on one server but fail on another with the same database name
Common causes
- Typo in database name - misspelled name in USE statement or connection string
- Database does not exist - trying to access a database that was never created or was deleted
- Invalid characters in database name - using special characters like hyphens (-), spaces, or dots (.) without quoting
- Reserved MySQL keyword used as database name - using words like CREATE, SELECT, or DROP without backticks
- Case sensitivity mismatch - on case-sensitive filesystems (Linux), "MyDb" and "mydb" are different
- Leading or trailing spaces in database name - spaces at the beginning or end of the identifier
- Database name too long - exceeds 64 character limit for MySQL identifiers
- Hidden directories in data directory - folders starting with a dot (.) in /var/lib/mysql/ can cause errors during mysql_upgrade
- Leftover database directories - database folder remains in /var/lib/mysql/ after the database was dropped
- Supplementary or NUL characters - using non-ASCII or unicode supplementary characters in the name
How to fix "ERROR 1102: Incorrect database name"
First, check if the database you're trying to access actually exists on your MySQL server:
SHOW DATABASES;Look through the output for your database name. If it's not listed, you'll need to create it first:
CREATE DATABASE my_database;If the database should exist but doesn't appear, verify you're connected to the correct MySQL server (check hostname and port).
Review your SQL statement or connection configuration for typos. Common mistakes include:
- Misspelled database names: USE databse; instead of USE database;
- Wrong database selected in connection string
- Copy-paste errors from documentation or configuration files
Compare the name in your query character-by-character with the output from SHOW DATABASES;. Check for:
- Extra or missing letters
- Swapped letters
- Numbers instead of letters (0 vs O, 1 vs l)
If your database name contains special characters like hyphens, spaces, or dots, you must quote it with backticks:
USE `my-database`;
-- or for spaces
USE `my database`;
-- or for dots
USE `my.database`;This tells MySQL to treat the entire string as an identifier, not as SQL syntax. Without backticks, MySQL interprets hyphens as subtraction operators and spaces as separators.
Best practice: Avoid special characters in database names and use underscores instead:
CREATE DATABASE my_database;If your database name is a MySQL reserved word (CREATE, SELECT, DATABASE, etc.), you must enclose it in backticks:
USE `select`;
USE `database`;
USE `create`;Without backticks, MySQL interprets these as SQL keywords instead of identifiers, causing ERROR 1102.
Best practice: Avoid using reserved words as database names. If you must use one, always quote it with backticks.
On Linux and Unix systems, database names are case-sensitive. Windows and macOS are case-insensitive.
If you're migrating between systems, ensure consistency:
-- On Linux, these are THREE different databases:
USE MyDatabase;
USE mydatabase;
USE MYDATABASE;
-- On Windows, they all refer to the same databaseCheck the actual database names with SHOW DATABASES; and use the exact case:
USE MyDatabase; -- Matches the output exactlyIf moving databases between systems, rename them to lowercase for consistency:
ALTER DATABASE `OldName` MODIFY NAME = new_name;If ERROR 1102 appears after deleting a database, a leftover directory in /var/lib/mysql/ might be causing issues.
Warning: This requires MySQL server access and admin privileges.
1. Stop the MySQL server:
sudo systemctl stop mysql
# or on some systems
sudo service mysql stop2. List contents of the data directory:
ls -la /var/lib/mysql/ | grep "^\.\|^\.\."3. Move any directories starting with a dot (.) to a backup location:
sudo mv /var/lib/mysql/.directoryname /tmp/mysql-backup/4. Restart MySQL:
sudo systemctl start mysqlDuring mysql_upgrade, you might see ERROR 1102 for system directories like "#mysql50#.ssh":
ERROR 1102: Incorrect database name '#mysql50#.ssh'This happens when upgrading from MySQL 5.0 and legacy directories remain. The fix:
1. Check what's in the data directory:
ls -la /var/lib/mysql/ | grep "^d"2. Move the legacy directories:
sudo mv /var/lib/mysql/#mysql50# /tmp/mysql-legacy-backup/3. Run mysql_upgrade again:
sudo mysql_upgrade -u root -pIf you're getting ERROR 1102 from your application, check your database connection configuration:
Common connection string formats:
Node.js / mysql2:
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'my_database' // Check this matches exactly
});Python / mysql-connector:
connection = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="my_database" # Must be exactly correct
)PHP / mysqli:
$conn = new mysqli("localhost", "root", "password", "my_database");
// Last parameter is database - verify it existsVerify:
1. The database exists: SHOW DATABASES;
2. The name matches exactly (including case on Linux)
3. No typos or extra spaces
4. Restart the application after fixing
Advanced notes
MySQL identifier rules: Database names can contain letters, numbers, underscores, and dollar signs, but cannot start with a digit or contain ASCII NUL characters. The maximum length is 64 characters. Reserved words require backticks to be used as identifiers.
Character encoding: Special characters in database names are encoded in the filesystem according to MySQL's character set. Some filenames may contain percent-encoded characters (e.g., %40 for @). This is transparent in queries but matters when manually inspecting /var/lib/mysql/ directories.
Platform differences: Database and table names are case-sensitive on Linux/Unix but case-insensitive on Windows/macOS. This can cause migration issues. It's best practice to use lowercase with underscores for all database names to ensure cross-platform compatibility.
MySQL upgrade issues: When upgrading from older MySQL versions, the upgrade process may encounter ERROR 1102 if leftover system directories (like #mysql50# from MySQL 5.0) exist. These should be removed or moved before running mysql_upgrade.
Performance note: Error 1102 is caught at the parsing stage before query execution, so it has minimal performance impact - the server immediately rejects the query without resource allocation.
Frequently asked questions
- What does "1102" mean?
- ERROR 1102 (SQLSTATE: 42000) with symbol ER_WRONG_DB_NAME is a MySQL server error that indicates the database name specified in your SQL statement is invalid or does not exist. MySQL has strict naming rules for database identifiers: they must be 64 characters or less, cannot contain ASCII NUL or supplementary characters, and cannot end with spaces. When MySQL encounters a database name that violates these rules or doesn't match any existing database on the server, it raises ERROR 1102.
- How do I fix "1102"?
- ERROR 1102 occurs when MySQL encounters an invalid or non-existent database name in your query. This typically happens due to typos, special characters, reserved words, or databases that no longer exist.
- What causes "1102"?
- Typo in database name - misspelled name in USE statement or connection string Database does not exist - trying to access a database that was never created or was deleted Invalid characters in database name - using special characters like hyphens (-), spaces, or dots (.) without quoting
- Is the "1102" error serious?
- It is a low-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐MySQL Official Documentation
- ๐MySQL 8.0 Error Reference(Official Docs)
- ๐MySQL Schema Object Names Rules(Official Docs)
- ๐MariaDB Knowledge Base - Error 1102(Official Docs)