Connecting MySQL to an Amazon EC2 Instance from Ubuntu

14 Jun 2023 Balmiki Mandal 0 Web development

How to Setup and Connect MySQL to EC2 Instance from Ubuntu

Setting up and connecting a MySQL database to an Amazon EC2 instance from Ubuntu is a fairly straightforward process. By following these steps, you will be able to have a fully-functional MySQL database server running on your EC2 instance in no time.

Step 1: Install MySQL on Your EC2 Instance

The first step in setting up a MySQL database on an EC2 instance is to install the necessary packages. We can do this with the following command:

sudo apt-get update
sudo apt-get install mysql-server

Once the packages have finished installing, we'll need to secure the installation with the following command:

mysql_secure_installation

This will prompt you for a password for the root user and then run through a series of questions that will help secure the MySQL installation. When it's finished, you'll have a fully-functional MySQL database server running on your EC2 instance.

Step 2: Connect to Your MySQL Server from Ubuntu

Now that your MySQL server is up and running, it's time to connect to it from your Ubuntu machine. To do this, open the Terminal application and type the following command:

mysql -u root -p

This will prompt you for the password that you chose during the mysql_secure_installation step. Once you've entered it, you'll be connected to your MySQL server and can begin using it.

Step 3: Create a Database and User

Now that you're connected to your MySQL server, it's time to create a database and user. To do this, enter the following command:

CREATE DATABASE my_db;
CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'my_password';

This will create a new database called "my_db" and a new user called "my_user" with the password "my_password". You should replace these values with something more appropriate for your own application.

Step 4: Grant User Permissions

Now that you've created a database and user, you'll need to grant the user permissions to access the database. To do this, enter the following command:

GRANT ALL PRIVILEGES ON my_db.* TO 'my_user'@'localhost';
FLUSH PRIVILEGES;

This will give your user access to the database that you just created. You can now disconnect from the MySQL server with the following command:

exit

Step 5: Connect to Your Database from Ubuntu

Now that everything is setup and configured, you can connect to your database from your Ubuntu machine. To do this, open the Terminal application and type the following command:

mysql -u my_user -p my_db

This will prompt you for the password that you chose for your user. Once you've entered it, you'll be connected to your database and can begin using it.

Conclusion

By following these steps, you should now have a fully-functional MySQL database running on your EC2 instance and connected to your Ubuntu machine. You can now begin developing applications or conducting other database activities with the knowledge that your MySQL database is running securely and reliably on your EC2 instance.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.