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.
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
This will prompt you for your password. Once you enter the correct password, you can enter the mysql command line.
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;
This will create a database named "mydatabase".
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
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!'); });
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.
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); });
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.
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"); });
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".
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"); });
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".
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"); });
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!