digitalspeed logo

How to install MySQL CLI on your AWS EC2 Instance

install mysql cli on ec2m instance

Have you ever encountered an EC2 instance created from an AWS AMI without MySQL or MySQL CLI 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 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 

Run the commands for Debian, Ubuntu

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 CLI 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. 

Please share your thoughts in the comments section below, and don’t forget to explore our other articles. Stay connected! 

Share this article

Recent Post

Send Us A Message

Related Post

Join our newsletter to stay updated

digitalspeed-logo

At DIGITALSPEED, you can get updates, reviews and learn about new digital tools and features on existing tools. check us on social media.

Get In Touch

Lagos, Nigeria

DIGITALSPEED © All Rights Reserved.

2024

Scroll to Top

Seach for Articles