All database files in MySQL DBMS are stored on the hard disk. The database files are stored in the /var/lib/mysql directory. But the path can be changed. In the instruction, we will tell you how to see where the database files are stored in MySQL DBMS in Ubuntu 20.04 operating system.
All database files are stored in the /var/lib/mysql
directory. To make sure of this you can run a command that will display the full path to the files where the databases are stored:
$ sudo grep -R 'datadir' /etc/mysql/
If you go to the suggested path - /var/lib/mysql
, the directory will contain all the necessary files c databases. The command to go to the directory should be executed under the root user account otherwise there will be a Permission denied error:
$ cd /var/lib/mysq
$ sudo su
$ cd /var/lib/mysql
$ ls -l
Next, the user will find out where the MySQL databases are located. You can also run the command mysqld
with the parameters --verbose
and --help
, which will display the path of storage files with databases. This command is similar to the previous command.
$ mysqld --verbose --help | grep ^datadir
If you need to change the path of database files, you need to open the MySQL configuration file as root using any text editor. In this example, as we learned above, the configuration file is located at /etc/mysql/mysql.conf.d/mysqld.cnf
. The command to edit it would look like this:
$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
In the new file that will open, you need to find the block [mysqld]
and in it a parameter named datadir
in which you can write the desired path where the database will be stored. After the path has been set, you need to save the changes, close the text editor and restart the DBMS using the command:
$ sudo systemctl restart mysql
Now you know where the databases are stored in MySQL DBMS.