Home > Article > Web Front-end > mac nodejs installation database
When developing web applications, the database is a very important part, and using nodejs to install the database on the mac operating system is a basic skill. This article will introduce how to use nodejs to install a database on mac.
1. Install Node.js
Installing nodejs on mac is the first step. You can download the corresponding version from the nodejs official website https://nodejs.org/zh-cn/ for installation. . After the installation is successful, you can enter node -v on the command line to view the nodejs version number. If the version number can be output, the installation is successful.
2. Install the database
There are many ways to install the database on mac. The following are two common ways:
MongoDB is a scalable, high-performance, open source database. You can download the corresponding version from the official website https://www.mongodb.com/download-center/community and install it.
After the installation is complete, enter the "mongod" command in the terminal to start the MongoDB service. After the installation is successful, you can use the following command to verify:
mongo --version
If the version information appears after entering the command, the installation is successful.
MySQL is a lightweight, high-performance, open source database that can be downloaded from the official website https://dev.mysql.com/downloads/ mysql/Download the corresponding version and install it.
After the installation is completed, enter the following command in the terminal to start the MySQL service:
sudo /usr/local/mysql/support-files/mysql.server start
After the startup is successful, you can use the following command to verify:
mysqladmin --version
After entering the command, if If the version information appears, the installation is successful.
3. Establish a database connection
Before using nodejs to interact with the database, you need to establish a connection with the database first.
To connect to MongoDB in nodejs, you need to use the officially provided MongoDB driver. You can install it through the following command:
npm install mongodb --save
After successful installation, you can use the following code to establish a connection:
const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://localhost:27017/test'; MongoClient.connect(url, function(err, client) { if(err) { console.log("Failed to connect MongoDB."); } else{ console.log("Success to connect MongoDB."); } client.close(); });
In nodejs To connect to MySQL, you need to use the officially provided MySQL driver. You can install it through the following command:
npm install mysql --save
After successful installation, you can use the following code to establish a connection:
const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'root', database: 'test' }); connection.connect(function(err) { if (err) { console.error('Failed to connect MySQL.', err); } console.log('Success to connect MySQL.'); });
4. Summary
The above is the installation using nodejs in mac Database methods include installing Node.js, installing MongoDB and MySQL, and establishing a connection to the database. These steps are very basic skills for developing web applications.
The above is the detailed content of mac nodejs installation database. For more information, please follow other related articles on the PHP Chinese website!