Get to MySQL data directory on Ubuntu

I’ve been asked about this and realized that it may not be obvious to people who are more familiar with Windows than with Ubuntu. So here is a tip.

When learning MySQL on Ubuntu, it is helpful to be able to get to MySQL’s data directory and observe file creation and modification while you are performing database and table creation and modification, index creation and modification, etc. You can find out MySQL’s data directory by running:

show variables like ‘datadir’;

Suppose your datadir is /var/lib/mysql. And suppose you have a database called test. After opening a terminal window and type:

cd /var/lib/mysql/test/

You will likely receive an error like below:
bash: cd: /var/lib/mysql/test/: Permission denied

Now if you try the command below, as it seems logical on Ubuntu

sudo cd /var/lib/mysql/test/

You will likely get this message:

sudo: cd: command not found

A quick way to get around it and run with root privilege, is through

sudo -i

You will then be able to get to MySQL data directory without any problems. Hope this helps somebody out there trying to learn.