Have you ever encountered an error while trying to connect to MySQL database from an EC2 instance created from an Amazon Linux 2 AMI without MySQL or MySQL Client installed? It’s a common frustration, especially when you need to connect and query your MySQL database from an EC2 instance.
Even with a bash script to install MySQL upon instance creation, it fails when the instance can’t recognize mysql as a valid package.
I’ve faced this issue numerous times myself when trying to connect to MySQL database from mostly Amazon Linux 2 instance but eventually found a solution. Let me share how I resolved it.
Here’s how to check through to confirm if MySQL is installed on your instance.
- Run
mysql
from the command line, you should get a response Bash:mysql
, command not found
In most cases by running the commands below, it should get MySQL CLI installed on your EC2 instance.
For Debain, Ubuntu
Sudo apt update
Sudo apt install mysql –y
For CentOS, Amazon Linux 1, 2, Red Hat
Sudo yum update
Sudo yum install mysql –y
These commands did not work for me. I got a reply “No match for argument: mysql, Error: Unable to find a match: mysql” when I tried it.
To clarify why I connect to MySQL via EC2, I’ve set up a MySQL database in a private subnet, restricting access solely through the EC2 instance, and acting as a bastion host. Despite attaching a startup script for MySQL installation, it failed to execute as expected.
Even manual installation didn’t succeed. Despite numerous attempts, I still receive a command not known error when trying to run mysql or mysql-client
Below is how I fix it. Run the commands below for Amazon Linux, CentOS, and Red-Hat users
sudo yum install -y https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
sudo yum install -y mysql-community-client
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
Run the commands for Debian, Ubuntu
sudo apt install -y https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
sudo apt install -y mysql-community-client
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
After running those commands, you have installed MySQL CLI on your EC2 instance or have only downloaded some useful packages to install it. For my case running mysql
command again still shows me the reply that the command was not found which means that MySQL has not been installed yet.
I had to do more, I searched for the packages available in my instance that I could use, and then I installed the closest package. Here’s the command below.
sudo yum search mysql
sudo yum install mysql-community-client.x86_64
This should do it, if you run MySQL now you should see a response indicating MySQL client has been installed and you can use it to connect to your MySQL database from your EC2 instance.
Here’s the command you should use to connect to your MySQL database from the EC2 instance but make sure your EC2 instance can connect to your MySQL database i.e. You have done the required subnet and security group configurations.
mysql -h YOUR-DB-CONNECTION-STRING -P 3306 -u admin -p
If you use Amazon Linux 2023, check out this blog for more information on Installing the MySQL CLI on an EC2 Instance (thomasstep.com)
Conclusion
You’ve successfully installed MySQL CLI and can now connect to your MySQL RDS database from the EC2 instance. If this solution doesn’t work for you, consider recreating the EC2 instance using a different AWS instance image.
Although i mostly get this error when connecting through an EC2 instance running Amazon Linux 2 AMI.
Please share your thoughts in the comments section below, and don’t forget to explore our other articles. Stay connected!