Documentation Home
X DevAPI User Guide for MySQL Shell in JavaScript Mode
Download this Manual
PDF (US Ltr) - 1.2Mb
PDF (A4) - 1.2Mb

2.2.1 Connecting to a Single MySQL Server

In this example a connection to a local MySQL Server instance running X Plugin on the default TCP/IP port 33060 is established using the MySQL user account user with its password. As no other parameters are set, default values are used.

// Passing the parameters in the { param: value } format
var dictSession = mysqlx.getSession( {
        host: 'localhost', 'port': 33060,
        user: 'user', password: 'password' } )

var db1 = dictSession.getSchema('test')

// Passing the parameters in the URI format
var uriSession = mysqlx.getSession('user:password@localhost:33060')

var db2 = uriSession.getSchema('test')

The following example shows how to connect to a single MySQL Server instance by providing a TCP/IP address localhost and the same user account as before. You are prompted to enter the user name and password in this case.

// Passing the parameters in the { param: value } format
// Query the user for the account information
print("Please enter the database user information.");
var usr = shell.prompt("Username: ", {defaultValue: "user"});
var pwd = shell.prompt("Password: ", {type: "password"});

// Connect to MySQL Server on a network machine
mySession = mysqlx.getSession( {
        host: 'localhost', 'port': 33060,
        user: usr, password: pwd} );

myDb = mySession.getSchema('test');