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

备注

将要查看的是 Azure Database for MySQL 的新服务。You are viewing the new service of Azure Database for MySQL. 若要查看经典 MySQL Database for Azure 的文档,请访问此页To view the documentation for classic MySQL Database for Azure, please visit this page.

本主题列出了 Azure Database for MySQL 支持的连接字符串以及模板和示例。This topic lists the connection string types that are supported by Azure Database for MySQL, 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} = .mysql.database.chinacloudapi.cn{your_host} = .mysql.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={your_port};Database={your_database};Uid={username@servername};Pwd={your_password};[SslMode=Required;]

在此示例中,服务器名称为 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.mysql.database.chinacloudapi.cn"; Port=3306; Database= "wpdb"; Uid= "WPAdmin@mydemoserver"; Pwd="mypassword!2"; SslMode=Required;

JDBCJDBC

String url ="jdbc:mysql://%s:%s/%s[?verifyServerCertificate=true&useSSL=true&requireSSL=true]",{your_host},{your_port},{your_database}"; myDbConn = DriverManager.getConnection(url, {username@servername}, {your_password}";

Node.jsNode.js

var conn = mysql.createConnection({host: {your_host}, user: {username@servername}, password: {your_password}, database: {your_database}, Port: {your_port}[, ssl:{ca:fs.readFileSync({ca-cert filename})}}]);

ODBCODBC

DRIVER={MySQL ODBC 5.3 UNICODE Driver};Server={your_host};Port={your_port};Database={your_database};Uid={username@servername};Pwd={your_password}; [sslca={ca-cert filename}; sslverify=1; Option=3;]

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}, {your_port});

PythonPython

cnx = mysql.connector.connect(user={username@servername}, password={your_password}, host={your_host}, port={your_port}, 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: {your_port}[, sslca:{ca-cert filename}, sslverify:false, sslcipher:'AES256-SHA'])

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

Azure 门户中,转到 Azure Database for MySQL 服务器,再单击“连接字符串”,以获取实例的字符串列表:Azure 门户中的“连接字符串”窗格In the Azure portal, go to your Azure Database for MySQL 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.

后续步骤Next steps