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.
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.
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
This command will automatically download and install the MySQL module, allowing you to interact with the 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!"); });
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.
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"); });
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".
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"); });
Before executing the query, please make sure to use the correct database and table names, and the correct field.
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"); });
This will insert a new row of data into the table named "customers".
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); });
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.
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!