MySQL MariaDB

Where Does MySQL Store Databases on Linux?

The directory where MySQL stores its databases is known as the Data directory. It is created at the time of MySQL installation to store the metadata of the MySQL server. Sometimes while working with MySQL databases for administrative tasks. In Linux, the default directory is “/var/lib/mysql” for storing the database on Linux, but sometimes the user wants to locate it for their system.

This post will discuss where MySQL stores databases on Linux.

Prerequisite: Login to MySQL Server

To login into the local MySQL server, use this syntax:

sudo mysql -u <User-name> -p

Provide your MySQL database username in the syntax and run it. Enter the password to login to the MySQL server successfully:

Find Location of Data Directory Using “SELECT” Command

To find the location where MySQL stores databases, use this command:

SELECT @@datadir;

The command will display the data directory path:

Find Location of Data Directory Using “SHOW VARIABLE” Command

Use the “SHOW VARIABLE” command where the data is similar to “datadir”, find the location of the data directory:

SHOW VARIABLES LIKE 'datadir';

The output will display the variable name as “datadir” and the value containing the path of data directory:

Alternatively if you do not want to first login to the MySQL server and then run these command, Use this command to extract the path of data directory directly by providing the database username and password:

mysql -u <username> -p -e "SHOW VARIABLES LIKE 'datadir';"

Make sure to enter the username of your MySQL server. The output will return the data directory:

Conclusion

To locate the path of the data directory, login to the MySQL server and run the “SELECT @@datadir;” or “SHOW VARIABLES LIKE ‘datadir’;” command. If you don’t want to login and run the command separately, run the “mysql -u <username> -p -e “SHOW VARIABLES LIKE ‘datadir’;” command. This guide has demonstrated where MySQL stores databases on Linux.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.