Sharing a database between stacks

How to share a single database between stacks

There are a number of reasons why you might want to share a database between your stacks. For example, you can test UI changes with your users while still using production data.

It is not a good idea to share your database between stacks in certain scenarios (eg. for a reporting tool) - instead we recommend that you use database replication to use a master/slave setup.

Your first stack will be deployed as normal, with a database managed by Cloud 66. Your second stack will be deployed with an external database (as it will use the first stacks database).

Important

Ensure that you do not select the option for db:schema:load during the build of your second stack, as this could destroy the data on the first stack.

Firstly, you need to open your firewall on the first stack to allow your second stacks web servers to access the database.

You will then reference the database credentials from your first stack in the database.yml of your second stack. You can reference the environment variables for these credentials on your first stack like so (your stack UID is available on your Stack information page):

{{ STACK[STACK_UID].ENV_VAR }}

For example, your environment variables would be set like this:

MYSQL_ADDRESS={{ STACK[xyz].MYSQL_ADDRESS_INT }}
MYSQL_DATABASE={{ STACK[xyz].MYSQL_DATABASE }}

Database credentials such as username and password are not available for cross-stack referencing for security reasons. Instead, copy and paste them across as environment variables. Your database.yml would look something like this:

username: <%= ENV['MYSQL_USERNAME'] %>
password: <%= ENV['MYSQL_PASSWORD'] %>
hostname: <%= ENV['MYSQL_ADDRESS'] %>
database: <%= ENV['MYSQL_DATABASE'] %>

 

You must be logged in to comment on this article