文件首頁
X DevAPI 使用者指南
下載本手冊
PDF (美式信紙) - 1.4Mb
PDF (A4) - 1.4Mb


X DevAPI 使用者指南  /  ...  /  連接至單一 MySQL 伺服器

2.2.1 連接至單一 MySQL 伺服器

在此範例中,使用 MySQL 使用者帳戶 user 及其密碼,建立與在預設 TCP/IP 連接埠 33060 上執行 X 外掛程式的本機 MySQL 伺服器執行個體的連線。由於未設定其他參數,因此會使用預設值。

MySQL Shell JavaScript 程式碼

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

MySQL Shell Python 程式碼

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

db1 = dictSession.get_schema('test')

# Passing the parameters in the URI format
uriSession = mysqlx.get_session('user:password@localhost:33060')

db2 = uriSession.get_schema('test')

以下範例示範如何透過提供 TCP/IP 位址 localhost 和與先前相同的使用者帳戶來連線至單一 MySQL 伺服器執行個體。在這種情況下,系統會提示您輸入使用者名稱和密碼。

MySQL Shell JavaScript 程式碼

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

MySQL Shell Python 程式碼

# 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': "user"})
pwd = shell.prompt("Password: ", {'type': "password"})

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

myDb = mySession.get_schema('test')

Node.js JavaScript 程式碼

// Passing the parameters in the { param: value } format
mysqlx.getSession({ host: 'localhost', port: 33060, user: 'user', password: 'password' })
  .then(function (dictSession) {
    var db1 = dictSession.getSchema('test')
  })
// Passing the parameters in the URI format
mysqlx.getSession('user:password@localhost:33060')
  .then(function (uriSession) {
    var db2 = uriSession.getSchema('test')
  })

C# 程式碼

// Query the user for the user information
Console.WriteLine("Please enter the database user information.");
Console.Write("Username: ");
var usr = Console.ReadLine();
Console.Write("Password: ");
var pwd = Console.ReadLine();
         
// Connect to server on localhost using a connection URI
var mySession = MySQLX.GetSession(string.Format("mysqlx://127.0.0.1:33060/test?user={0}&password={1}", usr, pwd));

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

Python 程式碼

# Passing the parameters in the { param: value } format
dict_session = mysqlx.get_session({
    'host': 'localhost', 'port': 33060,
    'user': 'user', 'password': 'password'
})

my_schema_1 = dict_session.get_schema('test')

# Passing the parameters in the URI format
uri_session = mysqlx.get_session('user:password@localhost:33060')

my_schema_2 = uri_session.get_schema('test')

Java 程式碼

import com.mysql.cj.xdevapi.*;

// Connect to server on localhost using a connection URI
Session mySession = new SessionFactory().getSession("mysqlx://127.0.0.1:33060/test?user=user&password=password");

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

C++ 程式碼

// 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
Session mySession(SessionOption::HOST, "localhost",
                  SessionOption::PORT, 33060,
                  SessionOption::USER, usr,
                  SessionOption::PWD, pwd);

// An alternative way of defining session settings.

SessionSettings settings(SessionOption::HOST,"localhost",
                         SessionOption::PORT, 33060);

settings.set(SessionOption::USER, usr);
settings.set(SessionOption::PWD, pwd);

Session mySession(settings);

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