Home > Web Front-end > Front-end Q&A > nodejs mysql installation configuration

nodejs mysql installation configuration

WBOY
Release: 2023-05-12 10:10:36
Original
573 people have browsed it

In web application development, a database is essential. Whether used for user authentication, application data storage, or other functions, the database is a core component. As a popular relational database, MySQL is one of the first choices for web developers, and Node.js, as a JavaScript back-end language, has also become a popular choice in web development. This article will introduce how to use the MySQL database in Node.js, covering the installation and configuration of MySQL.

  1. Installing MySQL

Before you start using the MySQL database, you need to install the MySQL server. MySQL officially provides multiple installation packages and different installation methods. Here we take Windows as an example to illustrate.

The first step is to download the MySQL installation package. On the MySQL official website download page, select MySQL Community Server and download the version corresponding to your operating system.

The second step is to install MySQL. Double-click the downloaded installation package and follow the prompts to install it. Remember the password you set, which is required for subsequent access to the database.

  1. Install MySQL module

Node.js officially provides a MySQL module that can interact with the MySQL database. We can install it using NPM (Node.js package manager) in the terminal.

Enter the following command in the terminal:

npm install mysql
Copy after login

This command will automatically download and install the MySQL module, allowing you to interact with the MySQL database.

  1. Connecting to MySQL database

To interact with the MySQL database, you first need to initialize the connection to the database in the Node.js code. You can use the createConnection() method to create a database connection object and use the object to execute queries.

The following code demonstrates how to connect to a MySQL database:

var mysql = require('mysql');
var con = mysql.createConnection({
  host: "localhost", //服务器地址
  user: "yourusername", //用户名
  password: "yourpassword" //密码
});

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

Please make sure to replace the corresponding server address, username, and password with your MySQL server address, username, and password. The con.connect() function in the code will connect to the MySQL server. If the connection is successful, "Connected!" will be output.

  1. Create Database

Once connected to the MySQL server, you need to create a database to store your data. You can use Node.js to create a new database. The following code demonstrates how to create a database named "mydb":

con.query("CREATE DATABASE mydb", function (err, result) {
  if (err) throw err;
  console.log("Database created");
});
Copy after login

When executing the query() function, the function will try to execute on the MySQL server SQL statement. In this example, the SQL statement is a query to create a database named "mydb". If executed successfully, the query will output "Database created".

  1. Create table

Before inserting data into the database, you need to create a table to store the data. The following code demonstrates how to create a table named "customers", which contains fields such as ID, name, and address:

var sql = "CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))";
con.query(sql, function (err, result) {
  if (err) throw err;
  console.log("Table created");
});
Copy after login

Before executing the query, please make sure to use the correct database and table names, and the correct field.

  1. Insert Data

Once the database and table have been created, data can be inserted into the table. The following example demonstrates how to insert a row of data into a table named "customers":

var sql = "INSERT INTO customers (name, address) VALUES ('Company Inc', 'Highway 37')";
con.query(sql, function (err, result) {
  if (err) throw err;
  console.log("1 record inserted");
});
Copy after login

This will insert a new row of data into the table named "customers".

  1. Querying Data

Now that the data has been inserted, you can use Node.js to query the data stored in the database. Here is the sample code on how to query to retrieve all column data from the table named "customers":

con.query("SELECT * FROM customers", function (err, result, fields) {
  if (err) throw err;
  console.log(result);
});
Copy after login

After executing the query, you will get all the data from the table named "customers", and Query results (result) and fields (fields). In this example, the results are printed to the console.

  1. Conclusion

By using Node.js and MySQL modules, you can easily connect to the MySQL database and perform database operations such as query, insert, and update. Node.js is a fast and flexible back-end language that combined with MySQL will bring more efficient processing performance and faster response speed to web applications.

The above is the detailed content of nodejs mysql installation configuration. 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