--- type: video yt_id: VVU2YVRMdUlfajQtMHdpRFN6bWFQY3RRLk45RHdhVGhocHNn videoId: N9DwaThhpsg title: "MySQL on AWS EC2 Amazon Linux 2" date: "2021-09-18T07:28:32Z" slug: "mysql-on-aws-ec2-amazon-linux-2" image: name: "mysql-on-aws-ec2-amazon-linux-2.jpg" alt: "MySQL on AWS EC2 Amazon Linux 2" width: 1280 height: 720 status: 'published' description: "Install and set up a MySQL database on an Amazon Linux 2 ec2 instance." tags: ['aws', 'cloud computing', 'sql', 'mysql', 'database', 'relational database'] previousPostSlug: 'deploy-node-app-on-aws-ec2-amazon-linux-2' nextPostSlug: 'nginx-reverse-proxy-on-aws-ec2-amazon-linux-2' --- https://gist.github.com/meech-ward/db3597795904584b83a0cfa79b461b7d Install MySQL and start it using the following commands ```sh sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm sudo yum localinstall -y mysql57-community-release-el7-8.noarch.rpm sudo yum install -y mysql-community-server sudo systemctl start mysqld sudo systemctl enable mysqld ``` Get the password for the root user: ```sh sudo grep 'temporary password' /var/log/mysqld.log ``` Login to MySQL: ```sh mysql -u root -p ``` You have to change the root user's password before you can do anything, so run the following command to do that: ```mysql ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyNewPass1!'; ``` upload the sql file https://gist.githubusercontent.com/meech-ward/1723b2df87eae8bb6382828fba649d64/raw/ee52637cc953df669d95bb4ab68ac2ad1a96cd9f/lotr.sql Create a new user called `sam` with the password `MyNewPass1!` that can access the database from localhost: ```mysql CREATE USER 'sam'@'localhost' IDENTIFIED WITH mysql_native_password BY 'MyNewPass1!'; GRANT ALL PRIVILEGES ON lotr.* TO 'sam'@'localhost'; ``` Now we can access the database with this user from the ec2 instance. So If I installed a web app on this instance, I could use this username and password. However, if you want to be able to access the database from anywhere else, First, we have to create a new user that can access this database from any host. ```mysql CREATE USER 'frodo'@'%' IDENTIFIED WITH mysql_native_password BY 'MyNewPass1!'; GRANT ALL PRIVILEGES ON *.* TO 'frodo'@'%'; ```