Skip Navigation
Show nav
Heroku Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku Architecture
    • Dynos (app containers)
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Command Line
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery
    • Continuous Integration
  • Language Support
    • Node.js
    • Ruby
      • Working with Bundler
      • Rails Support
    • Python
      • Background Jobs in Python
      • Working with Django
    • Java
      • Working with Maven
      • Java Database Operations
      • Working with the Play Framework
      • Working with Spring Boot
      • Java Advanced Topics
    • PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
    • Heroku Redis
    • Apache Kafka on Heroku
    • Other Data Stores
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
    • Compliance
  • Heroku Enterprise
    • Private Spaces
      • Infrastructure Networking
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
    • Single Sign-on (SSO)
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Integrating with Salesforce
  • Add-ons
  • All Add-ons
  • Stackhero for MySQL
Stackhero for MySQL

This add-on is operated by Stackhero

MySQL on dedicated instances, up-to-date versions and super attractive prices.

Stackhero for MySQL

Last updated March 01, 2022

Table of Contents

  • Provisioning the add-on
  • Local setup
  • Connect to MySQL from your favorite language
  • Connect to MySQL from PHP
  • Connect from Node.js to MySQL
  • Connect to Stackhero dashboard
  • Connect to PhpMyAdmin
  • Create a user on MySQL
  • Upgrading your plan
  • Removing the add-on
  • Error “Authentication plugin ‘caching_sha2_password’ cannot be loaded”
  • Support
  • Additional resources

Stackhero for MySQL provides a managed MySQL instance running on a fully dedicated instance.

With your Stackhero for MySQL add-on you will get:

  • A private instance (dedicated VM) for high performances and security
  • A dedicated public IP (v4)
  • TLS encryption (aka SSL)
  • A full access to PhpMyAdmin
  • An automatic backup every 24 hours
  • One click to update to new MySQL versions

All MySQL clients can connect to Stackhero for MySQL and there is a MySQL client library for almost every language out there, including Ruby, Node.js, Java, Python, Clojure and Erlang.

Provisioning the add-on

Stackhero for MySQL can be attached to a Heroku application via the CLI:

A list of all plans available can be found here.

$ heroku addons:create ah-mysql-stackhero --app <your app name>
-----> Adding ah-mysql-stackhero to sharp-mountain-4005... done

After you provision Stackhero for MySQL, the STACKHERO_MYSQL_HOST and STACKHERO_MYSQL_ROOT_PASSWORD config variables are available in your app’s configuration. They contain the URLs to your MySQL instance as its root password.

You can see the content of those variables via the heroku config:get command:

$ heroku config:get STACKHERO_MYSQL_HOST

After you install Stackhero for MySQL, your application should be configured to fully integrate with the add-on.

Local setup

After you provision the add-on, it’s necessary to locally replicate its config variables so your development environment can operate against the service.

Use the Heroku Local command-line tool to configure, run and manage process types specified in your app’s Procfile. Heroku Local reads configuration variables from a .env file. To view all of your app’s config variables, type heroku config. Use the following command for each value that you want to add to your .env file:

$ heroku config:get STACKHERO_MYSQL_HOST -s  >> .env

Credentials and other sensitive configuration values should not be committed to source-control. In Git, exclude the .env file with: echo .env >> .gitignore.

For more information, see the Heroku Local article.

Connect to MySQL from your favorite language

You can use almost any client library available for your language that is able to connect to a MySQL server.

We recommend to create a dedicated user (see sections below). If you don’t want to, you can connect to MySQL using the “root” user and the following environment variables:

  • STACKHERO_MYSQL_HOST
  • STACKHERO_MYSQL_ROOT_PASSWORD

If you have created a dedicated user, you can use these environment variables:

  • STACKHERO_MYSQL_HOST
  • STACKHERO_MYSQL_USER
  • STACKHERO_MYSQL_PASSWORD

Connect to MySQL from PHP

In these examples, we will use the “MySQL Improved Extension” known as MySQLi.

Connect PHP to MySQL with object-oriented style

<?php

$hostname = '<XXXXXX>.stackhero-network.com';
$user = 'root';
$password = '<yourPassword>';
$database = 'mysql'; // You shouldn't use the "mysql" database. This is just for the example. The recommended way is to create a dedicated database (and user) in PhpMyAdmin and use it then here.

$mysqli = mysqli_init();
$mysqliConnected = $mysqli->real_connect($hostname, $user, $password, $database, NULL, NULL, MYSQLI_CLIENT_SSL);
if (!$mysqliConnected) {
  die("Connect Error: " . $mysqli->connect_error());
}

echo 'Success... ' . $mysqli->host_info . "\n";

$mysqli->close();

?>

Connect PHP to MySQL with procedural style

<?php

$hostname = '<XXXXXX>.stackhero-network.com';
$user = 'root';
$password = '<yourPassword>';
$database = 'mysql'; // You shouldn't use the "mysql" database. This is just for the example. The recommended way is to create a dedicated database (and user) in PhpMyAdmin and use it then here.

$mysqli = mysqli_init();
$mysqliConnected = mysqli_real_connect($mysqli, $hostname, $user, $password, $database, NULL, NULL, MYSQLI_CLIENT_SSL);
if (!$mysqliConnected) {
  die("Connect Error: " . mysqli_connect_error($mysqli));
}

echo 'Success... ' . mysqli_get_host_info($mysqli) . "\n";

mysqli_close($mysqli);

?>

Connect PHP to MySQL with using credentials from environment variables

We recommend to not keep your credentials in your source code but use environment variables in place.

Here is how to retrieve these credentials:

$hostname = parse_url(getenv('STACKHERO_MYSQL_HOST'));
$user = parse_url(getenv('STACKHERO_MYSQL_USER'));
$password = parse_url(getenv('STACKHERO_MYSQL_PASSWORD'));
$database = parse_url(getenv('STACKHERO_MYSQL_USER'));

Connect from Node.js to MySQL

In this example we will use the official xdevapi package that will use the new MySQL X protocol. To install it simply run this command: npm install --save @mysql/xdevapi

const mysqlx = require('@mysql/xdevapi');

(async () => {
  // Connection to MySQL using MySQL X Protocol
  const session = await mysqlx.getSession({
    host: process.env.STACKHERO_MYSQL_HOST,
    user: 'root',
    password: process.env.STACKHERO_MYSQL_ROOT_PASSWORD,
  });


  // Create a schema (database) if not exists
  const schemaExists = await session.getSchema('stackherotest').existsInDatabase();
  if (!schemaExists) {
    await session.createSchema('stackherotest');
  }

  // Create table "users" if not exists
  const tableExists = await session
    .getSchema('stackherotest')
    .getTable('users')
    .existsInDatabase();
  if (!tableExists) {
    await session
      .sql('CREATE TABLE `stackherotest`.`users` '
        + '('
        + '`userId` INT UNSIGNED NOT NULL,'
        + '`name` VARCHAR(128) NOT NULL,'
        + '`address` TEXT NOT NULL,'
        + '`email` VARCHAR(265) NOT NULL'
        + ') '
        + 'ENGINE = InnoDB;')
      .execute();
  }


  // Insert a fake user
  await session
    .getSchema('stackherotest') // Database name
    .getTable('users') // Table name
    .insert('userId', 'name', 'address', 'email') // Columns names
    .values(
      Math.round(Math.random() * 100000), // Generate a fake userId
      'User name', // column 'name'
      'User address', // column 'address'
      'user@email.com' // column 'email'
    )
    .execute();


  // Count number of rows in table users
  const usersCount = await session
    .getSchema('stackherotest') // Database name
    .getTable('users')
    .count();

  console.log(`There is now ${usersCount} in table "users"`);

  // Close the connection to MySQL
  await session.close();

})().catch(error => {
  console.error('');
  console.error('🐞 An error occurred!');
  console.error(error);
  process.exit(1);
});

Connect to Stackhero dashboard

Stackhero dashboard allows you to see your instance usage, restart it, and apply updates. It also gives you the ability to access the PhpMyAdmin UI to consult your MySQL data directly in a graphical way.

You can access the dashboard via the CLI:

$ heroku addons:open ah-mysql-stackhero
Opening ah-mysql-stackhero for sharp-mountain-4005

or by visiting the Heroku Dashboard and selecting the application in question. Select Stackhero for MySQL from the Add-ons menu.

Connect to PhpMyAdmin

PhpMyAdmin is a web UI that gives you access to your MySQL in a graphical way.

To connect to it, you have to first connect to your Stackhero dashboard (see above).

Then, select your MySQL service, click on the “Configure” button and copy the PhpMyAdmin user and password. Go back to the previous page and click on the “PhpMyAdmin” link. Login with the PhpMyAdmin user (admin per default) and the PhpMyAdmin password you copied just before.

Create a user on MySQL

A best practice is to create a user for your application other than the default “root”.

The easiest way to do that is to use PhpMyAdmin.

  1. In PhpMyAdmin, click on User accounts on top.

  2. Click on Add user account.

  3. Fill the user creation form:

    • Define an account name (generally your application name)
    • Click on “Generate password” to get a secured password (copy it to your clipboard)
    • Check the “Create database with same name and grant all privileges”

Once validated, the user will be created as its database which will get the same name as the username.

Add your user to environment variables

You can now define your environment variables as follow:

$ heroku config:set STACKHERO_MYSQL_USER=<yourUserName>
$ heroku config:set STACKHERO_MYSQL_PASSWORD=<yourUserPassword>

And if you need to access them locally, for development purporse for example, you can save them to you .env file:

$ heroku config:get STACKHERO_MYSQL_USER -s  >> .env
$ heroku config:get STACKHERO_MYSQL_PASSWORD -s  >> .env

Upgrading your plan

You cannot downgrade an existing add-on.

 

Application owners should carefully manage the migration timing to ensure proper application function during the migration process.

Use the heroku addons:upgrade command to migrate to a new plan.

$ heroku addons:upgrade ah-mysql-stackhero:newplan
-----> Upgrading ah-mysql-stackhero:newplan to sharp-mountain-4005... done
       Your plan has been updated to: ah-mysql-stackhero:newplan

Removing the add-on

You can remove Stackhero for MySQL via the CLI:

This will destroy all associated data and cannot be undone!

$ heroku addons:destroy ah-mysql-stackhero
-----> Removing ah-mysql-stackhero from sharp-mountain-4005... done

Error “Authentication plugin ‘caching_sha2_password’ cannot be loaded”

MySQL 8 uses “Caching SHA2 password” authentication system by default.

With older client libraries this could lead to errors like “Authentication plugin ‘caching_sha2_password’ cannot be loaded” when trying to connect.

To resolve this error, you can change the default authentication from “Caching SHA2 password” to the previous one “MySQL native password”.

In your Stackhero dashboard, go to your MySQL configuration and simply select “MySQL native password” as “Authentication plugin”. This will change the authentication system for the “root” user and for future users creation.

For other users than “root” that are yet created, you can connect via MySQL CLI or PhpMyAdmin and execute these commands:

USE mysql;
ALTER USER '<userToUpdate>'@'%' IDENTIFIED WITH mysql_native_password BY '<userPassword>';
FLUSH PRIVILEGES;

Support

Stackhero for MySQL support and runtime issues should be submitted via one of the Heroku Support channels. We recommend adding support@stackhero.io in copy for urgent issues.

Additional resources

  • MySQL documentation by Stackhero
  • MySQL managed cloud

Keep reading

  • All Add-ons

Feedback

Log in to submit feedback.

Ziggeo Stackhero for RabbitMQ

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Podcasts
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing

Subscribe to our monthly newsletter

Your email address:

  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Heroku Podcasts
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Facebook
  • Instagram
  • Github
  • LinkedIn
  • YouTube
Heroku is acompany

 © Salesforce.com

  • heroku.com
  • Terms of Service
  • Privacy
  • Cookies
  • Cookie Preferences