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 subnet
as 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
lsblk
to see the name of the new EBS volume. sudo file -s /dev/SSD_Name
to check if the SSD has data in it. Not necessary since we just created the SSD.sudo mkfs -t ext4 /dev/SSD_Name
to create a file system in that SSD.sudo mkdir /var/lib/mysql
create 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/mysql
mount the SSD there.sudo rm -rf /var/lib/mysql/
A file calledlost+found
will be created after mounting the SSD. Remove it.
- Using
Install and Start MySQL
sudo apt install mysql-server
install MySQL.- Grant permission to MySQL.
sudo chown mysql:mysql /var/lib/mysql
sudo chmod 777 /var/lib/mysql
777 is dangerous, change the number accordingly.
sudo mysqld --initialize
sudo vim /var/log/mysql/error.log
to check the log and see if the initialization is successful.
sudo systemctl start mysql
start MySQL.
Log into MySQL as root user
sudo grep 'temporary password' /var/log/mysql/error.log
to see the temporary password for MySQL’s root user.- Set MysQL users.
mysql -u root -p
to 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-address
to0.0.0.0
or specified IP address. sudo systemctl restart mysql
restarting 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 -h
to see if the SSD is mounted as we expected.
Create Snapshot
- Easiest step