sidebarLevel |
---|
3 |
Craft can connect to MySQL and Postgres databases.
Database connection settings may be set from a config/db.php
file, but because they’re often entirely environment-specific, Craft supports assigning directly from environment variables. In a new Craft 4 project, your .env
file will need to define these options:
# Required variables:
CRAFT_APP_ID=
CRAFT_ENVIRONMENT=dev
CRAFT_SECURITY_KEY=
# Database-specific variables:
CRAFT_DB_DRIVER=mysql
CRAFT_DB_SERVER=127.0.0.1
CRAFT_DB_PORT=3306
CRAFT_DB_DATABASE=
CRAFT_DB_USER=root
CRAFT_DB_PASSWORD=
CRAFT_DB_SCHEMA=public
CRAFT_DB_TABLE_PREFIX=
We recommend this approach because it:
- Keeps sensitive information out of your project’s codebase (
.env
files should never be shared or committed to version control); - Makes collaborating with other developers easier, as each developer can define their own settings from scratch, without overwriting someone else’s settings.
::: tip Environment overrides are covered in greater detail on the configuration overview page. :::
If you need to use your own environment variables in a config file (or connection details are provided via platform-specific keys), create config/db.php
and return an explicit array of settings:
use craft\helpers\App;
return [
'driver' => App::env('MY_DB_DRIVER'),
'server' => App::env('MY_DB_SERVER'),
'port' => App::env('MY_DB_PORT'),
'database' => App::env('MY_DB_DATABASE'),
'user' => App::env('MY_DB_USER'),
'password' => App::env('MY_DB_PASSWORD'),
'schema' => App::env('MY_DB_SCHEMA'),
'tablePrefix' => App::env('MY_DB_TABLE_PREFIX'),
'attributes' => [
PDO::MYSQL_ATTR_SSL_CA => '/path/to/my.crt.pem'
// ...
],
];
::: warning
Finer-grain control of Craft’s database connection is possible by configuring the underlying db
application component. This may be necessary if you have specific security requirements, or your app needs to connect to multiple databases.
Note that if you need to supply custom PDO attributes to your primary database connection, you should do so via the attributes
key in your config/db.php
file, not from config/app.php
while overriding the db
component.
:::
!!!include(docs/.artifacts/cms/4.x/config-db.md)!!!