Target:
Make a snapshot for MySQL and store it on AWS EBS
Steps:
Create and Mount EBS
- Create a new EBS volume within the
same subnetas the EC2 instance running MysQL, andattach that EBS to the instance. - Mount newly created EBS to MySQL’s default data directory,
/var/lib/mysql.- Using
lsblkto see the name of the new EBS volume. sudo file -s /dev/SSD_Nameto check if the SSD has data in it. Not necessary since we just created the SSD.sudo mkfs -t ext4 /dev/SSD_Nameto create a file system in that SSD.sudo mkdir /var/lib/mysqlcreate the directory since we haven’t install MySQL yet.- Creating this directory before installing MySQL is crucial. If install MySQL first, might run into permission and other problems when mount the SSD or writing data to it.
sudo mount /dev/SSD_Name /var/lib/mysqlmount the SSD there.sudo rm -rf /var/lib/mysql/A file calledlost+foundwill be created after mounting the SSD. Remove it.
- Using
Install and Start MySQL
sudo apt install mysql-serverinstall MySQL.- Grant permission to MySQL.
sudo chown mysql:mysql /var/lib/mysqlsudo chmod 777 /var/lib/mysql777 is dangerous, change the number accordingly.
sudo mysqld --initializesudo vim /var/log/mysql/error.logto check the log and see if the initialization is successful.
sudo systemctl start mysqlstart MySQL.
Log into MySQL as root user
sudo grep 'temporary password' /var/log/mysql/error.logto see the temporary password for MySQL’s root user.- Set MysQL users.
mysql -u root -pto log in MySQL with temporary password.- In MySQL terminal,
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';to change password.
Log into MySQL Remotely
- Change AWS security group inbound rules to open MySQL port 3306 for connections.
- Change MySQL configuration file to allow remote connections.
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf.- Change
bind-addressto0.0.0.0or specified IP address. sudo systemctl restart mysqlrestarting MySQL to make the change effective.
- MySQL root user does not support remote log in
CREATE USER 'junxihe'@'71.182.163.25' IDENTIFIED BY 'super_super_secret_password;
GRANT ALL PRIVILEGES ON *.* TO 'junxihe'@'71.182.163.25' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Remember to change the IP address accordingly.
Change to % to allow access from anywhere.
Data Migration to MySQL
- Personally recommend Navicat to migrate data
- late double check:
df -hto see if the SSD is mounted as we expected.
Create Snapshot
- Easiest step
AWS tutorial on creating EBS snapshots