The MySQL database needs to be used in the project, and the following is how to access MySQL
Here I use the Mac to develop the first need to download MySQL
Address: http://dev.mysql.com/downloads/mysql/
Refer to this installation http://dev.mysql.com/doc/refman/5.0/en/macosx-installation.html
After the installation is complete, you can see MySQL in the preferences settings, which can be started by shutting down MySQL.
When the default installation is complete, the root user of the database has no password.
For ease of operation, here I have installed a MySQL graphical operation tool Sequelpro
Download Address: http://www.sequelpro.com/download/
The installation is successful and the landing interface is shown in the following image:
When you log in as root, you can add a password to the root user.
The above is a simple introduction to MySQL installation. Here's how to use Nodejs to access MySQL
Create Express project, here I use the Db-mysql
Official website: http://nodejsdb.org/db-mysql/
According to its description above,
Before this you need to run the following command at the command line:
The code is as follows |
Copy Code |
Export Mysql_config=/usr/local/mysql/bin/mysql_config |
Then install Db-mysql
The code is as follows |
Copy Code |
NPM Install Db-mysql |
Then write a JS file, here I refer to the official example, I wrote a read MySQL in the test database in the user table data:
Code:
The code is as follows |
Copy Code |
var mysql = require (' Db-mysql '); New MySQL. Database ({ Hostname: ' localhost ', User: ' Root ', Password: ' Password ', Database: ' Test ' ). Connect (function (error) { if (Error) { Return Console.log (' CONNECTION error: ' + error); } This.query (). Select (' * '). From (' User '). Execute (function (error, rows, cols) { if (Error) { Console.log (' ERROR: ' + error); Return } for (Var i=0;i<rows.length;i++) { Console.log (Rows[i].name); } }); }); |
Very simple, and then I use the line JS, but there is a problem:
throw E;//Process.nexttick error, or ' Error ' event on a-tick
^
error:unable to load shared library/users/wanzhang/webstormprojects/readmysql/node_modules/db-
The code is as follows |
Copy Code |
Mysql/build/release/mysql_bindings.node At Object.. Node (module.js:472:11) At Module.load (module.js:348:31) At Function._load (module.js:308:12) At Module.require (module.js:354:17) At require (MODULE.JS:370:17) At Object.<anonymous> (/users/wanzhang/webstormprojects/readmysql/node_modules/db-mysql/db-mysql.js:18:15) At Module._compile (module.js:441:26) At Object.. JS (module.js:459:10) At Module.load (module.js:348:31) At Function._load (module.js:308:12) |
Workaround:
Export dyld_library_path=/usr/local/mysql/lib/
Then run this JS file, OK, will print out the data in the table.
Above is a simple example of Nodejs access to MySQL.