Node.js is a very popular server-side JavaScript runtime environment that allows us to build high-performance network applications using JavaScript. In Node.js, it is very common to use the MySQL database because MySQL is a fast, reliable, and easy-to-use open source relational database.
In this article, we will explore how to set up MySQL in Node.js. We'll cover how to install and configure MySQL and Node.js, and see how to use Node.js to connect to a MySQL database and perform basic operations.
Installation and Configuration of MySQL
First, we need to install MySQL and configure it to allow us to connect from our Node.js application. Here we will show how to install MySQL on Ubuntu operating system.
The way to install MySQL on Ubuntu is through the apt-get package manager. We can open Ubuntu's terminal and enter the following command to install MySQL:
sudo apt-get update sudo apt-get install mysql-server
After the installation is complete, we need to set a password and omit remote access. You can do this by using the following command:
sudo mysql_secure_installation
This command will prompt you to enter the MySQL root user password, and once completed, it will ask you to set a new password. It is recommended that you use a strong password.
Next, we need to ensure that the MySQL service is started and running. We can use the following command to start the MySQL service:
sudo systemctl start mysql
If you need to stop the MySQL service, you can use the following command:
sudo systemctl stop mysql
MySQL configuration
Now we have succeeded Installed and configured MySQL. Next, we need to use the MySQL Command Line Interface (CLI) to connect to MySQL and create a new database. We can connect to the MySQL CLI using the following command:
mysql -u root -p
This will prompt you for the password of the MySQL root user. Once completed, you should see the MySQL CLI prompt indicating that you have successfully connected to MySQL.
To create a new database, use the following command:
CREATE DATABASE my_database;
Now our MySQL database is ready to use and we can start connecting to it using Node.js.
Connecting MySQL with Node.js
We have installed and configured MySQL, now we are ready to connect it using Node.js. Here we will use the node-mysql driver to connect to the MySQL database.
First, we need to install the node-mysql driver. We can use npm (Node.js package manager) to install node-mysql. In the terminal, please enter the following command to install:
npm install mysql
After the installation is complete, we can use the require statement to introduce the node-mysql driver into our Node.js application. In a Node.js application, we can introduce the driver using the following command:
const mysql = require('mysql');
Now, we are ready to connect to MySQL and perform some basic operations.
The following is a simple example to connect to a MySQL database through Node.js, open a new connection, query some data, close the connection and query the results:
const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'my_database' }); connection.connect(); connection.query('SELECT * from my_table', function (error, results, fields) { if (error) throw error; console.log('The solution is: ', results[0].solution); }); connection.end();
In this example , we first created a new MySQL connection using the mysql.createConnection method. We specified the MySQL server's host name (host), user name (user), password (password), and the database to be used.
Next, we call the connection.connect() method to open the connection. We then executed a simple SELECT query in the MySQL database that fetched all the data in the my_table table. Finally, we close the connection using the connection.end() method.
Conclusion
This article shows how to connect to a MySQL database in a Node.js application. We install and configure MySQL, and then use the node-mysql driver to connect to MySQL and perform simple query operations.
The combination of Node.js and MySQL is very powerful and can be used to build a variety of real-time applications and web applications. Hopefully this article will help you get started with Node.js and MySQL and build faster, more efficient web applications.
The above is the detailed content of How to set up mysql in nodejs. For more information, please follow other related articles on the PHP Chinese website!