In MySQL, the data directory stores information managed by the MySQL server. Each subdirectory of the data directory is a database directory and corresponds to a database managed by the server.
If you ever need to find out where the data directory is located on your MySQL implementation, below are some options to try.
The @@datadir
Variable
The easiest way to return the data directory is with the @@datadir
variable.
Example:
SELECT @@datadir;
Result:
+--------------------------+ | @@datadir | +--------------------------+ | /opt/homebrew/var/mysql/ | +--------------------------+
I installed MySQL via Homebrew, and the data directory location reflects that.
The SHOW VARIABLES
Statement
The SHOW VARIABLES
statement shows the values of MySQL system variables. MySQL contains a lot of system variables, but we can filter the results to just those we’re interested in.
Example:
SHOW VARIABLES WHERE Variable_Name LIKE "datadir";
Result:
+---------------+--------------------------+ | Variable_name | Value | +---------------+--------------------------+ | datadir | /opt/homebrew/var/mysql/ | +---------------+--------------------------+
The data directory location is in the Value
column. We can see the variable name in the Variable_name
column. This is the variable we looked up in the previous example. This is also the variable name that we filtered our results on (i.e. with WHERE Variable_name LIKE "datadir"
).
We can modify our statement to expand the results returned. For example, we can return all rows where the variable name ends with dir
:
SHOW VARIABLES WHERE Variable_Name LIKE "%dir";
Example result:
+-----------------------------+---------------------------------------------------------+ | Variable_name | Value | +-----------------------------+---------------------------------------------------------+ | basedir | /opt/homebrew/Cellar/mysql/8.0.29/ | | character_sets_dir | /opt/homebrew/Cellar/mysql/8.0.29/share/mysql/charsets/ | | datadir | /opt/homebrew/var/mysql/ | | innodb_data_home_dir | | | innodb_doublewrite_dir | | | innodb_log_group_home_dir | ./ | | innodb_temp_tablespaces_dir | ./#innodb_temp/ | | innodb_tmpdir | | | lc_messages_dir | /opt/homebrew/Cellar/mysql/8.0.29/share/mysql/ | | plugin_dir | /opt/homebrew/opt/mysql/lib/plugin/ | | replica_load_tmpdir | /var/folders/nl/60kzsgdn4cgdmvz534n7bdxh0000gn/T/ | | slave_load_tmpdir | /var/folders/nl/60kzsgdn4cgdmvz534n7bdxh0000gn/T/ | | tmpdir | /var/folders/nl/60kzsgdn4cgdmvz534n7bdxh0000gn/T/ | +-----------------------------+---------------------------------------------------------+
From the Command Line
We can run the above statements from the command line if required.
For example on a Mac, we can open a new Terminal window and enter the following statement:
mysql -uYourUserName -p -e 'SELECT @@datadir'
Result:
Enter password: +--------------------------+ | @@datadir | +--------------------------+ | /opt/homebrew/var/mysql/ | +--------------------------+
Where YourUserName
is the username. In this example I used -p
in order for the user to be prompted for the password.