Spec-Zone.ru › MySQL Connectors 1.0

2.2.1 Подключение к отдельному узлу MySQL

В этом примере устанавливается подключение к локальному экземпляру MySQL Server, на котором запущен плагин X на стандартном TCP/IP порту 33060, используя учетную запись пользователя MySQL user с её паролем. Поскольку другие параметры не заданы, используются значения по умолчанию.

Код 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')
	

Следующий пример демонстрирует, как подключиться к отдельному экземпляру MySQL Server, указав TCP/IP адрес “localhost” и ту же учетную запись пользователя, что и ранее. В этом случае вам будет предложено ввести имя пользователя и пароль.

Код 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://localhost: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://localhost: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");

© 2025 Oracle
Licensed under the GPLv2 License.
https://docs.oracle.com/cd/E17952_01/x-devapi-userguide-en/connecting-to-a-single-mysqld-node-setup.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API