<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>proxysql Archives | Clever Cloud</title>
	<atom:link href="https://www.clever.cloud/blog/tag/proxysql/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.clever.cloud/blog/tag/proxysql/</link>
	<description>From Code to Product</description>
	<lastBuildDate>Wed, 05 May 2021 13:44:00 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://cdn.clever-cloud.com/uploads/2023/03/cropped-cropped-favicon-32x32.png</url>
	<title>proxysql Archives | Clever Cloud</title>
	<link>https://www.clever.cloud/blog/tag/proxysql/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Auto managed connection pooling for MySQL add-ons</title>
		<link>https://www.clever.cloud/blog/features/2021/05/05/proxysql-release/</link>
		
		<dc:creator><![CDATA[Arnaud Lefebvre]]></dc:creator>
		<pubDate>Wed, 05 May 2021 13:44:00 +0000</pubDate>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[connection]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[pool]]></category>
		<category><![CDATA[proxysql]]></category>
		<category><![CDATA[release]]></category>
		<guid isPermaLink="false">https://www2.cleverapps.io/wp/blog/technology/2021/05/05/proxysql-release/</guid>

					<description><![CDATA[<p><img width="1400" height="540" src="https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="mysql connection pooling 1" decoding="async" fetchpriority="high" srcset="https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1.jpg 1400w, https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1-300x116.jpg 300w, https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1-1024x395.jpg 1024w, https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1-768x296.jpg 768w, https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1-1368x528.jpg 1368w" sizes="(max-width: 1400px) 100vw, 1400px" /></p>Starting today, we provide <a href="https://www.proxysql.com/">ProxySQL</a>, a MySQL proxy, on all of our managed instances for out of the box connection pooling to your MySQL add-ons.

<span id="more-2987"></span>

ProxySQL acts as a local proxy between your application and your MySQL add-on. Your application will be able to connect to ProxySQL which will then forward your requests to the linked MySQL add-on.
<blockquote>A connection pool will cache your database connections and keep them open for future requests</blockquote>
Thanks to the connection pool, your database connections will be cached and your application won't have to wait for the connection to be established to your add-on. This can lead to faster response time when a lot of requests are executed. It also solves the problem of too many opened connections to the database. You will be able to do as many queries as you need without getting a "user max connection exceeded" error. ProxySQL will queue all the requests until a connection slot is available.

<em>Sidenote: ProxySQL is not available on Docker instances as we do not manage your Dockerfile.</em>
<h2 id="configure-proxysql">Configure ProxySQL</h2>
To enable ProxySQL for your application, you first need to have a MySQL add-on linked to your application. You then have to set the <code>CC_ENABLE_MYSQL_PROXYSQL=true</code> environment variable.

Once enabled, the <code>CC_MYSQL_PROXYSQL_SOCKET_PATH</code> environment variable will be injected during the deployment phase. It provides a <a href="https://en.wikipedia.org/wiki/Unix_domain_socket">Unix Domain Socket</a> path to use to connect to ProxySQL.

There are also two other environment variables you can set to configure ProxySQL to match your needs:
<ul>
 	<li><code>CC_MYSQL_PROXYSQL_USE_TLS</code>: A boolean (<code>true</code> or <code>false</code>) to enable <code>TLS</code> between ProxySQL and your MySQL add-on. Defaults to <code>true</code>.</li>
 	<li><code>CC_MYSQL_PROXYSQL_MAX_CONNECTIONS</code>: An integer which defines how many maximum connections ProxySQL will open to your MySQL add-on. Defaults to <code>10</code>. You can refer to <a href="https://www.clever.cloud/developers/deploy/addon/mysql/proxysql#scalability">our ProxySQL documentation</a> to learn more about how you should compute the maximum connection value when auto scalability is involved.</li>
</ul>
<h3 id="examples">Examples</h3>
Here are some examples on how to connect to ProxySQL using various languages:
<h4 id="php-using-pdo">PHP using PDO</h4>
Using <a href="https://www.php.net/manual/en/ref.pdo-mysql.connection.php">PDO</a>, you have to use the <code>unix_socket</code> option in your DSN:
<pre><code class="language-php">&lt;?php
// This variable is injected during the deployment
$socket = getenv("CC_MYSQL_PROXYSQL_SOCKET_PATH");
// Get the database name from the environment
$db = getenv("MYSQL_ADDON_DB");
// Get the database user from the environment
$user = getenv("MYSQL_ADDON_USER");
// Get the database password from the environment
$pass = getenv("MYSQL_ADDON_PASSWORD");
$dsn = "mysql:unix_socket=$socket;dbname=$db";
try {
  $pdo = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
  throw new PDOException($e-&gt;getMessage(), (int)$e-&gt;getCode());
}
</code></pre>
<h4 id="wordpress">Wordpress</h4>
For Wordpress, you can change the <code>DB_HOST</code> variable in your <code>wp-config.php</code>:
<pre><code class="language-php">// To connect using a unix socket, the syntax is: `localhost:/path/to/socket`
define('DB_HOST', "localhost:" . getenv("CC_MYSQL_PROXYSQL_SOCKET_PATH") );
// Get the database user from the environment
define('DB_USER', getenv("MYSQL_ADDON_USER"));
// Get the database password from the environment
define('DB_PASSWORD', getenv("MYSQL_ADDON_PASSWORD"));
// Get the database name from the environment
define('DB_NAME', getenv("MYSQL_ADDON_DB"));
</code></pre>
<h4 id="nodejs">Node.js</h4>
On Node.js, using the <code>mysql</code> npm package, you have to set the <code>socketPath</code> property :
<pre><code class="language-javascript">const mysql      = require('mysql');
const connection = mysql.createConnection({
  // Get ProxySQL unix domain socket path from the environment
  socketPath : process.env["CC_MYSQL_PROXYSQL_SOCKET_PATH"],
  // Get the database user from the environment
  user       : process.env["MYSQL_ADDON_USER"],
  // Get the database password from the environment
  password   : process.env["MYSQL_ADDON_PASSWORD"],
  // Get the database name from the environment
  database   : process.env["MYSQL_ADDON_DB"]
});
connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
});
</code></pre>
<h3 id="metrics">Metrics</h3>
ProxySQL exposes some metrics using the Prometheus format. Those metrics are automatically collected into our Metrics system and can be accessed in the <a href="https://www.clever.cloud/developers/administrate/metrics/overview/#display-metrics">Advanced Metrics</a> view of your application. This allows you to track how effective your connection pool is and how many request your instances are making. You can get the list of metrics we expose in <a href="https://www.clever.cloud/developers/deploy/addon/mysql/proxysql#metrics">our ProxySQL documentation</a>
<h3 id="documentation">Documentation</h3>
You can find more information about this feature in <a href="https://www.clever.cloud/developers/deploy/addon/mysql/proxysql">our ProxySQL documentation</a>.
<h3 id="conclusion">Conclusion</h3>
Using ProxySQL may help decrease the response time of your application if it does a lot of SQL requests. We'd love to hear from you on how ProxySQL's connection pool affected your applications performances.]]></description>
										<content:encoded><![CDATA[<p><img width="1400" height="540" src="https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="mysql connection pooling 1" decoding="async" srcset="https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1.jpg 1400w, https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1-300x116.jpg 300w, https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1-1024x395.jpg 1024w, https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1-768x296.jpg 768w, https://cdn.clever-cloud.com/uploads/2021/08/mysql-connection-pooling-1-1368x528.jpg 1368w" sizes="(max-width: 1400px) 100vw, 1400px" /></p>Starting today, we provide <a href="https://www.proxysql.com/">ProxySQL</a>, a MySQL proxy, on all of our managed instances for out of the box connection pooling to your MySQL add-ons.

<span id="more-2987"></span>

ProxySQL acts as a local proxy between your application and your MySQL add-on. Your application will be able to connect to ProxySQL which will then forward your requests to the linked MySQL add-on.
<blockquote>A connection pool will cache your database connections and keep them open for future requests</blockquote>
Thanks to the connection pool, your database connections will be cached and your application won't have to wait for the connection to be established to your add-on. This can lead to faster response time when a lot of requests are executed. It also solves the problem of too many opened connections to the database. You will be able to do as many queries as you need without getting a "user max connection exceeded" error. ProxySQL will queue all the requests until a connection slot is available.

<em>Sidenote: ProxySQL is not available on Docker instances as we do not manage your Dockerfile.</em>
<h2 id="configure-proxysql">Configure ProxySQL</h2>
To enable ProxySQL for your application, you first need to have a MySQL add-on linked to your application. You then have to set the <code>CC_ENABLE_MYSQL_PROXYSQL=true</code> environment variable.

Once enabled, the <code>CC_MYSQL_PROXYSQL_SOCKET_PATH</code> environment variable will be injected during the deployment phase. It provides a <a href="https://en.wikipedia.org/wiki/Unix_domain_socket">Unix Domain Socket</a> path to use to connect to ProxySQL.

There are also two other environment variables you can set to configure ProxySQL to match your needs:
<ul>
 	<li><code>CC_MYSQL_PROXYSQL_USE_TLS</code>: A boolean (<code>true</code> or <code>false</code>) to enable <code>TLS</code> between ProxySQL and your MySQL add-on. Defaults to <code>true</code>.</li>
 	<li><code>CC_MYSQL_PROXYSQL_MAX_CONNECTIONS</code>: An integer which defines how many maximum connections ProxySQL will open to your MySQL add-on. Defaults to <code>10</code>. You can refer to <a href="https://www.clever.cloud/developers/deploy/addon/mysql/proxysql#scalability">our ProxySQL documentation</a> to learn more about how you should compute the maximum connection value when auto scalability is involved.</li>
</ul>
<h3 id="examples">Examples</h3>
Here are some examples on how to connect to ProxySQL using various languages:
<h4 id="php-using-pdo">PHP using PDO</h4>
Using <a href="https://www.php.net/manual/en/ref.pdo-mysql.connection.php">PDO</a>, you have to use the <code>unix_socket</code> option in your DSN:
<pre><code class="language-php">&lt;?php
// This variable is injected during the deployment
$socket = getenv("CC_MYSQL_PROXYSQL_SOCKET_PATH");
// Get the database name from the environment
$db = getenv("MYSQL_ADDON_DB");
// Get the database user from the environment
$user = getenv("MYSQL_ADDON_USER");
// Get the database password from the environment
$pass = getenv("MYSQL_ADDON_PASSWORD");
$dsn = "mysql:unix_socket=$socket;dbname=$db";
try {
  $pdo = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
  throw new PDOException($e-&gt;getMessage(), (int)$e-&gt;getCode());
}
</code></pre>
<h4 id="wordpress">Wordpress</h4>
For Wordpress, you can change the <code>DB_HOST</code> variable in your <code>wp-config.php</code>:
<pre><code class="language-php">// To connect using a unix socket, the syntax is: `localhost:/path/to/socket`
define('DB_HOST', "localhost:" . getenv("CC_MYSQL_PROXYSQL_SOCKET_PATH") );
// Get the database user from the environment
define('DB_USER', getenv("MYSQL_ADDON_USER"));
// Get the database password from the environment
define('DB_PASSWORD', getenv("MYSQL_ADDON_PASSWORD"));
// Get the database name from the environment
define('DB_NAME', getenv("MYSQL_ADDON_DB"));
</code></pre>
<h4 id="nodejs">Node.js</h4>
On Node.js, using the <code>mysql</code> npm package, you have to set the <code>socketPath</code> property :
<pre><code class="language-javascript">const mysql      = require('mysql');
const connection = mysql.createConnection({
  // Get ProxySQL unix domain socket path from the environment
  socketPath : process.env["CC_MYSQL_PROXYSQL_SOCKET_PATH"],
  // Get the database user from the environment
  user       : process.env["MYSQL_ADDON_USER"],
  // Get the database password from the environment
  password   : process.env["MYSQL_ADDON_PASSWORD"],
  // Get the database name from the environment
  database   : process.env["MYSQL_ADDON_DB"]
});
connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }

  console.log('connected as id ' + connection.threadId);
});
</code></pre>
<h3 id="metrics">Metrics</h3>
ProxySQL exposes some metrics using the Prometheus format. Those metrics are automatically collected into our Metrics system and can be accessed in the <a href="https://www.clever.cloud/developers/administrate/metrics/overview/#display-metrics">Advanced Metrics</a> view of your application. This allows you to track how effective your connection pool is and how many request your instances are making. You can get the list of metrics we expose in <a href="https://www.clever.cloud/developers/deploy/addon/mysql/proxysql#metrics">our ProxySQL documentation</a>
<h3 id="documentation">Documentation</h3>
You can find more information about this feature in <a href="https://www.clever.cloud/developers/deploy/addon/mysql/proxysql">our ProxySQL documentation</a>.
<h3 id="conclusion">Conclusion</h3>
Using ProxySQL may help decrease the response time of your application if it does a lot of SQL requests. We'd love to hear from you on how ProxySQL's connection pool affected your applications performances.]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
