Advanced database tips
Published
Updated
⚙️
Advanced MySQL configuration.
MySQL on fortrabbit runs in UTC, gives each environment one database without CREATE DATABASE or SUPER privileges, and supports InnoDB tables only. How to work with the time zone, the password and the storage engine.
Time zone
MySQL on fortrabbit runs in UTC. Handle other time zones in the app, or per connection in MySQL, see MySQL time zone support.
Setting time zone in MySQL
-- set time zone to +3 hours
SET time_zone = '+03:00';
-- set time zone to -7 hours
SET time_zone = '-07:00';
-- show the time zone of the connection
SELECT @@session.time_zone;
Setting time zone with PDO
PDO runs a command right after it connects:
$pdo = new PDO(
'mysql:host=' . getenv('FORTRABBIT_DB_HOST') . ';dbname=' . getenv('FORTRABBIT_DB_NAME'),
getenv('FORTRABBIT_DB_USER'),
getenv('FORTRABBIT_DB_PASSWORD'),
[PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '+02:00'"]
);
Resetting the MySQL password
The MySQL settings of the environment in the dashboard reset the password. Apps that read it from ENV vars pick up the new one. Config files with the password, and the saved connections of local database clients, need the new one by hand.
Connecting from local code
A local app can connect to the fortrabbit database through an SSH tunnel. It feels slow, and local tests write into the live data. Use a local database and sync it instead.
MySQL limits
Each environment has one database, named like the environment ID: {{app-env-id}}. The MySQL user cannot run CREATE DATABASE or CREATE SCHEMA.
Creating functions, procedures and triggers needs the SUPER privilege, which the user does not have. Dumps that contain them, or GTID statements, fail on import, see database troubleshooting.
Storage engine
fortrabbit supports the InnoDB storage engine only. Convert MyISAM and other tables before the import, or it fails. To find them, run this on the source database:
SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'your_database_name' AND ENGINE != 'InnoDB';
Option 1: Convert in the source database
Run this for each table before creating the dump:
ALTER TABLE table_name ENGINE=InnoDB;
Option 2: Edit the SQL dump file
Replace the engine in the dump file with a text editor or on the command line:
sed -i 's/ENGINE=MyISAM/ENGINE=InnoDB/g' your-dump.sql