Magento MySQL Config: How to Find and Edit Database Config File

June 10, 2021

Introduction

Magento 2 uses MySQL (or MariaDB) to store data and streamline background processes. Configuring the MySQL database parameters is mandatory when installing Magento 2 in Linux.

If the MySQL user credentials or database server settings change, the parameters in the Magento database config file need to reflect those changes.

Learn where to find and how to edit the database config file in Magento 2.

Merchant making adjustments to Magetno config files.

Why Edit Magento Database Config File?

The database config file contains information Magento needs to connect and interact with the MySQL database.

You need to edit the settings in the config file if:

  • The database hostname changed.
  • The database password or username was modified.
  • You plan to use a different database for the Magento store.

Note: Learn everything you need to know about Magento database types.


How To Find and Edit Magento Database Config File?

The database connection settings in Magento are in the env.php file. The full path to the env.php file in Magento 2 is:

/Magento_Installation_Directory/app/etc/env.php

This file also contains settings for the Magento admin URL, cron jobs, encryption key, etc.

Location of the database config file in the Magetno sub-directory.

To edit the Magento database config settings:

1. Establish an SSH connection with the Magento server.

2. Use the command-line interface (CLI) to access the /app/etc/ sub-directory in the Magento root:

cd /var/www/html/magento2.4/app/etc

In this example, Magento is installed in the /var/www/html/magento2.4 directory. Adjust the command to match the Magento root directory path on your system.

3. Open the env.php file in a text editor, such as nano:

sudo nano env.php
Using a text editor to edit the env.php file in Magento.

4. Locate the db section in the env.php file and make the necessary edits.

Contents of the env.php file in Magento.

5. Edit the following parameters:

PARAMETERVALUE
hostEnter the hostname of the database server. If Magento is on the same server as the database, use localhost.
dbnameThe name of the Magento MySQL database.
usernameEnter the username of the MySQL database user.
passwordThe password for the MySQL database user.

6. Save the changes and exit the env.php file.

Conclusion

You have successfully edited the Magento database config file. Be prepared to adjust the database settings in Magento regularly.

Ecommerce stores need to scale their servers and databases often to maintain optimal website performance. Any change to the store's underlying infrastructure can prevent Magento from interacting with the MySQL server.

About the author
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at CCBill. He has more than 8 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His engaging writing style provides practical advice and aims to spark curiosity for innovative technologies.
Talk to a Merchant Support Specialist