Documentation Home
X DevAPI User Guide
Download this Manual

X DevAPI User Guide  /  ...  /  Connecting to a Single MySQL Server

Pre-General Availability Draft: 2017-07-20

2.4.1.1 Connecting to a Single MySQL Server

In some cases, like prototyping or applications running against a single MySQL Server, it can be helpful to directly specify the host explicitly in the connection string. The following examples show how this is done.

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

MySQL Shell JavaScript Code

// Passing the parameters in the { param: value } format
var dictSession = mysqlx.getSession( {
  host: 'localhost',
  port: 33060,
  dbUser: 'mike',
  dbPassword: 's3cr3t!' } );

var db1 = dictSession.getSchema('test');

// Passing the parameters in the URL format
var uriSession = mysqlx.getSession('mike:s3cr3t!@localhost:33060');

var db2 = uriSession.getSchema('test');

MySQL Shell Python Code

# Passing the parameters in the { param: value } format
dictSession = mysqlx.get_node_session( {
        'host': 'localhost', 'port': 33060,
        'dbUser': 'mike', 'dbPassword': 's3cr3t!' } )

db1 = dictSession.get_schema('test')

# Passing the parameters in the URL format
uriSession = mysqlx.get_node_session('mike:s3cr3t!@localhost:33060')

db2 = uriSession.get_schema('test')

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

MySQL Shell JavaScript Code

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

// Connect to MySQL Server running X Plugin on a network machine
var mySession = mysqlx.getSession( {
  host: 'localhost', port: 33060,
  dbUser: usr, dbPassword: pwd} );

var myDb = mySession.getSchema('test');

MySQL Shell Python Code

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

# Connect to MySQL Server on a network machine
mySession = mysqlx.get_node_session( {
        'host': 'localhost', 'port': 33060,
        'dbUser': usr, 'dbPassword': pwd} )

myDb = mySession.get_schema('test')

C# Code

// Passing the parameters in the { param: value } format
// Query the use for the user information
Console.WriteLine("Please enter the database user information.");
Console.Write("Username: ");
var usr = Console.ReadLine();
if (string.IsNullOrEmpty(usr)) usr = "mike";
Console.Write("Password: ");
var pwd = Console.ReadLine();
if (string.IsNullOrEmpty(pwd)) pwd = "s3cr3t!";

// Connect to MySQL Server on a network machine
var mySession = MySQLX.GetSession(string.Format("host=localhost; port=33060; User={0}; Password={1};", usr, pwd));

var myDb = mySession.GetSchema("test");

Java Code

import com.mysql.cj.api.xdevapi.*;
import com.mysql.cj.xdevapi.*;

// Connect to server on localhost using a connection URL
XSession mySession = new XSessionFactory().getSession("mysqlx://localhost:33060/test?user=mike&password=s3cr3t!");

Schema myDb = mySession.getSchema("test");

C++ Code

// This code sample assumes that we have function prompt() defined somewhere.

string usr = prompt("Username:");
string pwd = prompt("Password:");

// Connect to MySQL Server on a network machine
XSession mySession(SessionSettings::HOST, "localhost",
                   SessionSettings::PORT, 33060,
                   SessionSettings::USER, usr,
                   SessionSettings::PWD, pwd);

// An alternative way of defining session settings.

SessionSettings settings;

settings[SessionSettings::HOST] = "localhost";
settings[SessionSettings::PORT] = 33060;
settings[SessionSettings::USER] = usr;
settings[SessionSettings::PWD]  = pwd;

XSession mySession(settings);

Schema myDb= mySession.getSchema("test");

C Code


User Comments
Sign Up Login You must be logged in to post a comment.