correlation https://teguhth.blogspot.com/2025/01/test-insert-select-data-using-maxscale.html
1. info server testing
2. diagram
3. create datasource
4. run url
http://10.10.10.90:8888/test_maxscale_route.cfm
5. create lucee script to test
[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat test_maxscale_route.cfm
<cfsetting showdebugoutput="false">
<!DOCTYPE html>
<html>
<head>
<title>MariaDB MaxScale Routing Test</title>
<style>
body{
font-family: Arial, Helvetica, sans-serif;
margin:20px;
}
table{
border-collapse:collapse;
width:100%;
margin-bottom:20px;
}
th,td{
border:1px solid #ccc;
padding:8px;
text-align:left;
}
th{
background:#f2f2f2;
}
.ok{
color:green;
font-weight:bold;
}
.err{
color:red;
font-weight:bold;
}
</style>
</head>
<body>
<h2>MariaDB MaxScale Routing Hint Test</h2>
<!-- ========================================================== -->
<!-- Test 1 : -- maxscale route to master -->
<!-- ========================================================== -->
<cftry>
<cfquery name="qMasterLine" datasource="dbmax">
SELECT
@@hostname AS hostname,
@@version AS version
-- maxscale route to master
</cfquery>
<cfcatch>
<cfset qMasterLine="">
</cfcatch>
</cftry>
<!-- ========================================================== -->
<!-- Test 2 : -- maxscale route to slave -->
<!-- ========================================================== -->
<cftry>
<cfquery name="qSlaveLine" datasource="dbmax">
SELECT
@@hostname AS hostname,
@@version AS version
-- maxscale route to slave
</cfquery>
<cfcatch>
<cfset qSlaveLine="">
</cfcatch>
</cftry>
<!-- ========================================================== -->
<!-- Test 3 : /* maxscale route to master */ -->
<!-- ========================================================== -->
<cftry>
<cfquery name="qMasterBlock" datasource="dbmax">
SELECT
@@hostname AS hostname,
@@version AS version
/* maxscale route to master */
</cfquery>
<cfcatch>
<cfset qMasterBlock="">
</cfcatch>
</cftry>
<!-- ========================================================== -->
<!-- Test 4 : /* maxscale route to slave */ -->
<!-- ========================================================== -->
<cftry>
<cfquery name="qSlaveBlock" datasource="dbmax">
SELECT
@@hostname AS hostname,
@@version AS version
/* maxscale route to slave */
</cfquery>
<cfcatch>
<cfset qSlaveBlock="">
</cfcatch>
</cftry>
[root@teguhth-all ROOT]# pwd
/opt/lucee/tomcat/webapps/ROOT
[root@teguhth-all ROOT]#
[root@teguhth-all ROOT]# cat test_maxscale_route.cfm
<cfsetting showdebugoutput="false">
<!DOCTYPE html>
<html>
<head>
<title>MariaDB MaxScale Routing Test</title>
<style>
body{
font-family: Arial, Helvetica, sans-serif;
margin:20px;
}
table{
border-collapse:collapse;
width:100%;
margin-bottom:20px;
}
th,td{
border:1px solid #ccc;
padding:8px;
text-align:left;
}
th{
background:#f2f2f2;
}
.ok{
color:green;
font-weight:bold;
}
.err{
color:red;
font-weight:bold;
}
</style>
</head>
<body>
<h2>MariaDB MaxScale Routing Hint Test</h2>
<!-- ========================================================== -->
<!-- Test 1 : -- maxscale route to master -->
<!-- ========================================================== -->
<cftry>
<cfquery name="qMasterLine" datasource="dbmax">
SELECT
@@hostname AS hostname,
@@version AS version
-- maxscale route to master
</cfquery>
<cfcatch>
<cfset qMasterLine="">
</cfcatch>
</cftry>
<!-- ========================================================== -->
<!-- Test 2 : -- maxscale route to slave -->
<!-- ========================================================== -->
<cftry>
<cfquery name="qSlaveLine" datasource="dbmax">
SELECT
@@hostname AS hostname,
@@version AS version
-- maxscale route to slave
</cfquery>
<cfcatch>
<cfset qSlaveLine="">
</cfcatch>
</cftry>
<!-- ========================================================== -->
<!-- Test 3 : /* maxscale route to master */ -->
<!-- ========================================================== -->
<cftry>
<cfquery name="qMasterBlock" datasource="dbmax">
SELECT
@@hostname AS hostname,
@@version AS version
/* maxscale route to master */
</cfquery>
<cfcatch>
<cfset qMasterBlock="">
</cfcatch>
</cftry>
<!-- ========================================================== -->
<!-- Test 4 : /* maxscale route to slave */ -->
<!-- ========================================================== -->
<cftry>
<cfquery name="qSlaveBlock" datasource="dbmax">
SELECT
@@hostname AS hostname,
@@version AS version
/* maxscale route to slave */
</cfquery>
<cfcatch>
<cfset qSlaveBlock="">
</cfcatch>
</cftry>
<!-- ========================================================== -->
<!-- Test 5 : /* maxscale route to slave */ -->
<!-- ========================================================== -->
<cftry>
<cfquery name="qnormal" datasource="dbmax">
SELECT
@@hostname AS hostname,
@@version AS version
</cfquery>
<cfcatch>
<cfset qnormal="">
</cfcatch>
</cftry>
<table>
<tr>
<th>No</th>
<th>Routing Hint</th>
<th>Hostname</th>
<th>Version</th>
</tr>
<cfoutput>
<tr>
<td>1</td>
<td>select @@hostname,@@version -- maxscale route to master</td>
<cfif IsQuery(qMasterLine)>
<td>#qMasterLine.hostname#</td>
<td>#qMasterLine.version#</td>
<cfelse>
<td colspan="2" class="err">FAILED</td>
</cfif>
</tr>
<tr>
<td>2</td>
<td>select @@hostname,@@version -- maxscale route to slave</td>
<cfif IsQuery(qSlaveLine)>
<td>#qSlaveLine.hostname#</td>
<td>#qSlaveLine.version#</td>
<cfelse>
<td colspan="2" class="err">FAILED</td>
</cfif>
</tr>
<tr>
<td>3</td>
<td>select @@hostname,@@version /* maxscale route to master */</td>
<cfif IsQuery(qMasterBlock)>
<td>#qMasterBlock.hostname#</td>
<td>#qMasterBlock.version#</td>
<cfelse>
<td colspan="2" class="err">FAILED</td>
</cfif>
</tr>
<tr>
<td>4</td>
<td>select @@hostname,@@version /* maxscale route to slave */</td>
<cfif IsQuery(qSlaveBlock)>
<td>#qSlaveBlock.hostname#</td>
<td>#qSlaveBlock.version#</td>
<cfelse>
<td colspan="2" class="err">FAILED</td>
</cfif>
</tr>
<tr>
<!-- Test 5 : /* maxscale route to slave */ -->
<!-- ========================================================== -->
<cftry>
<cfquery name="qnormal" datasource="dbmax">
SELECT
@@hostname AS hostname,
@@version AS version
</cfquery>
<cfcatch>
<cfset qnormal="">
</cfcatch>
</cftry>
<table>
<tr>
<th>No</th>
<th>Routing Hint</th>
<th>Hostname</th>
<th>Version</th>
</tr>
<cfoutput>
<tr>
<td>1</td>
<td>select @@hostname,@@version -- maxscale route to master</td>
<cfif IsQuery(qMasterLine)>
<td>#qMasterLine.hostname#</td>
<td>#qMasterLine.version#</td>
<cfelse>
<td colspan="2" class="err">FAILED</td>
</cfif>
</tr>
<tr>
<td>2</td>
<td>select @@hostname,@@version -- maxscale route to slave</td>
<cfif IsQuery(qSlaveLine)>
<td>#qSlaveLine.hostname#</td>
<td>#qSlaveLine.version#</td>
<cfelse>
<td colspan="2" class="err">FAILED</td>
</cfif>
</tr>
<tr>
<td>3</td>
<td>select @@hostname,@@version /* maxscale route to master */</td>
<cfif IsQuery(qMasterBlock)>
<td>#qMasterBlock.hostname#</td>
<td>#qMasterBlock.version#</td>
<cfelse>
<td colspan="2" class="err">FAILED</td>
</cfif>
</tr>
<tr>
<td>4</td>
<td>select @@hostname,@@version /* maxscale route to slave */</td>
<cfif IsQuery(qSlaveBlock)>
<td>#qSlaveBlock.hostname#</td>
<td>#qSlaveBlock.version#</td>
<cfelse>
<td colspan="2" class="err">FAILED</td>
</cfif>
</tr>
<tr>
<td>5</td>
<td>select @@hostname,@@version </td>
<cfif IsQuery(qnormal)>
<td>#qnormal.hostname#</td>
<td>#qnormal.version#</td>
<cfelse>
<td colspan="2" class="err">FAILED</td>
</cfif>
<td>select @@hostname,@@version </td>
<cfif IsQuery(qnormal)>
<td>#qnormal.hostname#</td>
<td>#qnormal.version#</td>
<cfelse>
<td colspan="2" class="err">FAILED</td>
</cfif>
</tr>
</cfoutput>
</table>
<div style="font-family:Courier New, monospace;font-size:14px;">
Copyright by : Teguh Triharto<br>
Website :
<a href="https://www.linkedin.com/in/teguhth" target="_blank">
https://www.linkedin.com/in/teguhth
</a>
</div>
</body>
</html>
[root@teguhth-all ROOT]#
6. create .php script to test
[root@teguhth-all html]# pwd
/var/www/html
[root@teguhth-all html]# cat test_routing_maxscale.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
/* ==========================================================
MariaDB MaxScale Routing Hint Test
Copyright : Teguh Triharto
Website : https://www.linkedin.com/in/teguhth
========================================================== */
$host = "10.10.10.15"; // IP MaxScale
$port = 4306;
$db = "mysql";
$user = "admin";
$pass = "xxx";
try {
$pdo = new PDO(
"mysql:host=$host;port=$port;dbname=$db;charset=utf8mb4",
$user,
$pass,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
)
);
} catch (PDOException $e) {
die("Connection Failed : ".$e->getMessage());
}
function runTest($pdo, $sql)
{
try {
$stmt = $pdo->query($sql);
return $stmt->fetch();
} catch (Exception $e) {
return false;
}
}
$tests = array(
array(
"No"=>1,
"Hint"=>"SELECT @@hostname,@@version -- maxscale route to master",
"SQL"=>"SELECT
@@hostname AS hostname,
@@version AS version
-- maxscale route to master"
),
array(
"No"=>2,
"Hint"=>"SELECT @@hostname,@@version -- maxscale route to slave",
"SQL"=>"SELECT
@@hostname AS hostname,
@@version AS version
-- maxscale route to slave"
),
array(
"No"=>3,
"Hint"=>"SELECT @@hostname,@@version /* maxscale route to master */",
"SQL"=>"SELECT
@@hostname AS hostname,
@@version AS version
/* maxscale route to master */"
),
array(
"No"=>4,
"Hint"=>"SELECT @@hostname,@@version /* maxscale route to slave */",
"SQL"=>"SELECT
@@hostname AS hostname,
@@version AS version
/* maxscale route to slave */"
),
array(
"No"=>5,
"Hint"=>"SELECT @@hostname,@@version",
"SQL"=>"SELECT
@@hostname AS hostname,
@@version AS version"
)
);
?>
<!DOCTYPE html>
<html>
<head>
<title>MariaDB MaxScale Routing Test</title>
<style>
body{
font-family:Arial;
margin:20px;
}
table{
border-collapse:collapse;
width:100%;
}
th,td{
border:1px solid #ccc;
padding:8px;
}
th{
background:#f2f2f2;
}
.ok{
color:green;
font-weight:bold;
}
.err{
color:red;
font-weight:bold;
}
</style>
</head>
<body>
<h2>MariaDB MaxScale Routing Hint Test (PHP)</h2>
<table>
<tr>
<th>No</th>
<th>Routing Hint</th>
<th>Hostname</th>
<th>Version</th>
</tr>
<?php
foreach($tests as $t)
{
$r = runTest($pdo,$t["SQL"]);
echo "<tr>";
echo "<td>".$t["No"]."</td>";
echo "<td>".htmlspecialchars($t["Hint"])."</td>";
if($r)
{
echo "<td>".$r["hostname"]."</td>";
echo "<td>".$r["version"]."</td>";
}
else
{
echo "<td colspan='2' class='err'>FAILED</td>";
}
echo "</tr>";
}
?>
</table>
<br>
<div style="font-family:Courier New;font-size:14px">
Copyright by :
Teguh Triharto
<br>
Website :
<a href="https://www.linkedin.com/in/teguhth" target="_blank">
https://www.linkedin.com/in/teguhth
</a>
</div>
</body>
</html>
[root@teguhth-all html]#
/var/www/html
[root@teguhth-all html]# cat test_routing_maxscale.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
/* ==========================================================
MariaDB MaxScale Routing Hint Test
Copyright : Teguh Triharto
Website : https://www.linkedin.com/in/teguhth
========================================================== */
$host = "10.10.10.15"; // IP MaxScale
$port = 4306;
$db = "mysql";
$user = "admin";
$pass = "xxx";
try {
$pdo = new PDO(
"mysql:host=$host;port=$port;dbname=$db;charset=utf8mb4",
$user,
$pass,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
)
);
} catch (PDOException $e) {
die("Connection Failed : ".$e->getMessage());
}
function runTest($pdo, $sql)
{
try {
$stmt = $pdo->query($sql);
return $stmt->fetch();
} catch (Exception $e) {
return false;
}
}
$tests = array(
array(
"No"=>1,
"Hint"=>"SELECT @@hostname,@@version -- maxscale route to master",
"SQL"=>"SELECT
@@hostname AS hostname,
@@version AS version
-- maxscale route to master"
),
array(
"No"=>2,
"Hint"=>"SELECT @@hostname,@@version -- maxscale route to slave",
"SQL"=>"SELECT
@@hostname AS hostname,
@@version AS version
-- maxscale route to slave"
),
array(
"No"=>3,
"Hint"=>"SELECT @@hostname,@@version /* maxscale route to master */",
"SQL"=>"SELECT
@@hostname AS hostname,
@@version AS version
/* maxscale route to master */"
),
array(
"No"=>4,
"Hint"=>"SELECT @@hostname,@@version /* maxscale route to slave */",
"SQL"=>"SELECT
@@hostname AS hostname,
@@version AS version
/* maxscale route to slave */"
),
array(
"No"=>5,
"Hint"=>"SELECT @@hostname,@@version",
"SQL"=>"SELECT
@@hostname AS hostname,
@@version AS version"
)
);
?>
<!DOCTYPE html>
<html>
<head>
<title>MariaDB MaxScale Routing Test</title>
<style>
body{
font-family:Arial;
margin:20px;
}
table{
border-collapse:collapse;
width:100%;
}
th,td{
border:1px solid #ccc;
padding:8px;
}
th{
background:#f2f2f2;
}
.ok{
color:green;
font-weight:bold;
}
.err{
color:red;
font-weight:bold;
}
</style>
</head>
<body>
<h2>MariaDB MaxScale Routing Hint Test (PHP)</h2>
<table>
<tr>
<th>No</th>
<th>Routing Hint</th>
<th>Hostname</th>
<th>Version</th>
</tr>
<?php
foreach($tests as $t)
{
$r = runTest($pdo,$t["SQL"]);
echo "<tr>";
echo "<td>".$t["No"]."</td>";
echo "<td>".htmlspecialchars($t["Hint"])."</td>";
if($r)
{
echo "<td>".$r["hostname"]."</td>";
echo "<td>".$r["version"]."</td>";
}
else
{
echo "<td colspan='2' class='err'>FAILED</td>";
}
echo "</tr>";
}
?>
</table>
<br>
<div style="font-family:Courier New;font-size:14px">
Copyright by :
Teguh Triharto
<br>
Website :
<a href="https://www.linkedin.com/in/teguhth" target="_blank">
https://www.linkedin.com/in/teguhth
</a>
</div>
</body>
</html>
[root@teguhth-all html]#







No comments:
Post a Comment