Graduate Student in USC
Establish MySQL and Node.js environment in Ubuntu Server
Nov 9, 2017 / Written by Chengyu
This article shared my experience in deploying MySQL and Node.js remotelly with SSH Encryption connection and terminal.
1. Connect to the remote server
You can use embedded Terminal in Mac or PuTTY in Windows to connect to the server.
ssh 'username'@'Ip_address'
After you typing so, it will require you to enter the corresponding password if necessary. If you can see the following textfield. Congratulation! You have logged in successfully.
Terminal output after login
2. Download and install MySQL
Before downloading your MySQL, please check whether you have download it before. By typing "mysql", if the terminal can recognize this instruction, you can skip the following steps to step 3. If not, please type:
sudo apt-get update
sudo apt-get install mysql-server
sudo means you run this instruction as an admin. The download will take a few minutes and whenever you finish this step, typing mysql will help you enter the MySQL from terminal.
You can also verify the version of your mysql by typing: mysql --v
.
Please don't forget to setup your password for the database when download and installing. And after that you can type mysql -u root -p to enter the local database and you also need to enter the password you setup in the downloading process.
After login your MySQL
mysql> update user set update user set authentication_string=password("NEWPASSWORD") where User='root';
Before downloading Node.js in your server, please check wheter you have already had it before by typing "node".
sudo apt-get npm
npm init
Congraduation! You can put your Node.js file into server and run it now!
But, if you want to execute MySQL instrucitons in your Node file, please don't forget to download the necessary plug-in node_modules as well:
npm install express npm install mysqlHere is one example to connect the MySQL in Node file.
var mysql = require('mysql'); var app, express; //establish a server express = require("express"); app = express(); var bodyParser = require('body-parser'); app.use(bodyParser.json()); // support json encoded bodies app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies var con = mysql.createConnection({ host: "localhost", user: "your_username", password: "your_username's_corresponding_passwd", database: "your database" }); app.listen(8888) //any legal port you want to monitor