Ask the server for the location. The server maintains a number of variables pertaining to its operation, and it can report any of their values. The data directory location is indicated by the datadir variable, which you can obtain using a mysqladmin variables commandor a SHOW VARIABLES statement. From the command line, use mysqladmin. On UNIX, the output might look like this:
% mysqladmin variables +---------------+-----------------------+ | Variable_name | Value | +---------------+-----------------------+ ... | datadir | /usr/local/mysql/var/ | ...
On Windows, the output might look like the following instead:
C:\> mysqladmin variables +---------------+-----------------------+ | Variable_name | Value | +---------------+-----------------------+ ... | datadir | c:\mysql\data\ | ...
From within mysql, check the variable's value like this:
mysql> SHOW VARIABLES LIKE 'datadir'; +---------------+-----------------------+ | Variable_name | Value | +---------------+-----------------------+ | datadir | /usr/local/mysql/var/ | +---------------+-----------------------+
If you have multiple servers running, they will be listening on different TCP/IP port numbers, sockets, or named pipes. You can get data directory information from each of them in turn by supplying appropriate --port or --socket options to connect to the port or socket on which each server is listening. Specifying a host of 127.0.0.1 explicitly tells mysqladmin to connect to the server on the local host using a TCP/IP connection:
% mysqladmin --host=127.0.0.1 --port=port_num variables
Under UNIX, specifying a value of localhost causes a UNIX socket connection to be used. You can also specify a --socket option if necessary to indicate the socket file pathname:
% mysqladmin --host=localhost --socket=/path/to/socket variables
Under Windows NT-based systems, a named pipe connection can be specified by giving '.' as a hostname, perhaps with a --socket option to indicate the pipe name:
C:\> mysqladmin --host=. --socket=pipe_name variables
For any platform, to connect via TCP/IP to a remote server running on another host, specify a --host option that indicates the name of the server host:
% mysqladmin --host=host_name variables
Specify a --port option as well if you need to connect to a port number other than the default.
Under UNIX, use the ps command to see the command line of any currently executing mysqld process or processes. By looking for a --datadir option, you may be able to determine the data directory location. If you have a BSD-style ps, try the following command:
% ps axww | grep mysqld
For a System V-style ps, try this instead:
% ps -ef | grep mysqld
The ps command can be especially useful if your system runs multiple servers because you can discover multiple data directory locations at once. The drawbacks are that ps must be run on the server host and that no useful information is produced unless the --datadir option was specified explicitly on the mysqld command line. (On the other hand, some of the startup scripts that invoke mysqld for you attempt to determine the data directory pathname and put it in the mysqld command line, which makes that information available to ps.)