If you run into troubles, got a suggestion, or issue, please e-mail editor@webtigerteam.com

# Databases

Databases are useful for persistently storing structured (and relational) data.

The cloud editor has built in support for mySQL databases.

## Creating a database

In the menu, click "Database manager", and then click `Create new database`.
You then get a dialog box where you type the name of the database.

## Connecting to a database from a Node.JS script

Use the mysql2 Node.JS module. See example code below:

var mysql = require('mysql2'); var db = mysql.createConnection({ user: 'your_username', socketPath: '/sock/mysql', authSwitchHandler: true, // Need to be true:ish database: "information_schema" });

No password is required!

## Structured Query Language (SQL)

Interactions with a mySQL database are done using SQL queries.

### Making a database query from a Node.JS script

Assuming you have already made a connection:

db.query("SELECT foo FROM bar", function(err, rows, fields) { if (err) { // Handle error //console.error(err); } else { console.log(rows); } });

The mysql2 module supports Promises and thus async/await. So you can also make a database query like this:

const [rows,fields] = await db.query("SELECT foo FROM bar");

### Running a database query from the Editor

1. In the menu, click "Database manager".
2. Select which database to run the query on.
3. Write the SQL query somewhere, then select it.
4. Click `Run selected text as query` in the database manager widget.

The result from the query will show up in a new file.
You can write additional SQL queries in that file, and the result will be appended to the end of the file.

### Importing data, running many queries

Save the queries to a file. Then run the following query in the editor:

source /path/to/file.sql

## Misc

### Passwordless authentication (auth_socket)

The editor uses auth_socket mySQL plugin for authentication to the database to make it possible to login securely to the mySQL server without using a password.

The only mysql module I have found that works with auth_socket is mysql2.
And you have to specify authSwitchHandler in the connection options. See example above.

- ⬅Go back
- ⬆Index