Este tópico descreve como usar PHP para se conectar a um cluster do AnalyticDB for MySQL.
Precauções
Em sistemas operacionais Linux, instale o php-mysql 5.1.x.
Em sistemas operacionais Windows, instale o php_MySQL.dll.
Para se conectar a um cluster do AnalyticDB for MySQL pela Internet, adicione o endereço IP do dispositivo de origem à lista de permissões do cluster AnalyticDB for MySQL. Para mais informações, consulte Configure a whitelist.
Use MySQLi para se conectar a um cluster do AnalyticDB for MySQL
//The endpoint of the cluster. You can obtain the endpoint on the Cluster Information page in the AnalyticDB for MySQL console.
$ads_server_name="am-bp***.ads.aliyuncs.com";
//The account used to connect to the cluster. offers the following types of accounts: privileged accounts and standard accounts.
$ads_username="account_name";
//The password of the account used to connect to the cluster.
$ads_password="account_password";
//The name of the database in the cluster.
$ads_database="db_name";
//The port used to connect to the cluster.
$ads_port=3306;
//Connect to the cluster.
$ads_conn=mysqli_connect($ads_server_name,$ads_username,$ads_password,$ads_database, $ads_port);
$strsql="SELECT user_id FROM my_ads_db.my_first_table limit 20;";
$result=mysqli_query($ads_conn, $strsql);
while($row = mysqli_fetch_array($result)) {
//Obtain data in the user_id column.
echo $row["user_id"];
}
Use PDO para se conectar a um cluster do AnalyticDB for MySQL
Nota
Para obter informações sobre como ativar o PrepareStatement ao usar
para se conectar a um cluster do AnalyticDB for MySQL, consulte a seção "PDO" em
Ative PrepareStatement for a client in different programming languages
.
//The endpoint of the cluster. You can obtain the endpoint on the Cluster Information page in the AnalyticDB for MySQL console.
$ads_server_name = "am-bp***.ads.aliyuncs.com";
//The account used to connect to the cluster. offers the following types of accounts: privileged accounts and standard accounts.
$ads_username = "account_name";
//The password of the account used to connect to the cluster.
$ads_password = "account_password";
//The name of the database in the cluster.
$ads_database = 'db_name';
//The port used to connect to the cluster.
$ads_port = 3306;
$dsn = "mysql:host={$ads_server_name};dbname={$ads_database};port={$ads_port}";
try {
$dbh = new PDO($dsn, $ads_username, $ads_password);
echo 'PDO Success !';
} catch (PDOException $e) {
echo 'PDO Connection failed: ' . $e->getCode() ."\n" . $e->getMessage() ."\n". $e->getTraceAsString();
}