Skip to main content

Understanding MySQL Error 1049 (42000): Unknown database

Question

How do I resolve MySQL Error 1049: Unknown database when connecting using a URL like: mysql://user:pass@host:port/<database_name>?

danger

Error: mysql: query system variables: Error 1049 (42000): Unknown database '<database_name>'

What does this error mean, and how can I fix it?

Answer

MySQL Error 1049 means the database in your connection URL doesn't exist on the MySQL server. To resolve this, first check if the database (schema) is actually present. You can use the atlas schema inspect command to list all databases (schemas) on the server:

atlas schema inspect \
--url mysql://root:password@localhost:3306/ \
--exclude '*.*'

The command above lists all databases available on the MySQL server at localhost:3306, excluding their internal resources (tables, views, etc.) for readability. If the database you are trying to connect to isn't listed, it means it does not exist on the server, and it should be created first.

If the database is listed, but you're still seeing the error, check for special characters in the database name that may require escaping or quoting. For example, if the database name contains spaces like my db, you can use either quoted or escaped URLs to connect:

# Quoted URL.
atlas schema inspect \
--url "mysql://root:password@localhost:3306/my db" \
--exclude '*.*'

# Escaped URL.
atlas schema inspect \
--url mysql://root:password@localhost:3306/my%20db \
--exclude '*.*'

To learn more about URLs with non-alphanumeric characters, read this doc.