❤️ Star ❤️ the repo to support the project or 😄Follow Me.Thanks!
-
Clone the repository
git clone https://github.com/harsh6768/express-api-mysql.git
-
Install all the dependency
npm install
-
Create EC2 Instance in AWS
-
Run these command on your terminal where pem file exist
chmod 400 ec2-1.pem //here ec2-1.pem is file name. you can give any name to file
-
Run below command to connect with ec2 instance
ssh -i ec2-1.pem [email protected]
---->ec2-1.pem // pem file
---->ubuntu //by default username
---->ec2-18-191-201-10.us-east-2.compute.amazonaws.co //DNS Public domain name
Or just click the connect button to get all the necessary information to connect with the EC2 in your system
You will something like this ...follow these commands to connect with ec2.
-
Install apt in EC2 ubuntu
-
update ubuntu
sudo apt-get update
-
Install mysql server
sudo apt-get install mysql-server
-
Run below command to run sql queries
sudo mysql --user=root mysql
-
Show list of databases
show databases;
-
Use database :
use database_name;
-
Show list of tables
show tables;
-
Update root password after selecting mysql database.
update user set authentication_string='new-password' where user='root';
-
Flush privileges
flush privileges; exit
-
Restart mysql
sudo service mysql restart
-
Now you can use mysql using root password
sudo mysql -u root -p
follow below docs.
https://dev.mysql.com/doc/mysql-repo-excerpt/5.6/en/linux-installation-yum-repo.html
Now Clone any node.js project in ec2 instance and install all the dependency but before that you need to install node and npm in your ec2 instance
const Bluebird=require('bluebird');
const mysql=require('mysql');
const connection= mysql.createConnection({
host : 'localhost',
user : 'root', //by default user name
password :'', //put password that you have created while installing the mysql in ec2
database : 'node' // provide database name ,created in ec2
});
connection.connect((err)=>{
if(err) throw err;
console.log('MySQL Connected ...');
});
global.db=Bluebird.promisifyAll(connection);
module.exports=db;
Now set custom port in ec2 instance in which your express server is running after selecting security group from the left navigation view.
You can see we have Custom TCP rule in port 3002. You can set any port in which you are running your express server.
Client does not support authentication protocol requested by server; consider upgrading MySQL client
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
http://18.191.201.10:3002/getPosts will return all the post in the database
here 18.191.201.10 is the IPv4 Public IP of EC2
Install NODEJS if there is no APT-GET COMMAND found OR the easy way to install the nodejs and DOCKER INTO THE EC2
https://gist.github.com/npearce/6f3c7826c7499587f00957fee62f8ee9
sudo apt install nodejs
sudo apt install npm
https://github.com/nvm-sh/nvm
nvm install --lts //install using nodejs
sudo apt install phpmyadmin
Edit apache2.conf file
sudo nano /etc/apache2/apache2.conf
or
sudo vi /etc/apache2/apache2.conf
After that, add the following line into apache2.conf file and save it:
Include /etc/phpmyadmin/apache.conf
Note that, By default root cannot login as root user through phpMyAdmin. So, open your ssh terminal and type the following command to enable/allow mysql root login access for phpmyadmin on ubuntu aws web server:
sudo mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Note that, Replace password by the root password you entered while installing MySql.
After that type following query to changes into effect:
FLUSH PRIVILEGES;
sudo service apache2 restart
Now you can login as root from phpMyAdmin. So, open your browser and type the below url with your ec2 server ip:
http://[SERVER_PUBLIC_IP]/phpmyadmin
You can follow the bellow for better understanding and for step by step commands that you have to follow
https://www.tutsmake.com/how-to-install-phpmyadmin-amazon-ec2-ubuntu/