X DevAPI 用户指南  / 第 2 章连接和会话概念  / 2.2 连接到会话  /  2.2.1 连接到单个 MySQL 服务器

2.2.1 连接到单个 MySQL 服务器

user在此示例中,使用 MySQL 用户帐户及其密码 建立与在默认 TCP/IP 端口 33060 上运行 X 插件的本地 MySQL 服务器实例的连接 。由于没有设置其他参数,因此使用默认值。

// 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')

以下示例显示如何通过提供 TCP/IP 地址“ localhost和与之前相同的用户帐户 连接到单个 MySQL 服务器实例 。在这种情况下,系统会提示您输入用户名和密码。

// 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');