MySQL is a very popular relational database management system that is widely used in the fields of WEB development and data management. This article will introduce how to install MySQL on Windows and Linux platforms, and use JavaScript to connect and operate.
1. Install MySQL on Windows
1. Download MySQL
Download the latest version of MySQL from the MySQL official website (https://www.mysql.com/) Community Server. Select the operating system as Windows and download the installation package.
2. Install MySQL
Double-click the downloaded installation package and install according to the prompts. During the installation process, you need to set the password and port number of the root user. By default, port 3306 is used.
3. Start the MySQL service
After the installation is complete, enter the "Start Menu", find the "MySQL" folder, and select "MySQL Command Line Client" to start the MySQL service.
4. Create a database
In the MySQL command line, enter the following command to create a database named "test".
CREATE DATABASE test;
2. Install MySQL on Linux
1. Download MySQL
Enter the following command in the terminal to download the latest version MySQL Community Server.
sudo apt-get update
sudo apt-get install mysql-server
2. Install MySQL
During the installation process, you need to set the root user's password and The port number. By default, port 3306 is used.
3. Start the MySQL service
After the installation is complete, enter the following command to start the MySQL service.
sudo systemctl start mysql
4. Create database
In the MySQL command line, enter the following command to create a database named "test".
CREATE DATABASE test;
3. Use JavaScript to connect to MySQL
1. Install the MySQL module
Enter the following command in the terminal to install Node.js MySQL module.
npm install mysql
2. Connect to MySQL
In the JavaScript file, use the following code to connect to MySQL.
const mysql = require('mysql');
const pool = mysql.createPool({
host: 'localhost', user: 'root', password: 'password', database: 'test', port: 3306
});
3. Query data
In the JavaScript file, use the following code to query the data.
pool.query('SELECT * FROM user', (error, results, fields) => {
if (error) throw error; console.log('Results: ', results);
});
The above code will query the name All data in the data table of "user" and prints the results to the console.
4. Insert data
In the JavaScript file, use the following code to insert data.
const user = { name: 'Tom', age: 18 };
pool.query('INSERT INTO user SET ?', user, (error, results, fields) => ; {
if (error) throw error; console.log('Results: ', results);
});
The above code will insert a piece of data into the data table named "user". The name of the data is "Tom" and the age is "18".
Summary
This article briefly introduces how to install MySQL on Windows and Linux platforms, and use JavaScript to connect and operate. By studying the content of this article, readers can master the basic methods of installing MySQL and using JavaScript to operate, which will provide help for future WEB development and data management.
The above is the detailed content of How to install MySQL on Windows and Linux platforms. For more information, please follow other related articles on the PHP Chinese website!