Nodejs connection mysql steps

王林
Release: 2023-05-28 13:05:37
Original
1715 people have browsed it

In web applications, data storage is a very important part. When developing applications, we need to use databases to store and retrieve data. MySQL is a powerful relational database management system that is widely used. Node.js is a very popular JavaScript runtime environment because it makes it easy and fast to create highly scalable server-side applications. In this article, we will learn how to connect to MySQL database using Node.js.

  1. Installing MySQL

Before we start using Node.js to connect to MySQL, we need to make sure that MySQL has been installed. We can download and install MySQL from the MySQL official website. Once the installation is complete, enter the following command at the command line to start the MySQL server:

mysql -u root -p
Copy after login

This will prompt you for your password. Once you enter the correct password, you can enter the mysql command line.

  1. Create MySQL database

Before we start using Node.js to connect to MySQL, we need to create a database in MySQL first. From the MySQL command line, you can use the following command to create a database:

CREATE DATABASE mydatabase;
Copy after login

This will create a database named "mydatabase".

  1. Connecting to MySQL database

To connect to MySQL database in Node.js, you need to use the mysql driver. Before using the mysql driver, you need to install it using npm. Enter the following command on the command line to install the mysql driver:

npm install mysql
Copy after login

After installing the driver, you can use the following code to connect to the MySQL database:

var mysql = require('mysql');
var connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'mydatabase'
});

connection.connect(function(err) {
  if (err) throw err;
  console.log('Connected!');
});
Copy after login

In the above code, we used " mysql" module and creates a MySQL connection object. We need to pass the MySQL connection parameters such as hostname, username, password and database name to the createConnection() method. In the connect() method, we open the MySQL connection. If the connection is successful, a "Connected!" message is printed.

  1. Query MySQL data

After connecting to the MySQL database, we can use the following code to query MySQL data:

connection.query('SELECT * FROM mytable', function (error, results, fields) {
  if (error) throw error;
  console.log(results);
});
Copy after login

In the above code, we Use the query() method to query MySQL data. We pass the SELECT statement as a parameter of the query() method and process the result data in the callback function. In this example, we print the query results.

  1. Insert MySQL data

In the Node.js environment, you can use the following code to insert data into the MySQL database:

var sql = "INSERT INTO mytable (name, email) VALUES ('John', 'john@example.com')";
connection.query(sql, function (error, result) {
  if (error) throw error;
  console.log("1 record inserted");
});
Copy after login

In the above code, We use INSERT statement to insert data into the MySQL database. We pass the INSERT statement as a parameter of the query() method and process the result data in the callback function. In this example, we will print "1 record inserted".

  1. Update MySQL data

In the Node.js environment, you can use the following code to update the data of the MySQL table:

var sql = "UPDATE mytable SET email = 'peter@example.com' WHERE name = 'Peter'";
connection.query(sql, function (error, result) {
  if (error) throw error;
  console.log(result.affectedRows + " record(s) updated");
});
Copy after login

In the above code, We use the UPDATE statement to update the data. We pass the UPDATE statement as a parameter of the query() method and process the result data in the callback function. In this example, we will print "1 record updated".

  1. Delete MySQL data

In the Node.js environment, you can use the following code to delete the data of the MySQL table:

var sql = "DELETE FROM mytable WHERE name = 'John'";
connection.query(sql, function (error, result) {
  if (error) throw error;
  console.log(result.affectedRows + " record(s) deleted");
});
Copy after login

In the above code, We delete data using DELETE statement. We pass the DELETE statement as a parameter of the query() method and process the result data in the callback function. In this example, we will print "1 record deleted".

Summary

It’s very easy to use MySQL in Node.js. We just need to use the mysql driver and Node.js built-in methods to connect, query, insert, update and delete MySQL data. These operations are very important for web developers because the database is one of the core components in web applications.

The above is the detailed content of Nodejs connection mysql steps. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template