简体   繁体   中英

How do I create a mysql database for my nodejs project?

This should be really basic and simple to do but I can seriously not find any understandable information on how to create a simple database for my nodejs typescript project.

I have installed the following packages with npm:

  • mysql2
  • sequelize
  • sequelize-cli
  • sequelize-typescript

I have attempted the following commands at the terminal

C:\repos\NodeNew>mysql2 -u root -p
'mysql2' is not recognized as an internal or external command,
operable program or batch file.

C:\repos\NodeNew>mysql -u root -p
'mysql' is not recognized as an internal or external command,
operable program or batch file.

C:\repos\NodeNew>node mysql2 -u root -p
module.js:538
    throw err;
    ^

Error: Cannot find module 'C:\repos\NodeNew\mysql2'
    at Function.Module._resolveFilename (module.js:536:15)
    at Function.Module._load (module.js:466:25)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

C:\repos\NodeNew>

So how do I CREATE my database so I can connect to it with sequelize etc?

The tools you have installed are only for connecting a Node.js app to MySQL and do not include command-line tools to manage the MySQL server.

I will assume you have installed MySQL and it's running – you should then be able to find its mysql.exe command line client in the bin/ directory in the server's installation directory. If you haven't fiddled with authentication, just running it might work.

When you get to a MySQL prompt, you can follow any old instructions for creating a database; CREATE DATABASE foo; is the gist of it (authentication and permissions being a different story).

Since you're on Windows, you might want to look into HeidiSQL – it's been a while since I've used it, but it's a decent graphical MySQL management tool.

You can also use mysql2 to create the database – illustrated below – but I recommend getting a management tool.

const mysql = require('mysql2');
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'mysql',
});
connection.query('CREATE DATABASE foo');

You should have MySQL installed on your computer, to install mysql on Windows see the following page: MySql Once you have MySQL up and running on your computer. Open the Command Terminal and execute the following:

npm install mysql

Now you have downloaded and installed a mysql database driver. Node.js can use this module to manipulate the MySQL database:

var mysql = require('mysql');

To create a conecction create a file connection.js:

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

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

to test it save the file and run:

 node connection.js

Which will give you this result:

 Connected!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM