title | description | services | author | ms.author | manager | editor | ms.service | ms.custom | ms.devlang | ms.topic | ms.date |
---|---|---|---|---|---|---|---|---|---|---|---|
Connect to Azure Database for MySQL from Node.js |
This quickstart provides several Node.js code samples you can use to connect and query data from Azure Database for MySQL. |
mysql |
jasonwhowell |
jasonh |
kfile |
jasonwhowell |
mysql |
mvc |
nodejs |
quickstart |
11/21/2018 |
This quickstart demonstrates how to connect to an Azure Database for MySQL using Node.js from Windows, Ubuntu Linux, and Mac platforms. It shows how to use SQL statements to query, insert, update, and delete data in the database. This topic assumes that you are familiar with developing using Node.js and that you are new to working with Azure Database for MySQL.
This quickstart uses the resources created in either of these guides as a starting point:
- Create an Azure Database for MySQL server using Azure portal
- Create an Azure Database for MySQL server using Azure CLI
You also need to:
- Install the Node.js runtime.
- Install mysql package to connect to MySQL from the Node.js application.
Depending on your platform, follow the instructions in the appropriate section to install Node.js. Use npm to install the mysql package and its dependencies into your project folder.
-
Visit the Node.js downloads page, and then select your desired Windows installer option.
-
Make a local project folder such as
nodejsmysql
. -
Launch the command prompt, and then change directory into the project folder, such as
cd c:\nodejsmysql\
-
Run the NPM tool to install the mysql library into the project folder.
cd c:\nodejsmysql\ "C:\Program Files\nodejs\npm" install mysql "C:\Program Files\nodejs\npm" list
-
Verify the installation by checking the
npm list
output text. The version number may vary as new patches are released.
-
Run the following commands to install Node.js and npm the package manager for Node.js.
sudo apt-get install -y nodejs npm
-
Run the following commands to create a project folder
mysqlnodejs
and install the mysql package into that folder.mkdir nodejsmysql cd nodejsmysql npm install --save mysql npm list
-
Verify the installation by checking npm list output text. The version number may vary as new patches are released.
-
Enter the following commands to install brew, an easy-to-use package manager for Mac OS X and Node.js.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew install node
-
Run the following commands to create a project folder
mysqlnodejs
and install the mysql package into that folder.mkdir nodejsmysql cd nodejsmysql npm install --save mysql npm list
-
Verify the installation by checking the
npm list
output text. The version number may vary as new patches are released.
Get the connection information needed to connect to the Azure Database for MySQL. You need the fully qualified server name and login credentials.
- Log in to the Azure portal.
- From the left-hand menu in Azure portal, click All resources, and then search for the server you have created (such as mydemoserver).
- Click the server name.
- From the server's Overview panel, make a note of the Server name and Server admin login name. If you forget your password, you can also reset the password from this panel.
- Paste the JavaScript code into text files, and then save it into a project folder with file extension .js (such as C:\nodejsmysql\createtable.js or /home/username/nodejsmysql/createtable.js).
- Launch the command prompt or bash shell, and then change directory into your project folder
cd nodejsmysql
. - To run the application, type the node command followed by the file name, such as
node createtable.js
. - On Windows, if the node application is not in your environment variable path, you may need to use the full path to launch the node application, such as
"C:\Program Files\nodejs\node.exe" createtable.js
Use the following code to connect and load the data by using CREATE TABLE and INSERT INTO SQL statements.
The mysql.createConnection() method is used to interface with the MySQL server. The connect() function is used to establish the connection to the server. The query() function is used to execute the SQL query against MySQL database.
Replace the host
, user
, password
, and database
parameters with the values that you specified when you created the server and database.
const mysql = require('mysql');
var config =
{
host: 'mydemoserver.mysql.database.azure.com',
user: 'myadmin@mydemoserver',
password: 'your_password',
database: 'quickstartdb',
port: 3306,
ssl: true
};
const conn = new mysql.createConnection(config);
conn.connect(
function (err) {
if (err) {
console.log("!!! Cannot connect !!! Error:");
throw err;
}
else
{
console.log("Connection established.");
queryDatabase();
}
});
function queryDatabase(){
conn.query('DROP TABLE IF EXISTS inventory;', function (err, results, fields) {
if (err) throw err;
console.log('Dropped inventory table if existed.');
})
conn.query('CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);',
function (err, results, fields) {
if (err) throw err;
console.log('Created inventory table.');
})
conn.query('INSERT INTO inventory (name, quantity) VALUES (?, ?);', ['banana', 150],
function (err, results, fields) {
if (err) throw err;
else console.log('Inserted ' + results.affectedRows + ' row(s).');
})
conn.query('INSERT INTO inventory (name, quantity) VALUES (?, ?);', ['orange', 154],
function (err, results, fields) {
if (err) throw err;
console.log('Inserted ' + results.affectedRows + ' row(s).');
})
conn.query('INSERT INTO inventory (name, quantity) VALUES (?, ?);', ['apple', 100],
function (err, results, fields) {
if (err) throw err;
console.log('Inserted ' + results.affectedRows + ' row(s).');
})
conn.end(function (err) {
if (err) throw err;
else console.log('Done.')
});
};
Use the following code to connect and read the data by using a SELECT SQL statement.
The mysql.createConnection() method is used to interface with the MySQL server. The connect() method is used to establish the connection to the server. The query() method is used to execute the SQL query against MySQL database. The results array is used to hold the results of the query.
Replace the host
, user
, password
, and database
parameters with the values that you specified when you created the server and database.
const mysql = require('mysql');
var config =
{
host: 'mydemoserver.mysql.database.azure.com',
user: 'myadmin@mydemoserver',
password: 'your_password',
database: 'quickstartdb',
port: 3306,
ssl: true
};
const conn = new mysql.createConnection(config);
conn.connect(
function (err) {
if (err) {
console.log("!!! Cannot connect !!! Error:");
throw err;
}
else {
console.log("Connection established.");
readData();
}
});
function readData(){
conn.query('SELECT * FROM inventory',
function (err, results, fields) {
if (err) throw err;
else console.log('Selected ' + results.length + ' row(s).');
for (i = 0; i < results.length; i++) {
console.log('Row: ' + JSON.stringify(results[i]));
}
console.log('Done.');
})
conn.end(
function (err) {
if (err) throw err;
else console.log('Closing connection.')
});
};
Use the following code to connect and read the data by using an UPDATE SQL statement.
The mysql.createConnection() method is used to interface with the MySQL server. The connect() method is used to establish the connection to the server. The query() method is used to execute the SQL query against MySQL database.
Replace the host
, user
, password
, and database
parameters with the values that you specified when you created the server and database.
const mysql = require('mysql');
var config =
{
host: 'mydemoserver.mysql.database.azure.com',
user: 'myadmin@mydemoserver',
password: 'your_password',
database: 'quickstartdb',
port: 3306,
ssl: true
};
const conn = new mysql.createConnection(config);
conn.connect(
function (err) {
if (err) {
console.log("!!! Cannot connect !!! Error:");
throw err;
}
else {
console.log("Connection established.");
updateData();
}
});
function updateData(){
conn.query('UPDATE inventory SET quantity = ? WHERE name = ?', [200, 'banana'],
function (err, results, fields) {
if (err) throw err;
else console.log('Updated ' + results.affectedRows + ' row(s).');
})
conn.end(
function (err) {
if (err) throw err;
else console.log('Done.')
});
};
Use the following code to connect and read the data by using a DELETE SQL statement.
The mysql.createConnection() method is used to interface with the MySQL server. The connect() method is used to establish the connection to the server. The query() method is used to execute the SQL query against MySQL database.
Replace the host
, user
, password
, and database
parameters with the values that you specified when you created the server and database.
const mysql = require('mysql');
var config =
{
host: 'mydemoserver.mysql.database.azure.com',
user: 'myadmin@mydemoserver',
password: 'your_password',
database: 'quickstartdb',
port: 3306,
ssl: true
};
const conn = new mysql.createConnection(config);
conn.connect(
function (err) {
if (err) {
console.log("!!! Cannot connect !!! Error:");
throw err;
}
else {
console.log("Connection established.");
deleteData();
}
});
function deleteData(){
conn.query('DELETE FROM inventory WHERE name = ?', ['orange'],
function (err, results, fields) {
if (err) throw err;
else console.log('Deleted ' + results.affectedRows + ' row(s).');
})
conn.end(
function (err) {
if (err) throw err;
else console.log('Done.')
});
};
[!div class="nextstepaction"] Migrate your database using Export and Import