如何将应用程序连接到 Azure Database for MariaDBHow to connect applications to Azure Database for MariaDB

本主题列出了 Azure Database for MariaDB 支持的连接字符串类型,并收录了模板和示例。This topic lists the connection string types that are supported by Azure Database for MariaDB, together with templates and examples. 你可能会在连接字符串中使用不同的参数和设置。You might have different parameters and settings in your connection string.

  • 若要获取证书,请参阅如何配置 SSLTo obtain the certificate, see How to configure SSL.
  • {your_host} = [servername].mariadb.database.chinacloudapi.cn{your_host} = [servername].mariadb.database.chinacloudapi.cn
  • {your_user}@{servername} = userID 格式是用于身份验证的正确格式。{your_user}@{servername} = userID format for authentication correctly. 如果只使用 userID,身份验证将失败。If you only use the userID, the authentication will fail.

ADO.NETADO.NET

Server={your_host}; Port=3306; Database={your_database}; Uid={username@servername}; Pwd={your_password}; SslMode=Preferred;

在此示例中,服务器名称为 mydemoserver,数据库名称为 wpdb,用户名为 WPAdmin,密码为 mypassword!2In this example, the server name is mydemoserver, the database name is wpdb, the user name is WPAdmin, and the password is mypassword!2. 因此,连接字符串应为:As a result, the connection string should be:

Server= "mydemoserver.mariadb.database.chinacloudapi.cn"; Port=3306; Database= "wpdb"; Uid= "WPAdmin@mydemoserver"; Pwd="mypassword!2"; SslMode=Required;

JDBCJDBC

String url ="jdbc:mariadb://{your_host}:3306/{your_database}?useSSL=true&trustServerCertificate=true"; myDbConn = DriverManager.getConnection(url, "{username@servername}", {your_password});

Node.jsNode.js

var conn = mysql.createConnection({host: "{your_host}", user: "{your_username}", password: {your_password}, database: {your_database}, port: 3306, ssl:{ca:fs.readFileSync({ca-cert filename})}});

PHPPHP

$con=mysqli_init(); mysqli_ssl_set($con, NULL, NULL, {ca-cert filename}, NULL, NULL); mysqli_real_connect($con, "{your_host}", "{username@servername}", {your_password}, {your_database}, 3306);

PythonPython

cnx = mysql.connector.connect(user="{username@servername}", password={your_password}, host="{your_host}", port=3306, database={your_database}, ssl_ca={ca-cert filename}, ssl_verify_cert=true)

RubyRuby

client = Mysql2::Client.new(username: "{username@servername}", password: {your_password}, database: {your_database}, host: "{your_host}", port: 3306, sslca:{ca-cert filename}, sslverify:false, sslcipher:'AES256-SHA')

从 Azure 门户获取连接字符串详细信息Get the connection string details from the Azure portal

Azure 门户中,转到 Azure Database for MariaDB 服务器,再单击“连接字符串” ,以获取实例的字符串列表:Azure 门户中的“连接字符串”窗格In the Azure portal, go to your Azure Database for MariaDB server, and then click Connection strings to get the string list for your instance: The Connection strings pane in the Azure portal

字符串提供驱动程序、服务器和其他数据库连接参数等详细信息。The string provides details such as the driver, server, and other database connection parameters. 修改这些示例以使用自己的参数,例如数据库名称、密码等。Modify these examples to use your own parameters, such as database name, password, and so on. 然后,可以在代码和应用程序中使用此字符串连接到服务器。You can then use this string to connect to the server from your code and applications.