Do you ever wonder where does your database lives? MySQL is just like any other programs, it runs and listens to specified port, accepts connections, analyzes commands and performs action which is querying to databases. Now we wonder where does this database lives? How do they look like? What are they? This database are just files in their own type and format. In a normal MySQL installation, they usually located in
directory. Note that it varies the installation path, but whatever you installation path will be and whatever MySQL version you used, the directory of the databases are usually in “data” folder or “datadir” folder under MySQL folder. Now that we know where this databases are located, we can safely move them in a new location if needed. Example scenario when you are organizing things and you want your development or work space to move to another drive, say drive E. In this case, below are steps on what to do:
- Stop your MySQL Server
- Copy the files on “data” directory to new location
- Locate the my.ini file. This is the main MySQL configuration file. This is usually just on the MySQL folder installation.
- Open it with admin permission and find the line “datadir= “whatever/your/path/to/data”, update this to your new “data” folder location.
- Start you MySQL Server
That’s it! You have now moved you databases on the new drive.
For Linux users:
- SSH to your MySQL server
1sudo /etc/init.d/mysql stop
- Make sure to make a backup first!
1sudo cp -Rp /var/lib/mysql /var/lib/mysql.backup
- Lets now move to its new mount or location
1sudo cp -Rp /yournewlcoation/var/lib/mysql /var/lib/mysql
- Lastly, open the mysql config file, usually located in
and update the “datadir” location path to point to your new location.
- Start you the MySQL server
1sudo /etc/init.d/mysql start
That’s it. You are now able to move your MySQL databases file safely.