Skip to main content
Version: v3

LeanDB MySQL Guide

LeanDB MySQL is the hosted database provided by Cloud Engine. You can have your project connect to the database using MySQL libraries and have access to all the functions provided by MySQL. See Cloud Engine Overview to learn about the other hosted database services provided by Cloud Engine.

Creating and Managing Instances

You can create and manage LeanDB MySQL instances on Developer Center > Your game > Game Services > Cloud Services > Cloud Engine > Database > MySQL.

Creating Instances

caution
Your account will start to be billed as soon as you create a LeanDB instance. Please make sure there’s enough balance in your account.

You will see the following options when you click Create instance:

  • Name Used to access this instance from Cloud Engine. Should be unique in the application.
  • Memory You can choose from 0.5GB, 1GB, 2GB, and 4GB.
  • Storage You can choose from 20G, 100G, and 500G.

You’ll see the price of the current instance once you choose a specification.

More about the billing of LeanDB

LeanDB instances are charged every day. If you use an instance for less than one day, you will be charged for one day’s usage. You will be billed for your usage of the previous day every single day. You will be charged for your LeanDB instances based on the specification you chose. Even though you don’t use the service after you create an instance, you will still be billed. You will see the prices of different specifications when you create your LeanDB instance. You can also view the prices on the pricing page. You can view your bill history on the Transactions page on the Developer Center.

danger
If your account maintains a negative balance for more than a month, all the LeanDB instances in your account, including their data, will be deleted.

MySQL Version

LeanDB only provides MySQL 5.6 at this time.

Resizing Online

At this time, you can’t resize LeanDB MySQL instances on your own. Please reach out to us if you need to have your instances resized.

Sharing Instances

You can use the “Manage sharing” function to share your LeanDB instances with other applications. When you share an instance with another application, the instance will appear in this application. The relevant environment variables will also be available in this application’s Cloud Engine instances.

Accessing From Cloud Engine

When you deploy a project to the Cloud Engine instances under an application, some environment variables containing information for connecting to MySQL will be injected, including:

  • MYSQL_HOST_<NAME>
  • MYSQL_PORT_<NAME>
  • MYSQL_ADMIN_USER_<NAME>
  • MYSQL_ADMIN_PASSWORD_<NAME>

Here <NAME> is the name you provided when creating your LeanDB instance. If the name of your LeanDB instance is MYRDB, there will be an environment variable named MYSQL_HOST_MYRDB (together with the other three).

To connect to MySQL from Node.js:

const mysql = require("mysql");
const Promise = require("bluebird");

const mysqlPool = Promise.promisifyAll(
mysql.createPool({
host: process.env["MYSQL_HOST_MYRDB"],
port: process.env["MYSQL_PORT_MYRDB"],
user: process.env["MYSQL_ADMIN_USER_MYRDB"],
password: process.env["MYSQL_ADMIN_PASSWORD_MYRDB"],
database: "test",
connectionLimit: 10,
})
);

mysqlPool
.queryAsync("SELECT 1 + 1 AS solution")
.then((rows) => {
console.log("The solution is", rows[0].solution);
})
.catch((err) => {
console.error(err);
});
  • Make sure to install the dependencies needed by running npm install --save mysql bluebird
  • See mysqljs/mysql for more information

Managing Data

Besides accessing LeanDB with your code from Cloud Engine, you can also use the following ways to manage, debug, or perform operations on your instances.

Admin Panel

To make it easy for you to develop and debug, we provide a web interface for you to manage your MySQL instance. You can access the web interface by clicking “Admin panel” on the dashboard.

You can run SQL to query and update your data, create and manage databases, and create and manage indices.

Connecting With the CLI

You can open an interactive shell connected to LeanDB using tds db shell, a command provided by the CLI:

$ tds db shell mysqldb
Welcome to the MySQL monitor.
Your MySQL connection id is 3450564
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test
Database changed
mysql> insert into users set name = 'leancloud';
Query OK, 1 row affected (0.04 sec)
mysql> select * from users;
+------+-----------+
| id | name |
+------+-----------+
| 1 | zhenzhen |
| 2 | leancloud |
+------+-----------+
2 rows in set (0.06 sec)

With tds db proxy, you can export LeanDB to a local port and have local programs or GUI clients connect to the database:

$ tds db proxy myredis
[INFO] Now, you can connect myredis via [redis-cli -h 127.0.0.1 -a hsdt9wIAuKcTZmpg -p 5678]

As long as you keep the terminal open, you’ll be able to access LeanDB from the port 5678. You can use a GUI client to browse and interact with LeanDB. While running your project with tds up, you can also have your program connected to LeanDB using this feature. You can set the environment variable (from the output of tds db proxy):

export REDIS_URL_myredis=redis://default:[email protected]:5678
note

You should only use tds db for developing and debugging locally. Don’t use it for the production environment, as the connection might be interrupted occasionally.