a month ago
knex error shown while i want to connect mysql with strapi v5
1 Replies
a month ago
This thread has been opened as a bounty so the community can help solve it.
Status changed to Open Railway • about 1 month ago
a month ago
The issue is usually caused by an incorrect Strapi v5 database configuration or by using the wrong Railway MySQL environment variables.
For Strapi v5, make sure the MySQL driver is installed:
npm install mysql2
Then configure "config/database.ts" like this:
export default ({ env }) => ({
connection: {
client: 'mysql2',
connection: {
host: env('DATABASE_HOST'),
port: env.int('DATABASE_PORT', 3306),
database: env('DATABASE_NAME'),
user: env('DATABASE_USERNAME'),
password: env('DATABASE_PASSWORD'),
ssl: false,
},},
});
In Railway, map the MySQL service variables to the Strapi service:
DATABASE_HOST=${{MySQL.MYSQLHOST}}
DATABASE_PORT=${{MySQL.MYSQLPORT}}
DATABASE_NAME=${{MySQL.MYSQLDATABASE}}
DATABASE_USERNAME=${{MySQL.MYSQLUSER}}
DATABASE_PASSWORD=${{MySQL.MYSQLPASSWORD}}
If the MySQL service has a different name than "MySQL", replace "MySQL" with the actual Railway service name.
Also make sure you are NOT using:
DATABASE_HOST=localhost
because the Strapi service and MySQL service are separate Railway services. Railway provides the MySQL connection variables for service-to-service communication.
After updating the variables:
- Redeploy the Strapi service.
- Make sure "mysql2" is present in "package.json".
- Remove any old/incorrect Knex configuration.
- Check the deployment logs for the exact MySQL connection error.
Railway supports references to variables from another service using the "${{ServiceName.VARIABLE_NAME}}" syntax.