Saturday 6 May 2017

I cant seem to create a database in Mysql using node.js? How can i create a database in node.js?

I am trying to create a database using mysql using node.js. Whenever I type mysql -v on a terminal on mac I get this.

ERROR 1045 (28000): Access denied for user 'Andrew'@'localhost' (using password: NO)

I have brew, node, npm all installed and updated I think. When I run brew,node,npm - v I get the following.

Andrews-MacBook-Pro:Desktop Andrew$ brew -v
Homebrew 1.2.0
Homebrew/homebrew-core (git revision 1b3b; last commit 2017-05-06)
Andrews-MacBook-Pro:Desktop Andrew$ node -v
v7.10.0
Andrews-MacBook-Pro:Desktop Andrew$ npm -v
4.2.0

I am succesfull in creating a connection using the following script. It displays connected! once executed in terminal.

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: ""
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

However I cannot create a database using the following script.

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: ""
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  con.query("CREATE DATABASE mydb", function (err, result) {
    if (err) throw err;
    console.log("Database created");
  });
});
Run example »

The following message is displayed if executed in terminal.

/Users/Andrew/Desktop/table.js:17
Run example »
    ^^^^^^^
SyntaxError: Unexpected identifier
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)



via INternet Nub

No comments:

Post a Comment