It’s bad idea to run MySQL server on microSD card because usually this type of application will be heavy on read/write cycles and this will effect the lifespan of the sd card. Plus depending on the type of card you are using the external hard drive will be faster. There are two ways to do it. First move files to external drive and change data dir variable or creating  symlink. We will use the latter (How to set up MySQL on Raspberry Pi).

  1. Stop the mysql server:
    sudo service mysql stop
  2. Backup the database:
    sudo cp -Rp /var/lib/mysql/ /var/lib/mysql.back/
  3. Next we will copy our mysql folder to external drive:
    sudo cp -Rp /var/lib/mysql /mnt/usbstorage/raspberry/mysql
  4. Delete the original mysql folder:
    sudo rm -r /var/lib/mysql
  5.   Now create symbolic link, first will be destination and second part is the link “sudo ln -s DESTINATION LINK”:
    sudo ln -s /mnt/usbstorage/raspberry/mysql /var/lib/mysql
  6. Finally restart the mysql server:
    sudo service mysql start

     

If everything went smoothly you can log in to your server.