Connecting multiple MySQL databases

Hi,

I’m attempting to plug multiple MySQL databases to my existing forest admin panel. I was following along with the documentation but got stuck on something. How do I obtain the database connection strings for my databases?

The databases are all running on the same port, same MySQL server.

Thank you!

“express”: “~4.17.1”,
“express-jwt”: “5.3.1”,
“forest-express-sequelize”: “^7.0.0”,
“morgan”: “1.9.1”,
“require-all”: “^3.0.0”,
“sequelize”: “~5.15.1”,
“mysql2”: “~2.2.5”

Hello @Arda_Yurdakul,

Just to be sure I understand your question.

Are you following this documentation page?

If so, with databases running within the same SQL server, you can edit your .env file, check the value of DATABASE_URL and change the last part of it to build the new URL.

Hi, thank you for your reply!

I am in fact using that page. What I’m stuck on is what to write on the .env page actually. I understand I need to create environment variables for my database connection strings, but I do not know how to get the connection strings themselves. Basically what to input in these fields.

DATABASE_DEMO=postgres://forest:secret@localhost:5416/forest_demo
DATABASE_BILLING=postgres://forest:secret@localhost:5417/forest_billing

Hey @Arda_Yurdakul,

Actually, the mentioned documentation is not up-to-date with our latest development. Since you are using v7, you should hopefully have everything set up to easily add a new database of the same dialect to your project.

You should have a config/databases.js file in your project, that should contains something like:

module.exports = [{
  name: 'default',
  modelsDir: path.resolve(__dirname, '../models'),
  connection: {
    url: process.env.DATABASE_URL,
    options: { ...databaseOptions },
  },
}];

You should be able to add a new connection here, to your other database, like so:

module.exports = [{
  name: 'default',
  modelsDir: path.resolve(__dirname, '../models/your_database1_models'),
  connection: {
    url: process.env.DATABASE_URL,
    options: { ...databaseOptions },
  },
}, {
  name: 'another_connection',
  modelsDir: path.resolve(__dirname, '../models/your_database2_models'),
  connection: {
    url: process.env.ANOTHER_DATABASE_URL,
    options: { ...databaseOptions },
  },
}];

If everything goes well from here, running lumber update should create your missing folders & models for the new database. You will still have to remove previous files located in the models/ folder (except, of course, models/index.js).

Concerning the connection string, if you are using a SQL client to connect to your databases, it should be able to provide you the connection strings. If not, these are composed of your_dialect://your_db_username:your_db_passwd@your_db_ip:your_db_port/your_db_name

I’m not sure if that’s clear enough, but in case it doesn’t, just let me know.

1 Like

That did work thank you. All the new files are created and the project runs fine. The only problem is that the new tables are not visible in the admin panel. I tried shutting everything down and refreshing but that didn’t seem to work. Am I missing a step? Thank you again.

For clarification: The tables from the initial database are there, but the tables from the 2nd one are not showing up.

@Arda_Yurdakul,

You can debug this by:

  • Checking in your .forestadmin-schema.json that the models of your second database are present.
  • If that’s the case, you should see these collections using the layout editor (See screenshot) and enable visibility on the said collection.

Let me know if that helps.

Yes they were indeed hidden. Thank you so much, I’ve been stuck on this for a long time! :smiley:

1 Like