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:
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:
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:
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:
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.