On the server
mysql server
Don't turn off remote access while installing mysql.
The steps to take are outlined in Project 1 and 2
On the client server
run
sudo apt update
sudo apt-get install mysql-client
By default, both of your EC2 virtual servers are located in the same local virtual network, so they can communicate to each other using local IP addresses. Use mysql server's local IP address to connect from mysql client. MySQL server uses
TCP port 3306
by default, so you will have to open it by creating a new entry in ‘Inbound rules’ inmysql server
Security Groups. For extra security, do not allow all IP addresses to reach yourmysql server
– allow access only to the specific local IP address of your ‘mysql client’.
- You might need to configure MySQL server to allow connections from remote hosts.
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
- Replace ‘127.0.0.1’ to ‘0.0.0.0’ like this:
- Restart the server
sudo systemctl restart mysql.service
- Create Remote MySQL user and grant remote access to databases
CREATE USER 'username'@'%' IDENTIFIED BY 'new-password';
FLUSH PRIVILEGES;
- Then you can grant access to databases using the
GRANT ALL
command: \
GRANT ALL PRIVILEGES ON dbname.* TO 'username'@'%';